首頁 > 後端開發 > php教程 > Yii2 使用RBAC

Yii2 使用RBAC

WBOY
發布: 2016-08-08 09:24:22
原創
1049 人瀏覽過

1.在/basic/config/console.php和/basic/config/web.php裡,設定元件,這裡只貼出console.php裡的程式碼:

<?php

Yii::setAlias(&#39;@tests&#39;, dirname(__DIR__) . &#39;/tests&#39;);

$params = require(__DIR__ . &#39;/params.php&#39;);
$db = require(__DIR__ . &#39;/db.php&#39;);

return [
    &#39;id&#39; => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log', 'gii'],
    'controllerNamespace' => 'app\commands',
    'modules' => [
        'gii' => 'yii\gii\Module',
    ],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,'authManager' => [
    				'class' => 'yii\rbac\DbManager',
    				'itemTable' => 'web_auth_item',
    				'assignmentTable' => 'web_auth_assignment',
    				'itemChildTable' => 'web_auth_item_child',
        			'ruleTable'=>'web_auth_rule'
    		],
    ],
    'params' => $params,
];
登入後複製
如果console.php裡沒有配置,會報下列錯誤:

You should configure "authManager" component to use database before executing this migration.
登入後複製

2.開啟命令列

3.cd 指令切換到/php/basic目錄 

4.輸入指令:yii migrate --migration=@yii/rbac/migrations/5 .建立Permission:

    public function createPermission($item)
    {
        $auth = Yii::$app->authManager;

        $createPost = $auth->createPermission($item);
        $createPost->description = '创建了 ' . $item . ' 许可';
        $auth->add($createPost);
    }
登入後複製
6.創建Role:


public function createRole($item)
    {
        $auth = Yii::$app->authManager;

        $role = $auth->createRole($item);
        $role->description = '创建了 ' . $item . ' 角色';
        $auth->add($role);
    }
登入後複製
7.Role分配Permission


 static public function createEmpowerment($items)
    {
        $auth = Yii::$app->authManager;

        $parent = $auth->createRole($items['name']);
        $child = $auth->createPermission($items['description']);

        $auth->addChild($parent, $child);
    }
登入後複製
8.角色分配用戶:Control權限驗證


 static public function assign($item)
    {
        $auth = Yii::$app->authManager;
        $reader = $auth->createRole($item['name']);
        $auth->assign($reader, $item['description']);
    }
登入後複製
11.在Controller裡自訂驗證


 public function beforeAction($action)
    {
        $action = Yii::$app->controller->action->id;
        if(\Yii::$app->user->can($action)){
            return true;
        }else{
            throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限');
        }
    }
登入後複製
class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\web\AccessControl::className(),
                'only' => ['login', 'logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['login', 'signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    // ...
登入後複製


以上就介紹了Yii2 使用RBAC,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板