「Authモジュールによる認証[kohana 3.x]」で認証を行う部分を作成しました。
ここでは、認証した後の処理を見てみます。この例では認証を必要とするかどうか?やアクション単位での制限を行うかどうか?といった設定が行えます。
Controller_Templateクラスを継承して認証状態を確認する処理を追加したコントローラーを作成します。
<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Template_default extends Controller_Template { public $template = 'templates'; // 認証を必要とするかどうか?また、どのロールが? public $auth_required = FALSE; // アクション毎の制御を行うか?また、どのアクションに対してどのロールが? public $secure_actions = FALSE; public function before() { parent::before(); // セッションの作成 $this->session= Session::instance(); $action_name = Request::instance()->action; if (($this->auth_required !== FALSE && Auth::instance()->logged_in($this->auth_required) === FALSE) || (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions) && Auth::instance()->logged_in($this->secure_actions[$action_name]) === FALSE)) { if (Auth::instance()->logged_in()){ Request::instance()->redirect('account/noaccess'); }else{ Request::instance()->redirect('account/signin'); } } if($this->auto_render) { // Initialize $this->template->meta_keywords = ''; $this->template->meta_description = ''; $this->template->meta_copywrite = ''; $this->template->title = ''; $this->template->content = ''; $this->template->styles = array(); $this->template->scripts = array(); } } public function after() { if($this->auto_render) { } parent::after(); } }
上記、Controller_Template_defaultを継承して実際に制限をしてみます。
<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Admin extends Controller_Template_default { // adminロールがある場合のみ実行可能とする public $auth_required = array('admin'); function action_index(){ $this->template->content = View::factory('admincontents'); } }
この例では、adminロールが設定されたユーザーのみ実行可能となります。ロールが設定されて無い場合、 account/noaccessへリダイレクトされます。
次にアクション別に制限をかけてみます。
<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Admin extends Controller_Template_default { public $secure_actions = array('post' => array('login','admin'), 'edit' => array('login','admin'), 'delete' => array('login','admin')); function action_index() { $this->template->content = View::factory('infomation'); } function action_post() { $this->template->content = View::factory('postarticle'); } function action_edit() { $this->template->content = View::factory('editarticle'); } function action_delete() { $this->template->content = View::factory('deletearticle'); } }
Copyright 1997-2010 BBB All rights reserved.