4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ACL メモ

Posted at

メモ書き

認証(Auth)とACL(Access Controll List)がかなり絡んでるので混乱してきたのでメモ書き

書いてる最中も頭爆発しそうなのと、半分混乱しながらなので当てにはしないでください。

CakePHP2.4.10の話です。

気が向いたら、覚えていたら、書き込んでいく形。

ACL の aco, aro

  1. aco = Access Control Object
  2. aro = Access Request Object

ACO によりアクセスコントロールの許可/不許可判断をして、 ARO でアクセスの許可/不許可の表を作っている形。

認証と深く関わっているため、AuthComponentから利用する形になっている。

ACLの設定とか

core.php
	Configure::write('Acl.classname', 'PhpAcl');
	// Configure::write('Acl.database', 'default');

Acl.classname: Acl を使う時に利用するクラス名。 DbAcl,IniAcl,PhpAclもしくは独自作成のAclクラス名。
Acl.database: Acl.classname で指定したクラスがDBを使う時に設定する。使わない時は、空白文字を入れるのではなく、意図的にコメントアウトした方がいい

Auth の使い方

認証周り。

app/Controller/AppController.php
class AppController extends Controller {
    public $components = [
        'Acl',
        'Session',
        'Auth' => [
            'loginAction'    => ['controller' => 'users', 'action' => 'login'],
            'logoutRedirect' => ['controller' => 'users', 'action' => 'login'],
            'loginRedirect'  => ['controller' => 'posts', 'action' => 'add'],
             // Acl で必要な設定
            'authorize' => [
                'Actions' => ['actionPath' => 'controllers']
            ]
        ],
    ];
    public $helpers = ['Html', 'Form', 'Session'];

/**
 * Action が呼び出される前に実行される。
 */
public function beforeFilter() {
    parent::beforeFilter();

    // For CakePHP 2.0
    $this->Auth->allow('*');

    // For CakePHP 2.1 and up
    $this->Auth->allow();
}
}

AuthComponent

Auth.authorize の値により、ACLの関わり方が変わってくる。

app/Controller/AppController.php
class AppController extends Controller {
    public $components = [
        'Auth' => [
                // Acl で必要な設定
            'authorize' => [
                'Actions' => ['actionPath' => 'controllers']
            ],..
        ],
    ];
}

認証を終えた後、それぞれのコントローラアクションにて、authorizeの値により認証後の認可が変わる。

Auth.authorizeが取りうる値

  1. Actions : ACLComponentを使ってAction単位でチェックする。対応表はDBのを利用するっぽい?
  2. Crud : Action と CRUDの表からチェックをする。コントローラー毎に設定する形になるっぽい。
  3. Controller : isAuthorized($user)メソッドをControllerに投げて、返り値をみてチェックする

Auth.authorize の独自拡張

LDAPなどを使用したい場合は下記のように、クラスを拡張する必要がある。

app/Controller/Component/Auth/LdapAuthorize.php
class LdapAuthorize extends BaseAuthorize {
    public function authorize($user, CakeRequest $request) {
        // Do things for ldap here.
    }
}

false を返せばアクセスの認可を拒否できる。そして、下記のように追加してやれば、独自拡張したクラスを使用することができる。

app/Controller/AppController.php
class AppController extends Controller {
    public $components = [
        'Auth' => [
                // Acl で必要な設定
            'authorize' => [
                'Ldap',
            ],..
        ],
    ];
}

CrudAuthorize の使い方

コントローラー毎に設定する必要がある。

多分こんな感じ。

検証してないので要注意

app/Controller/AppController.php
class AppController extends Controller {
    public $components = [
        'Auth' => [
                // Acl で必要な設定
            'authorize' => [
                'Crud',
            ],..
        ],
    ];
}

mapActionの引数にある、keyはCRUD permissionsにあたり、値はアクションになる。

app/Controller/HogeController.php
class AppController extends Controller {
    public function beforeFilter () {
        parent::beforeFilter();

        $this->Auth->mapAction([
            'create' => ['register'],
            'view'   => ['show', 'display']
        ]);
    }
}

参考

  1. http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html
  2. http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization
  3. http://blog.milds.net/2012/11/cakephp.html
  4. http://hampom.wordpress.com/2012/11/24/cakephp%E3%81%AEacl%E3%82%92phpphpacl%E3%81%AB%E3%81%97%E3%81%A6%E7%AE%A1%E7%90%86%E3%81%8C%E3%83%A9%E3%82%AF%E3%81%AB%E3%81%AA%E3%81%A3%E3%81%9F%E8%A9%B1/
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?