下面介紹一個 yii2.0 的 Rbac 權限設置,閒話少說,直接上代碼,php
'authManager' => [ 'class' => 'yii\rbac\DbManager', 'itemTable' => 'auth_item', 'assignmentTable' => 'auth_assignment', 'itemChildTable' => 'auth_item_child', ],
public function createPermission($item) { $auth = Yii::$app->authManager; $createPost = $auth->createPermission($item); $createPost->description = '建立了 ' . $item . ' 許可'; $auth->add($createPost); }
三、好的,許可咱們就建立完成了,下面咱們建立一個 角色吧 roleshtml
public function createRole($item) { $auth = Yii::$app->authManager; $role = $auth->createRole($item); $role->description = '建立了 ' . $item . ' 角色'; $auth->add($role); }
static public function createEmpowerment($items) { $auth = Yii::$app->authManager; $parent = $auth->createRole($items['name']); $child = $auth->createPermission($items['description']); $auth->addChild($parent, $child); }
五、好的,分配許可也建立完成了,我操,太尼瑪簡單了,繼續上代碼,給角色分配用戶 web
static public function assign($item) { $auth = Yii::$app->authManager; $reader = $auth->createRole($item['name']); $auth->assign($reader, $item['description']); }
六、好的好的,就是這麼簡單,我本身都他媽不敢相信啊,你相信嗎???最後一步,驗證用戶是否有權限sql
public function beforeAction($action) { $action = Yii::$app->controller->action->id; if(\Yii::$app->user->can($action)){ return true; }else{ throw new \yii\web\UnauthorizedHttpException('對不起,您如今還沒獲此操做的權限'); } }
http://www.open-open.com/lib/view/open1434638805348.html數據庫
來自:http://blog.sina.com.cn/s/blog_88a65c1b0101izml.html