首页 / PHP / Yii2使用RBAC
Yii2使用RBAC
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Yii2使用RBAC,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2842字,纯文字阅读大概需要5分钟。
内容图文
![Yii2使用RBAC](/upload/InfoBanner/zyjiaocheng/154/c8f5c80fd3b14d3a8d0f2dae4f4fadc0.jpg)
<?php Yii::setAlias('@tests', dirname(__DIR__) . '/tests'); $params = require(__DIR__ . '/params.php'); $db = require(__DIR__ . '/db.php'); return [ 'id' => 'basic-console', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log', 'gii'], 'controllerNamespace' => 'app\commands', 'modules' => [ 'gii' => 'yii\gii\Module', ], 'components' => [ 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'db' => $db,'authManager' => [ 'class' => 'yii\rbac\DbManager', 'itemTable' => 'web_auth_item', 'assignmentTable' => 'web_auth_assignment', 'itemChildTable' => 'web_auth_item_child', 'ruleTable'=>'web_auth_rule' ], ], 'params' => $params, ];如果console.php里没有配置,会报下面错误:
You should configure "authManager" component to use database before executing this migration.
2.打开命令行
3.cd 命令切换到/php/basic目录
4.输入命令:yii migrate --migrationPath=@yii/rbac/migrations/
5.创建Permission:
public function createPermission($item) { $auth = Yii::$app->authManager; $createPost = $auth->createPermission($item); $createPost->description = '创建了 ' . $item . ' 许可'; $auth->add($createPost); }
6.创建Role:
public function createRole($item) { $auth = Yii::$app->authManager; $role = $auth->createRole($item); $role->description = '创建了 ' . $item . ' 角色'; $auth->add($role); }
7.Role分配Permission
static public function createEmpowerment($items) { $auth = Yii::$app->authManager; $parent = $auth->createRole($items['name']); $child = $auth->createPermission($items['description']); $auth->addChild($parent, $child); }
8.角色分配用户:
static public function assign($item) { $auth = Yii::$app->authManager; $reader = $auth->createRole($item['name']); $auth->assign($reader, $item['description']); }
9.验证权限:
public function beforeAction($action) { $action = Yii::$app->controller->action->id; if(\Yii::$app->user->can($action)){ return true; }else{ throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限'); } }
10.Controller里的权限验证
class SiteController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => \yii\web\AccessControl::className(), 'only' => ['login', 'logout', 'signup'], 'rules' => [ [ 'actions' => ['login', 'signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; } // ...
11.在Controller里自定义验证
class SiteController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => \yii\web\AccessControl::className(), 'only' => ['special-callback'], 'rules' => [ [ 'actions' => ['special-callback'], 'allow' => true, 'matchCallback' => function ($rule, $action) { return date('d-m') === '31-10'; } ],
// ... // Match callback called! 此页面可以访问只有每个10月31日 public function actionSpecialCallback() { return $this->render('happy-halloween'); }
以上就介绍了Yii2 使用RBAC,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的Yii2使用RBAC全部内容,希望文章能够帮你解决Yii2使用RBAC所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。