Browse Source

dashboard stub with account switcher

pull/3/head
Aaron Parecki 8 years ago
parent
commit
d3b14ec8af
4 changed files with 63 additions and 5 deletions
  1. +28
    -2
      controllers/Controller.php
  2. +9
    -0
      lib/helpers.php
  3. +1
    -3
      views/dashboard.php
  4. +25
    -0
      views/layout-loggedin.php

+ 28
- 2
controllers/Controller.php View File

@ -6,7 +6,7 @@ class Controller {
private function _is_logged_in(&$request, &$response) {
session_start();
if(!array_key_exists('user_id', $_SESSION)) {
if(!session('user_id')) {
session_destroy();
$response->setStatusCode(302);
$response->headers->set('Location', '/login?return_to='.$request->getPathInfo());
@ -28,10 +28,36 @@ class Controller {
return $response;
}
// If there is an account in the query string, set the session variable and redirect back to the dashboard
if($request->get('account') || !session('account')) {
// Check that the user has permission to access this account
$role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
if(!$role) {
$role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
}
$_SESSION['account'] = $role->site_id;
$response->setStatusCode(302);
$response->headers->set('Location', '/dashboard');
return $response;
}
$response->setContent(view('dashboard', [
'title' => 'Telegraph Dashboard'
'title' => 'Telegraph Dashboard',
'user' => $this->_user(),
'accounts' => $this->_accounts()
]));
return $response;
}
private function _user() {
return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
}
private function _accounts() {
return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
->where('roles.user_id', session('user_id'))
->find_many();
}
}

+ 9
- 0
lib/helpers.php View File

@ -34,3 +34,12 @@ function random_string($len) {
}
return $str;
}
function display_url($url) {
return preg_replace(['/^https?:\/\//','/\/$/'], '', $url);
}
function session($k, $default=null) {
if(!isset($_SESSION)) return $default;
return array_key_exists($k, $_SESSION) ? $_SESSION[$k] : $default;
}

+ 1
- 3
views/dashboard.php View File

@ -1,3 +1 @@
<?php $this->layout('layout', ['title' => $title]); ?>
<?= print_r($_SESSION); ?>
<?php $this->layout('layout-loggedin', ['title' => $title, 'accounts' => $accounts, 'user' => $user]); ?>

+ 25
- 0
views/layout-loggedin.php View File

@ -0,0 +1,25 @@
<?php $this->layout('layout', ['title' => $title]); ?>
<div class="ui fixed inverted menu">
<div class="ui container">
<a href="/" class="header item">
<img class="logo" src="/assets/telegraph-logo-256.png">
Telegraph
</a>
<a href="/dashboard" class="item">Dashboard</a>
<a href="/api" class="item">API</a>
<div class="ui right simple dropdown item">
Sites <i class="dropdown icon"></i>
<div class="menu">
<div class="header"><?= display_url($user->url) ?></div>
<?php foreach($accounts as $account): ?>
<a class="item" href="/dashboard?account=<?= $account->id ?>"><?= $this->e($account->name) ?></a>
<?php endforeach; ?>
<div class="divider"></div>
<a class="item" href="/new-site">New Site</a>
<a class="item" href="/logout">Log Out</a>
</div>
</div>
</div>
</div>
<?= $this->section('content') ?>

Loading…
Cancel
Save