You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

9 years ago
9 years ago
9 years ago
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class Controller {
  5. private function _is_logged_in(&$request, &$response) {
  6. session_start();
  7. if(!session('user_id')) {
  8. session_destroy();
  9. $response->setStatusCode(302);
  10. $response->headers->set('Location', '/login?return_to='.$request->getPathInfo());
  11. return false;
  12. } else {
  13. return true;
  14. }
  15. }
  16. public function index(Request $request, Response $response) {
  17. $response->setContent(view('index', [
  18. 'title' => 'Telegraph'
  19. ]));
  20. return $response;
  21. }
  22. public function dashboard(Request $request, Response $response) {
  23. if(!$this->_is_logged_in($request, $response)) {
  24. return $response;
  25. }
  26. // If there is an account in the query string, set the session variable and redirect back to the dashboard
  27. if($request->get('account') || !session('account')) {
  28. // Check that the user has permission to access this account
  29. $role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
  30. if(!$role) {
  31. $role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
  32. ->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
  33. }
  34. $_SESSION['account'] = $role->site_id;
  35. $response->setStatusCode(302);
  36. $response->headers->set('Location', '/dashboard');
  37. return $response;
  38. }
  39. $response->setContent(view('dashboard', [
  40. 'title' => 'Telegraph Dashboard',
  41. 'user' => $this->_user(),
  42. 'accounts' => $this->_accounts()
  43. ]));
  44. return $response;
  45. }
  46. private function _user() {
  47. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  48. }
  49. private function _accounts() {
  50. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  51. ->where('roles.user_id', session('user_id'))
  52. ->find_many();
  53. }
  54. }