denchotsanov/yii2-user-rbac

安装次数: 55

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:yii2-extension

v0.3.7 2019-11-18 20:02 UTC

This package is auto-updated.

Last update: 2024-09-25 08:11:36 UTC


README

Yii2 RBAC 用户扩展


Latest Stable Version Total Downloads Latest Unstable Version License

安装

安装此扩展的首选方式是通过 Composer

运行以下命令:

composer require --prefer-dist denchotsanov/yii2-user-rbac "*"

或在您的 composer.json 文件的 require 部分添加以下内容:

"denchotsanov/yii2-user-rbac ": "*"

安装

使用

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

return [
    'modules' => [
        'rbac' => [
            'class' => 'denchotsanov\rbac\Module',
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];

下载并配置 Yii2-rbac 后,您需要执行以下操作更新您的数据库模式:

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

或在控制台配置文件中添加

'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                ...
                '@yii/rbac/migrations',
                ...
            ],
        ],
    ],

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

[SERVER]/rbac/
[SERVER]/rbac/route
[SERVER]/rbac/permission
[SERVER]/rbac/role
[SERVER]/rbac/assignment

应用规则

  1. 仅对 controller 应用规则,请添加以下代码:
use denchotsanov\rbac\filters\AccessControl;

class ExampleController extends Controller 
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'allowActions' => [
                    'index',
                    // The actions listed here will be allowed to everyone including guests.
                ]
            ],
        ];
    }
}
  1. module 应用规则,请添加以下代码:
use Yii;
use denchotsanov\rbac\filters\AccessControl;

/**
 * Class Module
 */
class Module extends \yii\base\Module
{
    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            AccessControl::class
        ];
    }
}
  1. 您还可以通过主要配置应用规则。
// apply for single module

'modules' => [
    'rbac' => [
        'class' => 'denchotsanov\rbac\Module',
        'as access' => [
            'class' => denchotsanov\rbac\filters\AccessControl::class
        ],
    ]
]

// or apply globally for whole application

'modules' => [
    ...
],
'components' => [
    ...
],
'as access' => [
    'class' => denchotsanov\rbac\filters\AccessControl::class,
    'allowActions' => [
        'site/*',
        'admin/*',
        // 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.
    ]
 ],