cakephp2.4 ACL 試してみた
参考サイト
- Cookbook 2.x
http://book.cakephp.org/2.0/ja/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html - CakePHP2で簡単にACLを使う方法
http://blog.asial.co.jp/1088 - CakePHP-2.3.8 に Plugin–Acl を導入する
http://iwave-fe.blogspot.jp/2013/07/cakephp-238-pluginacl.html
流れ
Cookbookとは違いGUIのACL(アクセス制御リスト)プラグインを使います。
1) DBの用意
CREATE TABLE users ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) NOT NULL UNIQUE, password CHAR(40) NOT NULL, group_id INT(11) NOT NULL, created DATETIME, modified DATETIME ); CREATE TABLE groups ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, created DATETIME, modified DATETIME ); CREATE TABLE posts ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id INT(11) NOT NULL, title VARCHAR(255) NOT NULL, body TEXT, created DATETIME, modified DATETIME ); CREATE TABLE widgets ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, part_no VARCHAR(12), quantity INT(11) );
$app/Console/cake.php schema create DbAcl
2) プラグインの準備
http://www.alaxos.net/blaxos/pages/view/download_plugin_acl_2.0
Download ACL Plugin 2.2.0
古いみたいだけど一応動いた
解凍後 app/Plugin/Acl/
3) BAKE ALL
$app/Console/cake bake all
4) M/V/C修正
モデル
User.php
public $actsAs = array('Acl' => array('type' => 'requester')); public function parentNode() { if (!$this->id && empty($this->data)) { return null; } if (isset($this->data['User']['group_id'])) { $groupId = $this->data['User']['group_id']; } else { $groupId = $this->field('group_id'); } if (!$groupId) { return null; } else { return array('Group' => array('id' => $groupId)); } } public function beforeSave($options = array()) { $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); return true; } public function bindNode($user) { return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']); }
Group.php
public $actsAs = array('Acl' => array('type' => 'requester')); public function parentNode() { return null; }
コントローラー
AppController.php
public $components = array( 'Acl', 'Auth' => array( 'authorize' => array( 'Actions' => array('actionPath' => 'controllers') ) ), 'Session', 'DebugKit.Toolbar' ); public $helpers = array('Html', 'Form', 'Session'); public function beforeFilter() { //Configure AuthComponent $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); $this->Auth->loginRedirect = array('controller' => 'topics', 'action' => 'add'); }
UsersController.php
public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow(); } public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { $this->Session->setFlash('You are logged in!'); $this->redirect($this->Auth->redirect()); } else { $this->Session->setFlash('Your username or password was incorrect.'); } } } public function logout() { //Leave empty for now. }
GroupsController.php
/Plugin/Acl/Controller/AclController.php
/Plugin/Acl/Controller/AcosController.php
/Plugin/Acl/Controller/ArosController.php
public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow(); }
ビュー
Users/login.ctp
<?php echo $this->Form->create('User', array('action' => 'login')); echo $this->Form->inputs(array( 'legend' => __('Login'), 'username', 'password' )); echo $this->Form->end('Login');
5) グループ作成、ユーザー作成
/users/group にアクセスしグループ追加
administrators, managers, users
/users/add にアクセスしユーザー追加
6) bootstrap.phpの修正
/admin/acl/にアクセスするとエラーがでる。
プラグインの方のbootstrap.phpを使うと反映されない
Config/bootstrap.php
#14 Groupに変更 Configure :: write('acl.aro.role.model', 'Group'); #21 id を 追記 Configure :: write('acl.aro.role.primary_key', 'id'); #40 id を 追記 Configure :: write('acl.aro.user.primary_key', 'id');