mysql添加表
打开\vendor\yiisoft\yii2\rbac\migrations\schema-mysql.sql,复制粘贴里面的sql语句放在mysql里执行一遍,增加了4张表
2.配置rbac
1
2
3
4
5
6
7
8
9
10
11
|
‘components‘ => [ ...... //添加这段代码 ‘authManager‘ => [ ‘class‘ => ‘yii\rbac\DbManager‘ , ‘itemTable‘ => ‘auth_item‘ , ‘assignmentTable‘ => ‘auth_assignment‘ , ‘itemChildTable‘ => ‘auth_item_child‘ , ], ...... ] |
3.表的简介
auth_item
存放 1,角色(type=1) 2,权限&路由(type=2)
auth_item_child
存放 1,角色->权限 2,权限->路由 之间的对应关系
auth_assignment
存放 角色与用户id的对应关系
controller中验证用户权限
1.
1
2
3
4
5
6
7
8
|
public function actionCreate() { if (Yii:: $app ->authManager->checkAccess(Yii:: $app ->user->id, ‘item/create‘ )) //第二个参数可以填权限,也可以填具体的路由 { echo "ok!" ; } ... } |
2. 或者添加一个beforeAction方法
1
2
3
4
5
6
7
8
|
public function beforeAction() { if (Yii:: $app ->user->can(Yii:: $app ->requestedRoute)){ return true; } else { throw new \yii\web\UnauthorizedHttpException( ‘你没有操作权限‘ ); } } |
原文:http://www.cnblogs.com/longzhankunlun/p/6261407.html