*

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

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

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

関連記事

no image

Gitに挑んだりしてみる

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

記事を読む

no image

WEBは入り口

スクール革命っていう、日曜の昼に放送してる番組見てたら、「近いうちに夢を録画できるようになる

記事を読む

no image

wordpress plugin change the taxonomy when you suggest the time

1. at first, download Post Expirator.2.change code

記事を読む

キャプチャソフト

capture STAFF lite 何かいいキャプチャソフトないか?と探したところ出てきたソフト。

記事を読む

no image

ホームページ(笑)を作ればモノが売れるという幻想(笑)

まだこんな考えもってる人がいたのかwww もう「ほーむぺーじ」って言うのもやめて欲

記事を読む

no image

アマゾンのクラウド(AWS)を使ってみる

前々からやろうやろうと思っていて手を着けられていなかったサービス、アマゾンウェブサービス(AWS

記事を読む

no image

Blogger Syntax Highlighter

今さらですが、Blogger移転を機にSyntax Highlighterを入れてみました。こちらの

記事を読む

CakePHPでFacebook-PHP-SDKを使ってFacebookログインするアプリの作り方

こんにちは! 昨日は東京では45年ぶりに大雪が降りましたね! 自分の家の方でも雪が2

記事を読む

wordpressのパーマリンク設定を変更してみたら大丈夫だった件

wordpressでパーマリンクを変えようと思ってたんですが、なかなか踏み切るタイミングがな

記事を読む

no image

EC-cube+MYSQL < EC-cube+PostgreSQL

http://sasapurin.doorblog.jp/archives/cat_50047102

記事を読む

Google AD

Google AD

PAGE TOP ↑