Yii2 使用RBAC

來源:互聯網
上載者:User
1.在/basic/config/console.php和/basic/config/web.php裡,配置組件,這裡只貼出console.php裡的代碼 :


 '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 --migrationPath=@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.角色指派使用者:

 static public function assign($item)    {        $auth = Yii::$app->authManager;        $reader = $auth->createRole($item['name']);        $auth->assign($reader, $item['description']);    }

9.驗證許可權:

 public function beforeAction($action)    {        $action = Yii::$app->controller->action->id;        if(\Yii::$app->user->can($action)){            return true;        }else{            throw new \yii\web\UnauthorizedHttpException('對不起,您現在還沒獲此操作的許可權');        }    }

10.Controller裡的許可權驗證

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' => ['@'],                    ],                ],            ],        ];    }    // ...

11.在Controller裡自訂驗證

class SiteController extends Controller{    public function behaviors()    {        return [            'access' => [                'class' => \yii\web\AccessControl::className(),                'only' => ['special-callback'],                'rules' => [                    [                        'actions' => ['special-callback'],                        'allow' => true,                        'matchCallback' => function ($rule, $action) {                            return date('d-m') === '31-10';                        }                    ],

   // ...    // Match callback called! 此頁面可以訪問只有每個10月31日    public function actionSpecialCallback()    {        return $this->render('happy-halloween');    }

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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.