*

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

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

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

記事を読む

no image

紛糾!システム構築の巻

システム作る時って、会社の縮図だと思う。いろいろな立場の人の思惑や損得が入り混じり、考えが違うために

記事を読む

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

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

記事を読む

no image

WordPressアプリ

iPhoneのwordpressアプリ、さっきダウンロードして今テスト。ほとんどの機能が付いてて、い

記事を読む

no image

「おっ」と思った記事

前回に引き続き、「おっ」と思ったサイトを紹介します。今回はデザインうんぬんでなく、コンテンツの方で。

記事を読む

no image

ワードプレスのプラグイン初作成

タイトルのとおり。ワードプレスのプラグインを作ってみました。仕事でEC-CUBE使って販売をしている

記事を読む

ipad2買ったら、コレは買っとけ的なもの その2

前回に引き続き、買ってよかったipadアプリです。i文庫HD以前PDF化した本を読むのにデフォルトの

記事を読む

CakePHP1.3でブラウザを閉じてもセッションが切れないようにする方法

ちょっと体調を崩してしまってましたが、なんとか回復してきました。(辛かった・・・) 本当に

記事を読む

andoさん写真

「オープンソースカンファレンス2012」でcandycaneのセミナー聞いてきた

 オープンソースカンファレンス2012に行ってきました。 オープンソースカンファレンス20

記事を読む

no image

今頃知りました。php

http://www.php.net/manual/ja/oop5.intro.phpabstrac

記事を読む

Google AD

Google AD

PAGE TOP ↑