博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii2之RBAC权限控制
阅读量:6434 次
发布时间:2019-06-23

本文共 4756 字,大约阅读时间需要 15 分钟。

一、简单自带的ACF静态权限过滤控制

1. 配置:

'components' => [    'authManager' => [        'class' => 'yii\rbac\PhpManager',    ],]

2. controllers/AuthController.php

authManager; // 创建和添加角色 $admin = $auth->createRole('admin'); $auth->add($admin); // 将角色分配到用户 $auth->assign($admin, 1); }} # 访问auth/access进行创建,需要新建文件夹rbac

3. 注册控制

public function behaviors()    {        return [            'access' => [                'class' => AccessControl::className(),                'rules' => [                    [                        'actions' => ['login', 'error'],                        'allow' => true,                    ],                    [                        'actions' => ['captcha', 'logout', 'index'],                        'allow' => true,                        'roles' => ['@'],                    ],                    [ // 控制器方法绑定到角色                        'actions' => ['signup'],                        'allow' => true,                        'roles' => ['admin']                    ]                ],            ],            'verbs' => [                'class' => VerbFilter::className(),                'actions' => [                    'logout' => ['post'],                ],            ],        ];    }

 二、动态自定义的权限控制

1. controller/AuthController.php

authManager; // 创建和添加角色 $admin = $auth->createRole('admin'); $auth->add($admin); // 将角色分配到用户 $auth->assign($admin, 1); return 'success'; } // 动态授权管理 public function actionAuth() { $auth = Yii::$app->authManager; // 删除全部授权 $auth->removeAll(); // 添加权限 $siteSignup = $auth->createPermission('site/signup'); $siteSignup->description = '用户注册'; $auth->add($siteSignup); $userDelete = $auth->createPermission('user/delete'); $userDelete->description = '用户删除'; $auth->add($userDelete); $postDelete = $auth->createPermission('post/delete'); $postDelete->description = '文章删除'; $auth->add($postDelete); $recruitDelete = $auth->createPermission('recruit/delete'); $recruitDelete->description = '招聘删除'; $auth->add($recruitDelete); $feedDelete = $auth->createPermission('contact/delete'); $feedDelete->description = '留言删除'; $auth->add($feedDelete); // 添加规则 $authorDeleteRule = new AuthorDelete; $auth->add($authorDeleteRule); // 添加权限,绑定规则 $authorDelete = $auth->createPermission('authorDelete'); $authorDelete->description = '允许作者删除自己的文章'; $authorDelete->ruleName = $authorDeleteRule->name; // 绑定规则 $auth->add($authorDelete); // 添加角色 $admin = $auth->createRole('admin'); $auth->add($admin); $author = $auth->createRole('author'); $auth->add($author); // 为角色赋予权限 $auth->addChild($admin, $siteSignup); $auth->addChild($admin, $userDelete); $auth->addChild($admin, $postDelete); $auth->addChild($admin, $recruitDelete); $auth->addChild($admin, $feedDelete); $auth->addChild($authorDelete, $postDelete); // 将postDelete作为authorDelete子规则 $auth->addChild($author, $authorDelete); // 将角色分配到用户 $auth->assign($admin, 1); return 'success'; } public function actionError() { return $this->render('error'); }}

2. rbac/AuthorDelete.php

2. siteController.php

// behaviors'access' => [                'class' => AccessControl::className(),                'rules' => [                    [                        'actions' => ['login', 'error', 'signup'],                        'allow' => true,                    ],                    [                        'actions' => ['captcha', 'logout', 'index'],                        'allow' => true,                        'roles' => ['@'],                    ],                ],            ],// signup    public function actionSignup()    {        $this->layout = 'login';        if (!Yii::$app->user->can('site/signup')) {            $this->layout = 'main';            return $this->redirect('/auth/error');        }                            $model = new SignupForm();        if ($model->load(Yii::$app->request->post())) {            if ($user = $model->signup()) {                if (Yii::$app->getUser()->login($user)) {                    $auth = Yii::$app->authManager;                    $author = $auth->createRole('author');                    $auth->assign($author, Yii::$app->user->id);                                        return $this->goHome();                }            }        }        return $this->render('signup', [            'model' => $model,        ]);    }

 

转载于:https://www.cnblogs.com/maoriaty/p/9273012.html

你可能感兴趣的文章
centos7 修改主机名
查看>>
hive中UDF、UDAF和UDTF使用
查看>>
Hibernate学习(六) HQL
查看>>
linux中权限对文件和目录的意义
查看>>
k8s监控
查看>>
案例5:使用Cookie对象保存页面信息
查看>>
Scala 深入浅出实战经典 第77讲:模式匹配下的提取器动手构造实战
查看>>
我的友情链接
查看>>
Windows Server 2016 和Windows 10的中Hyper-V虚拟机生产检查点
查看>>
版本号呀。乱七八遭的
查看>>
搭建自己的CA服务 - OpenSSL CA 实战
查看>>
webservice客户端专题文档
查看>>
DNS 原理
查看>>
我的友情链接
查看>>
python 列表函数
查看>>
【转载】Myeclipse10 安装Aptana插件
查看>>
Social empire here to create a Facebook new headquarters the big Jiedi - Sohu IT
查看>>
对Java多线程技术中所有方法的详细解析
查看>>
我的友情链接
查看>>
我的友情链接
查看>>