PHPIN.NET

 找回密码
 立即注册
查看: 6546|回复: 0

[技巧手记] Yii2 使用 RBAC

[复制链接]

469

主题

31

回帖

5507

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5507
发表于 2015-7-9 10:15:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Yii2 使用 RBAC


转载:http://blog.csdn.net/xundh/article/details/45687859

1.在/basic/config/console.php和/basic/config/web.php里,配置组件,这里只贴出console.php里的代码 :
  1. </pre><pre name="code" class="php"><?php

  2. Yii::setAlias('@tests', dirname(__DIR__) . '/tests');

  3. $params = require(__DIR__ . '/params.php');
  4. $db = require(__DIR__ . '/db.php');

  5. return [
  6.     'id' => 'basic-console',
  7.     'basePath' => dirname(__DIR__),
  8.     'bootstrap' => ['log', 'gii'],
  9.     'controllerNamespace' => 'app\commands',
  10.     'modules' => [
  11.         'gii' => 'yii\gii\Module',
  12.     ],
  13.     'components' => [
  14.         'cache' => [
  15.             'class' => 'yii\caching\FileCache',
  16.         ],
  17.         'log' => [
  18.             'targets' => [
  19.                 [
  20.                     'class' => 'yii\log\FileTarget',
  21.                     'levels' => ['error', 'warning'],
  22.                 ],
  23.             ],
  24.         ],
  25.         'db' => $db,'authManager' => [
  26.                                     'class' => 'yii\rbac\DbManager',
  27.                                     'itemTable' => 'web_auth_item',
  28.                                     'assignmentTable' => 'web_auth_assignment',
  29.                                     'itemChildTable' => 'web_auth_item_child',
  30.                                 'ruleTable'=>'web_auth_rule'
  31.                     ],
  32.     ],
  33.     'params' => $params,
  34. ];
复制代码
如果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:
  1.     public function createPermission($item)
  2.     {
  3.         $auth = Yii::$app->authManager;

  4.         $createPost = $auth->createPermission($item);
  5.         $createPost->description = '创建了 ' . $item . ' 许可';
  6.         $auth->add($createPost);
  7.     }
复制代码
6.创建Role:
  1. public function createRole($item)
  2.     {
  3.         $auth = Yii::$app->authManager;

  4.         $role = $auth->createRole($item);
  5.         $role->description = '创建了 ' . $item . ' 角色';
  6.         $auth->add($role);
  7.     }
复制代码
7.Role分配Permission
  1. static public function createEmpowerment($items)
  2.     {
  3.         $auth = Yii::$app->authManager;

  4.         $parent = $auth->createRole($items['name']);
  5.         $child = $auth->createPermission($items['description']);

  6.         $auth->addChild($parent, $child);
  7.     }
复制代码
8.角色分配用户:
  1. static public function assign($item)
  2.     {
  3.         $auth = Yii::$app->authManager;
  4.         $reader = $auth->createRole($item['name']);
  5.         $auth->assign($reader, $item['description']);
  6.     }
复制代码
9.验证权限:
  1. public function beforeAction($action)
  2.     {
  3.         $action = Yii::$app->controller->action->id;
  4.         if(\Yii::$app->user->can($action)){
  5.             return true;
  6.         }else{
  7.             throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限');
  8.         }
  9.     }
复制代码
10.Controller里的权限验证
  1. class SiteController extends Controller
  2. {
  3.     public function behaviors()
  4.     {
  5.         return [
  6.             'access' => [
  7.                 'class' => \yii\web\AccessControl::className(),
  8.                 'only' => ['login', 'logout', 'signup'],
  9.                 'rules' => [
  10.                     [
  11.                         'actions' => ['login', 'signup'],
  12.                         'allow' => true,
  13.                         'roles' => ['?'],
  14.                     ],
  15.                     [
  16.                         'actions' => ['logout'],
  17.                         'allow' => true,
  18.                         'roles' => ['@'],
  19.                     ],
  20.                 ],
  21.             ],
  22.         ];
  23.     }
  24.     // ...
复制代码
11.在Controller里自定义验证
  1. class SiteController extends Controller
  2. {
  3.     public function behaviors()
  4.     {
  5.         return [
  6.             'access' => [
  7.                 'class' => \yii\web\AccessControl::className(),
  8.                 'only' => ['special-callback'],
  9.                 'rules' => [
  10.                     [
  11.                         'actions' => ['special-callback'],
  12.                         'allow' => true,
  13.                         'matchCallback' => function ($rule, $action) {
  14.                             return date('d-m') === '31-10';
  15.                         }
  16.                     ],
复制代码
  1.    // ...
  2.     // Match callback called! 此页面可以访问只有每个10月31日
  3.     public function actionSpecialCallback()
  4.     {
  5.         return $this->render('happy-halloween');
  6.     }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-20 03:55

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表