下面介绍一个 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