AppUsersController.php


SUBMITTED BY: Guest

DATE: Aug. 7, 2014, 10:04 a.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 938

  1. <?php
  2. App::uses('UsersController', 'Users.Controller');
  3. class AppUsersController extends UsersController {
  4. public $name = 'AppUsers';
  5. public function admin() {
  6. if ($this->Auth->user()) {
  7. if ($this->Auth->user('role') == 'admin') {
  8. return $this->redirect(array(
  9. 'plugin' => 'users',
  10. 'controller' => 'users',
  11. 'action' => 'index',
  12. 'admin' => true
  13. ));
  14. } else {
  15. $this->Session->setFlash(__('Sorry! You doesn\'t have access to administrator area'));
  16. return $this->redirect(array(
  17. 'plugin' => 'users',
  18. 'controller' => 'users',
  19. 'action' => 'index',
  20. 'admin' => false
  21. ));
  22. }
  23. }
  24. }
  25. protected function _setupAuth() {
  26. parent::_setupAuth();
  27. $role = $this->Auth->user('role');
  28. $this->getEventManager()->attach(function ($event) use ($role) {
  29. if ($role == 'admin') {
  30. // set Auth login redirect to /admin/users
  31. } else {
  32. // set Auth login redirect to /users
  33. }
  34. }, 'Users.Controller.Users.afterLogin');
  35. }
  36. public function beforeFilter() {
  37. parent::beforeFilter();
  38. $this->User = ClassRegistry::init('AppUser');
  39. $this->set('model', 'AppUser');
  40. }
  41. public function beforeRender() {
  42. parent::beforeRender();
  43. $this->response->disableCache();
  44. }
  45. public function render($view = null, $layout = null) {
  46. if (is_null($view)) {
  47. $view = $this->action;
  48. }
  49. $viewPath = substr(get_class($this), 0, strlen(get_class($this)) - 10);
  50. if (!file_exists(APP . 'View' . DS . $viewPath . DS . $view . '.ctp')) {
  51. $this->plugin = 'Users';
  52. $this->viewPath = 'Users';
  53. } else {
  54. $this->viewPath = $viewPath;
  55. }
  56. return parent::render($view, $layout);
  57. }
  58. }

comments powered by Disqus