zc/yii2-rbac

用户rbac模块

安装: 15

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 1

分支: 58

类型:yii2-extension

dev-master 2015-11-26 11:37 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:58:46 UTC


README

安装

安装此扩展的首选方法是使用 composer.

可以运行

php composer.phar require --prefer-dist zc/yii2-rbac "*"
php composer.phar require "yiisoft/yii2-jui": "~2.0@dev" //can't be installed via composer.json requiremtns because of DependencyResolver issue

或者添加

"zc/yii2-rbac": "*"

到您的composer.json文件的要求部分。

使用方法

扩展安装完成后,只需修改应用程序配置,如下所示

return [
    //....
    'modules' => [
        'rbac' => [
            'class' => 'zc\rbac\Module',
            //Some controller property maybe need to change.
            'controllerMap' => [
                'assignment' => [
                    'class' => 'zc\rbac\controllers\AssignmentController',
                    'userClassName' => 'path\to\models\User',
                ]
            ]
        ],
    ],

  'components' => [
        ....
        'user' => [
                    'identityClass' => 'common\models\User',//'app\models\AdminUser',用户模型
                    'enableAutoLogin' => true,
                    'loginUrl' => ['user/login'],//登录地址
                ],
         'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest', 'user'],
            'cache' => 'yii\caching\FileCache',
            //'itemTable' => 'AuthItem',
            //'itemChildTable' => 'AuthItemChild',
            //'assignmentTable' => 'AuthAssignment',
            //'ruleTable' => 'AuthRule',
        ],
    ]
    'as access' => [
        'class' => 'zc\rbac\components\AccessControl',
        'allowActions' => [
            '/',
            'home/*',
            'backend/reflushmenu',
            'home/captcha',
            'home/error',
            'user/logout',
            'user/login',
            //'some-controller/some-action',
            // The actions listed here will be allowed to everyone including guests.
            // So, 'admin/*' should not appear here in the production, of course.
            // But in the earlier stages of your development, you may probably want to
            // add a lot of actions here until you finally completed setting up rbac,
            // otherwise you may not even take a first step.
        ],
        'rules'        => [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
            [
                'actions' => ['logout'],
                'allow' => true,
                'roles' => ['@'],
            ],
            [
                'actions' => ['error'],
                'allow'   => true,
            ],
            [
                'actions' => ['login'],
                'allow'   => true,
                'roles'   => ['?'],
            ],
        ],
    ],
];

您需要通过以下命令执行rbac初始化迁移

php yii migrate/up --migrationPath=@zc/rbac/migrations

然后您可以通过以下URL访问Auth管理器

https:///path/to/index.php?r=rbac/
https:///path/to/index.php?r=rbac/route
https:///path/to/index.php?r=rbac/permission
https:///path/to/index.php?r=rbac/role
https:///path/to/index.php?r=rbac/assignment

要应用规则,请将以下代码添加到您的控制器中

use zc\rbac\components\AccessControl;

class ExampleController extends Controller 
{

/**
 * 如果在配置文件里面配置了,这里就可以不用了
 * Returns a list of behaviors that this component should behave as.
 */
public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
            ],
            'verbs' => [
                ...
            ],
        ];
    }
  // Your actionss
}