LoginSignup
24
21

More than 5 years have passed since last update.

fuelPHPのormauthまわり

Last updated at Posted at 2014-07-10

====================================================================================

Ver. 1.7 想定
個人的に少しわからなかった部分を調査メモ。

生成・下準備

oil r migrate --packages=auth

でテーブルが生成される

Already on the latest migration for package:auth.

と言われてしまうとき、

oil r migrate:current --packages=auth

生成したけど一度テーブルを削除してしまった。
マイグレーションファイルをコピーして下記コマンドで再度生成。

oil refine migrate:current
oil refine migrate

ユーザー作成
oil console
Auth::create_user('hoge', 'foo', 'hoge@hoge.com', 100);

パーミッション

ormauthまわりの処理で使うのでモデルを生成しておく
ひとまず下記は生成した

oil refine fromdb:scaffold users
oil refine fromdb:scaffold users_permissions
oil refine fromdb:scaffold users_group_permissions
oil refine fromdb:scaffold users_roles
oil refine fromdb:scaffold users_role_permissions

ロールに対する権限設定をするので、users_role_permissionsを編集する
users_group_permissionsについては使い方がよくわからない。
データをinsertしてもデフォルト処理だと紐付けられなかったように思える。

下記のuser_の部分をgroup_とかにしたものを自分で記述すればつかえるのかとは思う。必要なら使う。

\$cache_key = \Config::get('ormauth.cache_prefix', 'auth').'.permissions.user_'.(\$user ? \$user_id[1] : 0);
Debug::dump(\Cache::get(\$cache_key));

ACL

パーミッションについてもうちょっと調べることに。

アクションはシリアライズの形式。

serialize(array('read'))

ID:1 ; area:backend ; permission:page ; actions:NULL
ID:2 ; area:backend ; permission:page ; actions:a:1:{i:0;s:3:"get";}

http://fuelphp.jp/docs/1.7/packages/auth/ormauth/intro.html
http://fuelphp.com/forums/discussion/12278/ormauth-example-permission-

test.php
The code in controller for check access : 

            $user_id = Auth::get_user_id();
            $user = \Auth\Model\Auth_User::find($user_id[1]);
            Debug::dump($user);

            if (Auth::has_access('backend.page.read')) {
                echo "OK !";
            } else {
                echo "No access !";
            }
            self::flush_auth();

function flush_auth() contains :

        // flush all the cached permissions
        \Cache::delete(\Config::get('ormauth.cache_prefix', 'auth').'.permissions');

        // flush all the cached groups
        \Cache::delete(\Config::get('ormauth.cache_prefix', 'auth').'.groups');

        // flush all the cached roles
        \Cache::delete(\Config::get('ormauth.cache_prefix', 'auth').'.roles');

areaとpermission,actionsを全て指定したのにうまく判別されていない。どういう解釈だ。
どうもキャッシュがなかなか消えないみたい。
下記で確認

        $cache_key = \Config::get('ormauth.cache_prefix', 'auth').'.permissions.user_'.($user ? $user->id : 0);
            list($current_rights, $revoked_rights, $global_access) = \Cache::get($cache_key);
            var_dump(array($current_rights, $revoked_rights, $global_access));

blog.title.[test] ok
blog.title.[ngact] ok
blog.title.[test,ngact] ok
blog.title[test] ng
blog.title[ngact] ng
blog.title[test,ngact] ng
blog.title.test ok
blog.title.ngact ok
blog.title.test ok
blog.test ng
blog.ngact ng
title.test ng
title.ngact ng

scopeってなに。。

ひもづけは下記

test.php
// $role_id で識別されるロールを取得
$role = \Model\Auth_Role::find(4);

// $perm_id で識別されるパーミッションを取得
$perm = \Model\Auth_Permission::find(1);

// アクションのサブセレクションを追加し 2 つを関連付け
$role->rolepermission[] = \Model\Auth_Rolepermission::forge(array(
    'role_id' => $role->id,
    'perms_id' => $perm->id,
    'actions' => array(1),
));
// そして、関連付けを保存
$role->save();

たぶんうまくいった。まだ多少怪しいけど。書き方には注意が必要だな。
「.」は上位が許可されていれば下位も許可されるって解釈の文法なのだな。
他の注意としては、文字列「test,titleとか、、」によってはだめだったかも。いや、そんなはずはないか。。気のせいかな。
キャッシュにはかなり注意。(権限を更新したときには、当面キャッシュを除去する処理をいれるとか、ログイン時には毎回キャッシュを除去するようにとか。もうされてるかな。)

mainpage.permtitle.[read] 1 ok 
mainpage.permtitle.[write] 1 ok 
mainpage.permtitle.[read,write] 1 ok 
mainpage.permtitle[read] 1 ok 
mainpage.permtitle[write] ng 
mainpage.permtitle[read,write] ng 
mainpage.permtitle.read 1 ok 
mainpage.permtitle.write 1 ok 
mainpage.permtitle.read 1 ok 
mainpage.permtitle 1 ok 
mainpage.read ng 
mainpage.write ng 
permtitle.read ng 

おさらい的メモ

下記に入力、紐付けはみたまま。
users_groups
users_groups_roles
users_role
users_role_permissions
actionsは文字列で書いちゃダメ。シリアライズで、intの配列キー指定に注意する。
(その後、たぶん、Ver1.8の公式ドキュメントはわかりやすくなった気がする)
デフォルトでは、adminのロールにはルールが適用されないみたい。フラグが立ってるから。
classとaction_メソッドをリストにして自動で権限テーブルに追加するスクリプト(汚い)を書いたけど、デフォルトで提供されてたらいいかもなと思った。。

// キャッシュされているすべてのパーミッションをフラッシュ
\Cache::delete_all(\Config::get('ormauth.cache_prefix', 'auth').'.permissions');
\Cache::delete(\Config::get('ormauth.cache_prefix', 'auth').'.groups');
\Cache::delete(\Config::get('ormauth.cache_prefix', 'auth').'.roles');
24
21
1

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
24
21