存取控制过滤器(ACF)详细

https://www.yiichina.com/doc/guide/2.0/security-authorization 

 

 

RBAC简介

 1.mysql添加表

    打开\vendor\yiisoft\yii2\rbac\migrations\schema-mysql.sql,复制粘贴里面的sql语句放在mysql里执行一遍,增加了4张表,然后依次往mysql数据表中填写数据

 

 

 

 或者

 在console/controllers下新建文件RbacController.php

<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{    
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        //添加 "createPost" 权限
        $createPost = $auth->createPermission('createPost');
        $createPost->description = '新增文章';
        $auth->add($createPost);

        //添加 "updatePost" 权限
        $updatePost = $auth->createPermission('updatePost');
        $updatePost->description = '修改文章';
        $auth->add($updatePost);

        //添加 "deletePost" 权限
        $deletePost = $auth->createPermission('deletePost');
        $deletePost->description = '删除文章';
        $auth->add($deletePost);

        //添加 "approveComment" 权限
        $approveComment = $auth->createPermission('approveComment');
        $approveComment->description = '审核评论';
        $auth->add($approveComment);

        //添加 "postadmin" 角色并赋予 "createPost" "updatePost" "deletePost" 权限
        $postAdmin = $auth->createRole('postAdmin');
        $postAdmin->description = '文章管理员';
        $auth->add($postAdmin);
        $auth->addChild($postAdmin, $createPost);
        $auth->addChild($postAdmin, $updatePost);
        $auth->addChild($postAdmin, $deletePost);

        //添加 "postOperator" 角色并赋予 "deletePost" 权限
        $postOperator = $auth->createRole('postOperator');
        $postOperator->description = '文章操作员';
        $auth->add($postOperator);
        $auth->addChild($postOperator, $deletePost);
        
        //添加 "commentAuditor" 角色并赋予 "approveComment" 权限
        $commentAuditor = $auth->createRole('commentAuditor');
        $commentAuditor->description = '评论审核员';
        $auth->add($commentAuditor);
        $auth->addChild($commentAuditor, $approveComment);

        //添加 "admin" 角色并赋予所有其他角色拥有的权限
        $admin = $auth->createRole('admin');
        $admin->description = '系统管理员';
        $auth->add($admin);
        $auth->addChild($admin, $postAdmin);
        $auth->addChild($admin, $commentAuditor);

        //为用户指派角色。其中 1 和 2 是由 IndentityInterface::getId() 返回的 id 
        //通常在你的 User 模型中实现这个函数
        $auth->assign($admin, 1);
        $auth->assign($postAdmin, 2);
        $auth->assign($postOperator, 3);
        $auth->assign($commentAuditor, 4);

    }

}

 在根目录下(有yii.bat)运行

yii rbac/init

 

    

    2.配置rbac,main.php

    '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.

public function actionCreate()
{
  if (!\Yii::$app->user->can('createPost')) //这个createPost就是auth_item表里的createPost权限,这个步骤会检测当前登陆用户的id是否有权限进行此操作,比如用户id为3,auth_assignment表中赋予该id"角色",auth_item_child表中有 角色->权限(角色->角色) 关系,有此权限则通过,没有则抛出一个错误
  {
    throw new ForbiddenHttpException("对不起,你没有进行该操作的权限");
  }

 

   2. 或者添加一个beforeAction方法

public function beforeAction() 
{
    if(Yii::$app->user->can(Yii::$app->requestedRoute))//这是直接把路由当作权限写进了auth_item表里,比如post/create,是个路由写法,而不是createPost这个专门的权限命名
   {
         return true;
    }
    else
    {
         throw new \yii\web\UnauthorizedHttpException('你没有操作权限');
    }
}

 

 

 

posted on 2017-01-08 08:56  longzhankunlun  阅读(481)  评论(0)    收藏  举报