Created
November 27, 2009 12:32
-
-
Save nojimage/243983 to your computer and use it in GitHub Desktop.
cakephp authコンポーネントのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ACL | |
* | |
* PHP version 5 | |
* | |
* @copyright Copyright 2009, nojimage | |
* @link http://php-tips.com/ | |
* @package app | |
* @subpackage app.config | |
* @version 1.0 | |
* @modifiedby nojimage | |
* | |
*/ | |
// -- AUTH groups | |
/** | |
* 管理者ユーザのグループ名 | |
* @var string | |
*/ | |
define('AUTH_GROUP_ADMIN', 'admin'); | |
/** | |
* クライアントユーザーのグループ名 | |
* @var string | |
*/ | |
define('AUTH_GROUP_CLIENT', 'client'); | |
/** | |
* 一般ユーザーのグループ名 | |
* @var string | |
*/ | |
define('AUTH_GROUP_NORMAL', 'normal'); | |
class ACL | |
{ | |
/** | |
* 全てのユーザグループを許可 | |
* @var array | |
*/ | |
static $ACL_ALL = array('*'); | |
/** | |
* クライアント機能にアクセス可能なユーザグループ | |
* (管理者、クライアントユーザともにアクセス可能) | |
* @var array | |
*/ | |
static $ACL_CLIENT = array(AUTH_GROUP_ADMIN, AUTH_GROUP_CLIENT); | |
/** | |
* 管理者機能にアクセス可能なユーザグループ | |
* (管理者のみアクセス可能) | |
* @var array | |
*/ | |
static $ACL_ADMIN = array(AUTH_GROUP_ADMIN); | |
/** | |
* ユーザ側機能にアクセス可能なユーザグループ | |
* (一般ユーザのみアクセス可能) | |
* @var array | |
*/ | |
static $ACL_USER = array(AUTH_GROUP_NORMAL); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* AppController | |
* | |
* PHP version 5 | |
* | |
* @copyright Copyright 2009, nojimage | |
* @link http://php-tips.com/ | |
* @package app | |
* @subpackage app | |
* @version 1.0 | |
* @modifiedby nojimage | |
* | |
*/ | |
class AppController extends Controller | |
{ | |
public $components = array('Auth'); | |
/** | |
* アクションのアクセスコントロール用 | |
* array( | |
* 'action' => '*', // すべてのグループで利用可能 | |
* 'action' => array('group1', 'group2') // group1とgroup2で利用可能 | |
* ) | |
* | |
* @var array | |
*/ | |
public $permissions = array(); | |
/** | |
* @var AuthComponent | |
*/ | |
public $Auth; | |
/** | |
* | |
* @var SessionComponent | |
*/ | |
var $Session; | |
/** | |
* (non-PHPdoc) | |
* @see cake/libs/controller/Controller#beforeFilter() | |
*/ | |
function beforeFilter() | |
{ | |
// Authコンポーネントの基本設定 | |
$this->Auth->fields = array('username' => 'username', 'password' => 'password'); // 認証に使うフィールドを指定 | |
$this->Auth->loginError = __('ログインに失敗しました。IDまたはパスワードが不正です。', true); | |
$this->Auth->authError = __('閲覧権限がありません。', true); | |
$this->Auth->authorize = 'controller'; // 権限があるかどうかをAppController::isAuthorized()を使ってチェック | |
if($this->isAdmin()){ | |
// adminルーティングの場合の処理 | |
$this->layout = 'admin'; // レイアウトを変更 | |
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => 'admin'); // loginアクションを UsersController::admin_login()にする | |
$this->Auth->loginRedirect = '/admin/'; // ログイン後のデフォルト遷移先 | |
} | |
} | |
/** | |
* Check Admin routing | |
* @return boolean | |
*/ | |
function isAdmin() | |
{ | |
return Configure::read('Routing.admin') && !empty($this->params['admin']); | |
} | |
/** | |
* (non-PHPdoc) | |
* @see cake/libs/controller/Controller#isAuthorized() | |
*/ | |
function isAuthorized() | |
{ | |
// - group base auth | |
$group = $this->Auth->user('group'); | |
$action = $this->action; | |
if (!empty($this->permissions[$action])) { | |
$permission = $this->permissions[$this->action]; | |
if (is_scalar($permission)) { | |
$permission = array($permission); | |
} | |
if ($permission[0] == '*') { | |
return true; | |
} | |
if (in_array($group, $permission)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// load - acl | |
require_once('acl.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User Model | |
* | |
* PHP version 5 | |
* | |
* @copyright Copyright 2009, nojimage | |
* @link http://php-tips.com/ | |
* @package app | |
* @subpackage app.models | |
* @version 1.0 | |
* @modifiedby nojimage | |
* | |
*/ | |
class User extends AppModel { | |
var $name = 'User'; | |
var $validate = array( | |
'username' => array('notempty'), | |
'password' => array('notempty'), | |
'group_id' => array('numeric') | |
); | |
//The Associations below have been created with all possible keys, those that are not needed can be removed | |
var $belongsTo = array( | |
'Group' => array( | |
'className' => 'Group', | |
'foreignKey' => 'group_id', | |
'conditions' => '', | |
'fields' => '', | |
'order' => '')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* UsersController | |
* | |
* PHP version 5 | |
* | |
* @copyright Copyright 2009, nojimage | |
* @link http://php-tips.com/ | |
* @package app | |
* @subpackage app.controllers | |
* @version 1.0 | |
* @modifiedby nojimage | |
* | |
*/ | |
class UsersController extends AppController { | |
var $name = 'Users'; | |
var $uses = array('User'); | |
/** | |
* | |
* @var User | |
*/ | |
var $User; | |
/** | |
* (non-PHPdoc) | |
* @see cake/libs/controller/Controller#beforeFilter() | |
*/ | |
public function beforeFilter() | |
{ | |
parent::beforeFilter(); | |
// 認証なしでアクセスできるアクション | |
$this->Auth->allow('login', 'admin_login'); | |
// 表示に権限が必要なアクション | |
$this->permissions = array( | |
'admin_index' => ACL::$ACL_CLIENT, // 例えば、ユーザ一覧の表示は管理者とクライアントが可能 | |
'admin_edit' => ACL::$ACL_ADMIN // 例えば、ユーザ一覧の編集はは管理者グループのみ可能 | |
); | |
// redirectを抑制 | |
if (in_array($this->action, array('login', 'admin_login'))) { | |
$this->Auth->autoRedirect = false; | |
} | |
} | |
/** | |
* コントロールパネル クライアント一覧 | |
*/ | |
public function admin_index() | |
{ | |
// ごにょごにょ | |
} | |
/** | |
* コントロールパネル クライアント編集 | |
*/ | |
public function admin_edit() | |
{ | |
// ごにょごにょ | |
} | |
/** | |
* コントロールパネル ログイン処理 | |
*/ | |
public function admin_login() | |
{ | |
// ログイン処理は共通メソッド | |
$this->_login(); | |
} | |
/** | |
* コントロールパネル ログアウト処理 | |
*/ | |
public function admin_logout() | |
{ | |
// ログアウト処理は共通メソッド | |
$this->_logout(); | |
$this->redirect(array('action' => 'login', 'admin' => true)); | |
} | |
/** | |
* ログイン処理(一般ユーザ側) | |
*/ | |
public function login() | |
{ | |
// ログイン処理は共通メソッド | |
$this->_login(); | |
} | |
/** | |
* ログアウト処理(一般ユーザ側) | |
*/ | |
public function logout() | |
{ | |
$this->_logout(); | |
$this->redirect(array('action' => 'login')); | |
} | |
/** | |
* ログイン処理(共通) | |
*/ | |
protected function _login() | |
{ | |
// ログインしていれば | |
if ($this->Auth->user()) { | |
// グループを取得 | |
$group = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'recursive' => 0)); | |
// Authセッションにグループ名を追加 | |
$this->Session->write($this->Auth->sessionKey . '.group', $group['Group']['name']); | |
// 画面を遷移 | |
$this->redirect($this->Auth->redirect()); | |
} | |
} | |
/** | |
* ログアウト処理(共通) | |
*/ | |
protected function _logout() | |
{ | |
$this->Auth->logout(); | |
$this->Session->del('Auth.redirect'); | |
$this->Session->setFlash(__('セッションを終了しました。', true), null, null, 'auth'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment