「アクセス権限」カテゴリーアーカイブ

パスワードを暗号化させない

http://www.maruhisa.org/2011/11/30/cakephp2-0using_plainpassword/
http://www.matomater.com/687/

[php]
/lib/Cake/Utility/Security.php 107行目あたり

public static function hash($string, $type = null, $salt = false) {

return $string;
/*if (empty($type)) {
$type = self::$hashType;
}
$type = strtolower($type);

if ($type === ‘blowfish’) {
return self::_crypt($string, $salt);
}
if ($salt) {
if (!is_string($salt)) {
$salt = Configure::read(‘Security.salt’);
}
$string = $salt . $string;
}

if (!$type || $type === ‘sha1’) {
if (function_exists(‘sha1’)) {
return sha1($string);
}
$type = ‘sha256’;
}

if ($type === ‘sha256’ && function_exists(‘mhash’)) {
return bin2hex(mhash(MHASH_SHA256, $string));
}

if (function_exists(‘hash’)) {
return hash($type, $string);
}
return md5($string);*/
}
[/php]

ACLを使わない簡単なアクセス制御

http://mojaie.hatenablog.jp/entry/2012/05/19/170024

bakeするときはgroupは後からカラム追加すると良いよ ※

[php]
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL,
`username` varchar(100) NOT NULL,
`group` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘1’, ‘admin’, ‘admin’, ‘adminpassword’);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘2’, ‘user’, ‘user’, ‘userpassword’);
[/php]

AppController

[php]
class AppController extends Controller {
public $helpers = array(
‘Html’,
‘Form’,
‘Session’,
);
public $components = array(
‘Session’,
‘Auth’ => array(

),
);
public function beforeFilter() {
$this->Auth->authorize = array(‘Controller’); //これを使うときはコントローラにbeforeFilterをからでも書くこと
$this->Auth->loginRedirect = ‘/’;
$this->Auth->logoutRedirect = ‘/’;
if ($this->Auth->loggedIn()) {
$this->set(‘authUser’, $this->Auth->user());
}
}
}
[/php]

UsersController

[php]
public function beforeFilter() {
}
/**
* login method
*/
public function login() {
$this->layout = ”;
/** POST送信時にログイン認証処理を行う */
if ($this->request->is(‘post’)) {
/** ログイン認証処理 */
if ($this->Auth->login()) {
/** 認証後のリダイレクト処理 */
$this->redirectlogin();
} else {
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}else{
/** ログイン認証済の場合、顧客画面へ遷移 */
if ($this->Auth->login()) {
$this->redirectlogin();
}
}
}

/**
* redirectlogin method
*/
public function redirectlogin() {
try {
/** 顧客一覧ページのURLを格納 */
$redirectUrl = $this->Auth->redirect();
/** 顧客一覧ページにリダイレクト */
$this->redirect($redirectUrl);
} catch (Exception $e) {
/** エラー(ログアウト) */
$this->Auth->logout();
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}

/**
* logout method
*/
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
[/php]

権限をつけたいコントローラーに書くよ 

※これを書くと基本adminしかアクセスできなくなる

[php]
public function beforeFilter(){
//openaccessには誰でもアクセスできる
$this->Auth->allowedActions = array(‘openaccess’);
//親クラス(AppController)読み込み
parent::beforeFilter();
}

public function isAuthorized() {
if ($this->Auth->user(‘group’) == ‘admin’) {
//admin権限を持つユーザは全てのページにアクセスできる
return true;
} elseif ($this->Auth->user(‘group’) == ‘user’) {
if ($this->action == ‘useronly’) {
//user権限を持つユーザはuseronlyにアクセスできる
return true;
}
}
return false;
}

//誰でもアクセスできる
public function openaccess() {
}

//会員だけアクセスできる
public function useronly() {

}

//管理者だけアクセスできる
public function adminonly() {
}
[/php]

openaccess.ctpは誰でもアクセスできる

useronly.ctpは会員だけアクセスできる

adminonly.ctpは会員だけアクセスできる

Authで暗号化してたらログインできなかったので、暗号化解除したよ

http://aman.sakura.ne.jp/?p=782

cakephp adminルーティング

adminルーティング

参考:http://blog.dicecream.net/2012/04/admin.php

/cake/app/Config/core.php

[php]
Configure::write(‘Routing.prefixes’, array(‘admin’)); //コメントを外す
[/php]

管理者用としても使いたいコントローラーのアクション名にadmin_をつける

UsersControllerのaddアクションを管理者用にしたいのであれば、

[php]
public function admin_add() {
// 処理
}
[/php]

view用のファイル(admin_add.ctp)を用意します。

http://www.example.com/admin/users/addが管理者用のURLになる

特定のページだけは認証なしで見えるようにする

参考:http://www.misty540.net/wp/article_187.html

pp_controller:beforeFilter  内で allow メソッドを使います。

[php]
// homeと test に誰でもアクセス可能
$this->Auth->allow(array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘home’));
$this->Auth->allow(array(‘controller’ => ‘pages’, ‘action’ => ‘display’, ‘test’));
[/php]

ACLでアクセス管理「CakePHP2.2」

ACLのデータベーステーブルの初期化

[text]./app/Console/cake schema create DbAcl[/text]

ACOデータの作成

[text]./app/Console/cake acl create aco root controllers[/text]

参考: