*

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

wordpressからbloggerに引越し

lolipopで使っているロリポプランは、DBが一つしか使用できない。もうちょっと有効に使えないかと

記事を読む

no image

ブログ更新情報をTwitterに流す、FeedBurnerの「Socialize機能」

タイトルの通り、ブログの更新情報をTwitterに流す方法。 結論から言うとFeedBurnaer

記事を読む

【wordpress】buddypressでの子テーマの作リ方

先日BuddyPressのインストール方法をお伝えしましたが、インストールしたままテーマを変

記事を読む

no image

HDからデータを救出。

前回、PCがぶっ壊れたと書いたが、PCI・USB変換アダプタを購入し、内蔵HDからアダプタを取り付け

記事を読む

no image

Gitに挑んだりしてみる

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

記事を読む

キャプチャソフト

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

記事を読む

【wordpress:プラグイン】会員制サイトを作るのでBuddyPressをインストールしてみた

今ちょっと温めてるアイデアがあって、それを実現させるために会員制サイトを作ろうと思います。

記事を読む

ログインアカウント

cakephpでviewからログイン判定してアカウント表示する機能

cakephpのログイン機能を、viewから判定させたかったので調べてみました。 よく

記事を読む

no image

WordPressアプリ

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

記事を読む

さくらインターネットでPEAR::MDB2とServices_Amazonを入れてみた

前回の続きで、PEAR::MDB2と、Services_Amazonをさくらインターネットに

記事を読む

Google AD

Google AD

PAGE TOP ↑