CakePHP Plugin ACL を利用する - 1 | RIDE WARP Ver2.0

CakePHP Plugin ACL を利用する - 1


$RIDE WARP Ver2.0

http://www.alaxos.net/blaxos/pages/view/download_plugin_acl


アクセス制御をしたいと思い探したらありました。

使った感じはとてもわかりやすく操作性が最高に良いです。


AJAXで通信しているようでグループごと、ユーザーごとにページ、アクションが細かく制御できます。


制御盤みたいな感じでON/OFFをサクッと切替えれるのが良いですね。


これを使うためにはAUTHとACLの土台が出来ていないとだめです。

誰にでも出来る手順を作りました。何回も手順を確認したんで間違いないと思います。



まずはCakePHPでAuthコンポーネントを組み込み、ログイン機能を作る。

・ログインしたらユーザー一覧が閲覧できる。

・未ログインならユーザー一覧が閲覧できない。

まずはこれだけ。


1.環境

・CakePHP1.3.11

・PHP5.3.6


2.DBの作成


このまま貼り付けて実行。(俺はInnoDBになってるよ、使いたいほうで。)↓
CREATE TABLE IF NOT EXISTS `acos` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(10) DEFAULT NULL,
`model` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',
`foreign_key` int(10) unsigned DEFAULT NULL,
`alias` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',
`lft` int(10) DEFAULT NULL,
`rght` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
--
-- テーブルの構造 `aros`
--
CREATE TABLE IF NOT EXISTS `aros` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(10) DEFAULT NULL,
`model` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',
`foreign_key` int(10) unsigned DEFAULT NULL,
`alias` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',
`lft` int(10) DEFAULT NULL,
`rght` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
--
-- テーブルの構造 `aros_acos`
--
CREATE TABLE IF NOT EXISTS `aros_acos` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`aro_id` int(10) unsigned NOT NULL,
`aco_id` int(10) unsigned NOT NULL,
`_create` char(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`_read` char(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`_update` char(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`_delete` char(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
--
-- テーブルの構造 `cake_sessions`
--
CREATE TABLE IF NOT EXISTS `cake_sessions` (
`id` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`data` text COLLATE utf8_unicode_ci NOT NULL,
`expires` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- テーブルの構造 `groups`
--
CREATE TABLE IF NOT EXISTS `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
--
-- テーブルの構造 `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` char(40) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(11) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

3.AppControllerの作成


app/controllers/app_controller.php

<?php
class AppController extends Controller {
public $components = array('Acl','Session','Auth','RequestHandler');
/*************************************************************************************
*Method : beforeFilter
*Process:
*************************************************************************************/
function beforeFilter() {
// AuthComponent の設定
$this->Auth->actionPath = 'controllers/';
$this->Auth->authorize = 'actions';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}
}
?>

4.UserとGroupのページを作る。


app/controllers/users_controller.php

<?php
class UsersController extends AppController {
public $name = 'Users';
/*************************************************************************************
*Method : beforeFilter
*************************************************************************************/
function beforeFilter() {
parent::beforeFilter();
//初期導入用
$this->Auth->allowedActions = array('*');
}
/*************************************************************************************
*Method : index
*************************************************************************************/
function index() {
$this->loadModel('User');
$this->set('users', $this->User->find('all'));
}
/*************************************************************************************
*Method : login
*************************************************************************************/
function login(){

}
/*************************************************************************************
*Method : logout
*************************************************************************************/
function logout(){
$this->redirect($this->Auth->logout());
}
/*************************************************************************************
*Method : add
*************************************************************************************/
function add(){
if(!empty($this->data)){
if($this->data){
$this->User->create();
$this->User->save($this->data);
$this->redirect(array('action' => 'login'));
}
}
$this->set('datas', $this->User->find('all'));
}
}
?>

app/models/user.php

<?php
class User extends AppModel{
public $name = 'User';
public $useTable = 'users';
public $belongsTo = array('Group' => array('className' => 'Group'
,'foreignKey' => 'group_id'));
public $actsAs = array('Acl' => 'requester');

function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
$data = $this->data;
if (empty($this->data)) {
$data = $this->read();
}
if (!$data['User']['group_id']) {
return null;
} else {
return array('Group' => array('id' => $data['User']['group_id']));
}
}
function afterSave($created) {
if (!$created) {
$parent = $this->parentNode();
$parent = $this->node($parent);
$node = $this->node();
$aro = $node[0];
$aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
$this->Aro->save($aro);
}
}
}
?>

app/views/users/add.ctp

<?php
echo $form->create('User', array('action' => 'add'));
echo $form->input('username',array('type' => 'text'));
echo $form->input('password',array('type' => 'text'));
echo $form->input('group_id',array('type' => 'text'));
echo $form->end('登録');
?>

app/views/users/index.ctp

<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>PassWord</th>
</tr>
</thead>
<tbody>
<?php for($i=0;$i<count($users);$i++){ ?>
<tr>
<td><?php echo $users[$i]['User']['id']; ?></td>
<td><?php echo $users[$i]['User']['username']; ?></td>
<td><?php echo $users[$i]['User']['password']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

app/views/users/login.ctp

<h2>Login</h2>
<?php
echo $form->create('User', array('url' => array('controller' => 'users', 'action' =>'login')));
echo $form->input('User.username');
echo $form->input('User.password');
echo $form->end('Login');
?>

app/views/users/logout.ctp


<?php echo $html->tag('h3', 'ログアウトしました。') ?>

app/controllers/groups_controller.php

<?php
class GroupsController extends AppController {
public $name = 'Groups';
/*************************************************************************************
*Method : beforeFilter
*************************************************************************************/
function beforeFilter() {
parent::beforeFilter();
//初期導入用
$this->Auth->allowedActions = array('*');
}
/*************************************************************************************
*Method : index
*************************************************************************************/
function index() {
$this->set('groups', $this->paginate());
}
/*************************************************************************************
*Method : add
*************************************************************************************/
function add(){
if(!empty($this->data)){
if($this->data){
$this->Group->create();
$this->Group->save($this->data);
$this->redirect(array('action' => 'add'));
}
}
}
}
?>

app/models/group.php

<?php
class Group extends AppModel{
public $name = 'Group';
public $useTable = 'groups';
public $actsAs = array('Acl' => 'requester');
public $hasMany = array('User' => array('className' => 'User'
,'foreignKey' => 'group_id'
,'dependent' => false
,'conditions' => ''
,'fields' => ''
,'order' => ''
,'limit' => ''
,'offset' => ''
,'exclusive' => ''
,'finderQuery' => ''
,'counterQuery' => ''));
function parentNode() {
return null;
}
}
?>

app/views/groups/add.ctp

<?php
echo $form->create('Group', array('action' => 'add'));
echo $form->input('name');
echo $form->end('login');
?>

app/views/groups/index.ctp

<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php for($i=0;$i<count($groups);$i++){ ?>
<tr>
<td><?php echo $groups[$i]['Group']['id']; ?></td>
<td><?php echo $groups[$i]['Group']['name']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

まだ登録はするな!


2に続く