PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/controller/base.php

https://github.com/pratikdhaboo/kodelearn
PHP | 188 lines | 135 code | 12 blank | 41 comment | 22 complexity | 6ec6226a74d168b07baee2a5aa9efa48 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Controller_Base extends Controller_Template {
  3. protected $content;
  4. public $template = 'template/template';
  5. protected $view;
  6. /**
  7. * Override the before method
  8. * check if ajax and select correct template
  9. */
  10. public function before()
  11. {
  12. $this->auth_filter();
  13. if (Auth::instance()->logged_in()) {
  14. $this->acl_filter();
  15. }
  16. // create a new Config reader and attach to the Config instance
  17. $config = Config::instance();
  18. $config->attach(new Config_Database());
  19. $this->template_filter();
  20. if ($this->request->is_ajax() || !$this->request->is_initial()) {
  21. $this->view = View::factory('template/content');
  22. } else {
  23. $this->view = View::factory($this->template);
  24. Breadcrumbs::add(array('Home', Url::site('home')));
  25. }
  26. return parent::before();
  27. }
  28. public function action_index()
  29. {
  30. $this->response->body('hello, world!');
  31. }
  32. /**
  33. * Check whether the user is logged in and set the correct
  34. * template to handle both the cases
  35. * a logged in user cannot access auth page again
  36. * a non logged in user can only access the auth page (temporary)
  37. * @todo add other pages that non loggedin user can access.
  38. */
  39. protected function auth_filter() {
  40. $logged_in = Auth::instance()->logged_in();
  41. $controller = $this->request->controller();
  42. $action = $this->request->action();
  43. if (!$logged_in && $controller !== 'auth') {
  44. $this->request->redirect('auth');
  45. }
  46. if ($logged_in && $controller === 'auth' && $action !== 'logout') {
  47. $this->request->redirect('home');
  48. }
  49. }
  50. /**
  51. * Check if the role of the current user is allowed to access this page
  52. * otherwise redirect to the access denied page.
  53. * first we check if user has permission on whole using has_access method
  54. * then we check if acl for current resource action combination is defined and
  55. * check for it
  56. * lastly we resolve standard action names to valid resource-action combinations
  57. * and check for them
  58. */
  59. protected function acl_filter() {
  60. $resource = $this->request->controller();
  61. $acl = Acl::instance();
  62. if (!$acl->has_access($resource)) {
  63. $this->redirect_after_filter('error/access_denied');
  64. // Request::current()->redirect('error/access_denied');
  65. }
  66. // check if current acl for current controller-action is defined in permissions
  67. $action = $this->request->action();
  68. $repr_key = Acl::repr_key($resource, $action);
  69. if ($acl->acl_exists($repr_key) && !$acl->is_allowed($repr_key)) {
  70. $this->redirect_after_filter('error/access_denied');
  71. // Request::current()->redirect('error/access_denied');
  72. }
  73. // check for standard action names
  74. $std_actions = array(
  75. 'index' => 'view',
  76. 'add' => 'create',
  77. 'edit' => 'edit',
  78. 'delete' => 'delete',
  79. );
  80. if (isset($std_actions[$action]) && !$acl->is_allowed(Acl::repr_key($resource, $std_actions[$action]))) {
  81. $this->redirect_after_filter('error/access_denied');
  82. // Request::current()->redirect('error/access_denied');
  83. }
  84. // if it reaches here, we assume the user has permission to this resource-level
  85. // any other checking will have to be done in the controller action
  86. }
  87. /**
  88. * Method to do the correct thing to deny access to the user to the requested
  89. * resource as per the permissions.
  90. * Depending upon the type of request, it will decide the mode of notifying the user
  91. * if action type = ajax - show overlay view
  92. * if action type = normal - redirect to the access denied page
  93. */
  94. protected function redirect_after_filter($page) {
  95. if ($this->request->is_ajax()) {
  96. echo json_encode(array('success' => 0, 'reason' => 'access_denied'));
  97. exit;
  98. } else {
  99. Request::current()->redirect($page);
  100. }
  101. }
  102. /**
  103. * Method to decide and set the template that will be used.
  104. * The decision will be taken depending upon whether the user is logged in
  105. * or not
  106. */
  107. protected function template_filter() {
  108. $logged_in = Auth::instance()->logged_in();
  109. $this->template = !$logged_in ? 'template/template' : 'template/logged_template';
  110. }
  111. public function after() {
  112. $controller = $this->request->controller();
  113. $action = $this->request->action();
  114. $page_description = Kohana::message('page_title', $controller.'_'.$action.'.description');
  115. $page_title = Kohana::message('page_title', $controller.'_'.$action.'.title');
  116. $breadcrumbs = Breadcrumbs::render();
  117. $this->content = str_replace('replace_here_page_description', $page_description, $this->content);
  118. $this->content = str_replace('replace_here_page_title', $page_title, $this->content);
  119. if ($this->request->is_ajax() || !$this->request->is_initial()) {
  120. $this->response->body($this->content);
  121. } else {
  122. $title = 'Kode Learn';
  123. $styles = array(
  124. 'media/css/reset.css' => 'screen',
  125. 'media/css/components.css' => 'screen',
  126. 'media/css/kodelearn.css' => 'screen',
  127. 'media/css/jquery-ui-1.8.14.custom.css' => 'screen'
  128. );
  129. $scripts = array(
  130. 'media/javascript/jquery-1.6.2.min.js',
  131. 'media/javascript/common.js',
  132. 'media/javascript/classes.js',
  133. 'media/javascript/events.js',
  134. 'media/javascript/ajaxupload.js',
  135. 'media/javascript/jquery-ui-1.8.14.custom.min.js',
  136. 'media/javascript/jquery-ui-timepicker-addon.js',
  137. 'media/javascript/kodelearnUI.js'
  138. );
  139. $this->view->set('content', $this->content);
  140. $this->view->set('styles', $styles);
  141. $this->view->set('scripts', $scripts);
  142. $this->view->set('title', $title . ' - ' . $page_title);
  143. $this->view->set('breadcrumbs', $breadcrumbs);
  144. $this->menu_init();
  145. $this->response->body($this->view);
  146. }
  147. }
  148. protected function menu_init() {
  149. $this->view->bind('topmenu', $topmenu)
  150. ->bind('sidemenu', $sidemenu)
  151. ->bind('myaccount', $myaccount)
  152. ->bind('image', $image)
  153. ->bind('role', $role)
  154. ->bind('username', $username)
  155. ->bind('user', $user);
  156. if (!Auth::instance()->logged_in()) {
  157. $role = 'guest';
  158. } else {
  159. $user = Auth::instance()->get_user();
  160. $role = $user->role()->name;
  161. $username = Auth::instance()->get_user()->firstname;
  162. if ($user->is_role('student')) {
  163. $avatar = Auth::instance()->get_user()->avatar;
  164. $avatar = $avatar === null ? '' : $avatar;
  165. $this->view->set('avatar', CacheImage::instance()->resize($avatar, 72, 72));
  166. }
  167. }
  168. $menu = Acl_Menu::factory($role);
  169. // var_dump($menu); exit;
  170. $topmenu = $menu->get('topmenu');
  171. $sidemenu = $menu->get('sidemenu');
  172. $myaccount = $menu->get('myaccount');
  173. $institution = ORM::factory('institution', $id=1);
  174. $image = CacheImage::instance()->resize($institution->logo, 240, 60);
  175. }
  176. }