*

cakephp2にACLプラグインを入れてみた

公開日: : 最終更新日:2014/10/03 WEBサービス, システム

CakePHP-2.3.8-Screenshot007_thumb[2]

cakephpで、ユーザーによって権限やアクセスを変えたりする方法を調べてたら、それ用のプラグインがあったのでトライ。

前提:
・プラグインはこちらからダウンロード(2.2.0)
http://www.alaxos.ch/blaxos/pages/view/download_plugin_acl_2.0
・cakeのバージョンは2.3.8
・postsという入力ができるWEBアプリ

1◆cakephpセットアップ

まずは通常通りCakePHPをセットアップするために、以下のファイルを編集します。
databaseへの接続設定と、coreファイルでセキュリティ系の設定をします。

database.php
core.php

2◆プラグイン設置

ダウンロードしたプラグインを、下記のディレクトリに設置します。

app/plugin/Acl

3◆adminルーティングを有効にする

ルーティングをadminに設定
app/Config/core.php

Configure::write('Routing.prefixes', array('admin'));

プラグインと設定の読み込み
app/Config/bootstrap.php

CakePlugin::load('Acl', array('bootstrap' => true));

4◆テーブル作成

ここまでできたら、テーブルを作成していきます。

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
);

5◆bake

DBができたらcake でbakeします。

cd web/cake/app
chmod o+x Console/cake.php
php Console/cake.php bake all

6◆ACLテーブル作成

php Console/cake.php schema create DbAcl

7◆app/Model/User.php

App::uses('AuthComponent', 'Controller/Component');

//class User extends AppModel { 内に以下を追記:

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;
}

8◆app/Model/Group.php

//class Group extends AppModel { 内に以下を追記:
public $actsAs = array('Acl' => array('type' => 'requester'));

public function parentNode(){
return null;
}

9◆app/View/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');
?>

10◆app/Controller/UsersController.php

//class UsersController extends AppController { 内に以下を追記:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow();
}

public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Your username or password miss mach.');
}
}
}
public function logout(){
//empty
}

11◆app/Controller/GroupsController.php

//class GroupsController extends AppController { 内に以下を追記:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow();
}

12◆app/Controller/AppController.php

//class AppController extends Controller { 内に以下を追記:
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'class' => 'alert-error'
)
)
),
'Session',
);

public $helpers = array(
'Session',
'Html' ,
'Form' 
);

public function beforeFilter() {
$this->Auth->loginAction = array('controller' => 'users','action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'posts','action' => 'index');
}

13◆グループ、ユーザを登録

/cake/groups/add にアクセス

例として以下を登録
グループ: administrators, managers, users
ユーザ: adminuser, manageuser, useruser

※上記のようにAROが追加できたら、以下の認証のコード(before fillter)は外す
app/Controller/GroupsController.php
app/Controller/UsersController.php

14◆app/Plugin/Acl/Config/bootstrap.php

Configure :: write('acl.aro.role.model', 'Group'); //変更
Configure :: write('acl.aro.role.primary_key', 'id'); //追記
Configure :: write('acl.aro.role.foreign_key', 'group_id'); //追記
Configure :: write('acl.aro.user.primary_key', 'id'); //追記

15◆app/Plugin/Acl/Controller/AclAppController.php

//function beforeFilter() { 内に追記: 
$this->Auth->allow();

16◆app/Plugin/Acl/Controller/AclController.php

public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow(); 
}

17◆app/Plugin/Acl/Controller/AcosController.php

public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow(); 
}

18◆app/Plugin/Acl/Controller/ArosController.php

//function beforeFilter() { 内に追記: 
$this->Auth->allow();

19◆ACOを登録

/cake/admin/acl にアクセス
「Permissions」の「Roles permissions」で権限を設定

とりあえずこれでOK。

 

2013年10月31日追記:

●ログインしてなくても見られるページには、以下の記述が必要。

※この例では、 /app/Controller/PostsController.php

	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index', 'view');
	}

●acl管理画面にアクセスできてしまうので、記述が必要。

/app/Plugin/Acl/Controller/AclController.php
/app/Plugin/Acl/Controller/AclAppController.php
function before fillter { 内の以下の部分をコメントアウト

		parent::beforeFilter();
		$this->Auth->allow();

 

Google AD


Message

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.

関連記事

no image

やっぱiphoneはすごかった

  いろいろ迷って2台持ちを決意。iphone1台にすると、データ通信量がハンパない

記事を読む

no image

オブジェクトとクラスについて

基本的なところをちょっと復習。 下記あたりが参考になったのでメモ。 http://www.k

記事を読む

ロリポップ

ロリポップからさくらインターネットにレンタルサーバを変更しました

前のエントリーで少し書きましたが、ロリポップを解約しました。 契約開始から3年と半年、本当

記事を読む

no image

オラクル、「OpenOffice.org」コードの寄贈を表明 – CNET Japan

オラクル、「OpenOffice.org」コードの寄贈を表明 - CNET Japanオープンの流れ

記事を読む

no image

ec-cubeで欲しい機能

仕事で、ec-cubeを触っている。どこの企業でも同じだと思うが、ec-cubeを使っているとカスタ

記事を読む

SVNリポジトリのリプレース(とeclipseの設定)

  SVNリポジトリのリプレースを行ったのでエントリー。 1.リポジトリをdump svn

記事を読む

no image

Gitに挑んだりしてみる

複数で開発するにあたって絶対必要と言ってもいい、バージョン管理ソフト。WEBSVNという選択肢も頭を

記事を読む

no image

AmazonEC2にcakephp2入れたときにエラーが出た件

  たぶんEC2にはcakephpをこれからも入れるだろうから自分用にエントリー。 Amazon

記事を読む

no image

ロリポップ+お名前.comで独自ドメイン取得

onamae.com で独自ドメイン取得しました。 そのときのメモ。 1.lollipo

記事を読む

postgresqlのスロークエリ(スローログ)を取得する

45年ぶりの雪が、1週おきに降るってどういうことでしょうか? とは言え、雪が降るといつもと

記事を読む

Google AD

Google AD

PAGE TOP ↑