'admin' will only allow users with the role admin to access action_adminpanel * 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel * * @var array actions that require a valid user */ protected $secure_actions = array( 'menu' => TRUE, ); public function __construct(Request $request,Response $response) { // Our Menu's can run without method authentication by default. if (! isset($this->secure_actions['menu'])) $this->secure_actions['menu'] = FALSE; return parent::__construct($request,$response); } /** * Check and see if this controller needs authentication * * if $this->auth_required is TRUE, then the user must be logged in only. * if $this->auth_required is FALSE, AND $this->secure_actions has an array of * methods set to TRUE, then the user must be logged in AND a member of the * role. * * @return boolean */ protected function _auth_required() { // If our global configurable is disabled, then continue if (! Kohana::$config->load('config')->method_security) return FALSE; return (($this->auth_required !== FALSE && Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__) === FALSE) || (is_array($this->secure_actions) && array_key_exists($this->request->action(),$this->secure_actions) && Auth::instance()->logged_in($this->secure_actions[$this->request->action()],get_class($this).'|'.__METHOD__) === FALSE)); } /** * Loads the template [View] object. * * Page information is provided by [meta]. * @uses meta */ public function before() { // Do not template media files if ($this->request->action() === 'media') { $this->auto_render = FALSE; return; } // Actions that start with ajax, should only be ajax if (! Kohana::$config->load('debug')->ajax AND preg_match('/^ajax/',Request::current()->action()) AND ! Request::current()->is_ajax()) die(); parent::before(); // Check user auth and role if ($this->_auth_required()) { // If auth is required and the user is logged in, then they dont have access. // (We have already checked authorisation.) if (Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__)) { if (Config::sitemode() == Kohana::DEVELOPMENT) SystemMessage::add(array( 'title'=>_('Insufficient Access'), 'type'=>'debug', 'body'=>Debug::vars(array('required'=>$this->auth_required,'action'=>$this->request->action(),'user'=>Auth::instance()->get_user()->username)), )); // @todo Login No Access redirects are not handled in JS? if ($this->request->is_ajax()) { echo _('You dont have enough permissions.'); die(); } else HTTP::redirect('login/noaccess'); } else { Session::instance()->set('afterlogin',Request::detect_uri()); HTTP::redirect($this->noauth_redirect); } } // For AJAX calls, we dont need to render the complete page. if ($this->request->is_ajax()) { $this->auto_render = FALSE; return; } // Bind our template meta variable $this->meta = new Meta; View::bind_global('meta',$this->meta); // Our default style sheet Style::add(array( 'type'=>'file', 'data'=>'css/default.css', )); // Our default script(s) foreach (array('file'=>array_reverse(array( 'js/jquery-1.6.4.min.js', 'js/jquery.jstree-1.0rc3.js', 'js/jquery.cookie.js', ))) as $type => $datas) { foreach ($datas as $data) { Script::add(array( 'type'=>$type, 'data'=>$data, ),TRUE); } } // Initialise our content $this->template->left = ''; $this->template->content = ''; $this->template->right = ''; } public function after() { if (! is_string($this->template) AND empty($this->template->content)) $this->template->content = Block::factory(); if ($this->auto_render) { // Application Title $this->meta->title = Kohana::$config->load('config')->appname; $this->template->title = ''; // Style Sheets Properties $this->meta->styles = Style::factory(); // Script Properties $this->meta->scripts = Script::factory(); // Application logo $this->template->logo = Config::logo(); // Link images on the header line $this->template->headimages = $this->_headimages(); // Control Line $this->template->control = $this->_control(); // System Messages line $this->template->sysmsg = $this->_sysmsg(); // Left Item $this->template->left = $this->_left(); // Right Item $this->template->right = $this->_right(); // Footer $this->template->footer = $this->_footer(); // For any ajax rendered actions, we'll need to capture the content and put it in the response } elseif ($this->request->is_ajax() && isset($this->template->content) && ! $this->response->body()) { // @todo move this formatting to a view? if ($s = $this->_sysmsg() AND (string)$s) $this->response->body(sprintf('
%s
',$s)); // In case there any style sheets for this render. $this->response->bodyadd(Style::factory()); // Since we are ajax, we should re-render the breadcrumb Session::instance()->set('breadcrumb',(string)Breadcrumb::factory()); $this->response->bodyadd(Script::add(array('type'=>'stdin','data'=>'$().ready($("#ajCONTROL").load("'.URL::site('welcome/breadcrumb').'",null,function(x,s,r) {}));'))); // In case there any javascript for this render. $this->response->bodyadd(Script::factory()); // Get the response body $this->response->bodyadd(sprintf('
%s
',$this->template->content)); } parent::after(); // Generate and check the ETag for this file if (Kohana::$environment === Kohana::PRODUCTION OR Kohana::$config->load('debug')->etag) $this->check_cache(sha1($this->response->body())); } /** * Default Method to call from the tree menu */ public function action_menu() { $this->template->content = _('Please choose from the menu on the left - you may need to expand the items by pressing on the plus.'); } protected function _headimages() { HeadImages::add(array( 'url'=>'http://dev.leenooks.net', 'img'=>'img/forum-big.png', 'attrs'=>array('onclick'=>"target='_blank';",'title'=>'Link') )); return HeadImages::factory(); } /** * Render our control menu bar */ protected function _control() { return Breadcrumb::factory(); } protected function _sysmsg() { return SystemMessage::factory(); } protected function _left() { return empty($this->template->left) ? Controller_Tree::js() : $this->template->left; } protected function _right() { return empty($this->template->right) ? '' : $this->template->right; } public function _footer() { return sprintf('© %s',Config::SiteName()); } } ?>