Yii2 RbacController示例

RbacController 示例:

<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
class RbacController extends Controll
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        // 添加“createPost”权限
        $createPost = $auth->createPermission('createPost');
        $createPost->description = 'Create a post';
        $auth->add($createPost);

        // 添加 "updatePost" 权限
        $updatePost = $auth->createPermission('updatePost');
        $updatePost->description = 'Update post';
        $auth->add($updatePost);

        // 添加 "author" 角色并给与 "createPost" 权限
        $author = $auth->createRole('author');
        $auth->add($author);
        $auth->addChild($author, $createPost);

        // 添加 "admin" 角色并给与 "updatePost" 权限
        // 和 "author" 权限
        $admin = $auth->createRole('admin');
        $auth->add($admin);
        $auth->addChild($admin, $updatePost);
        $auth->addChild($admin, $author);

        // 为用户指派角色.其中1和2是由 IdentityInterface::getId() 返回的id (user 表)
        // 通常在 user 模型中实现这个函数.$auth->assign($author, 2);
        $auth->assign($admin, 1);
    }
}

a

 

posted @ 2018-01-02 18:59  Chrdai  阅读(259)  评论(0编辑  收藏  举报