romi45/yii2-rbac-collector

使用此扩展,您可以在应用程序模块中组织 RBAC 配置到单独的文件中。

1.0.8 2018-04-03 15:33 UTC

This package is auto-updated.

Last update: 2024-08-27 18:57:12 UTC


README

此扩展在 Yii 2 AuthManager 和可存储在应用程序模块中单独配置类之间的访问配置之间提供了一层。

这对于拥有大量访问规则、角色和其他内容的大型项目非常有用。

当您在模块中组织 RBAC 代码时,您可以轻松管理它。

它仅是控制台应用程序组件 - 您不需要更改您的 web 应用程序中的任何内容 - 只需像以前一样使用 Yii::$app->authManager。

安装

通过 composer 安装此扩展。

运行以下命令之一:

$ composer require romi45/yii2-rbac-collector:~1.0

或添加

"romi45/yii2-rbac-collector": "~1.0"

到您的 composer.json 文件的 require 部分。

配置

将 rbacc 模块添加到 控制台应用程序 配置的模块部分。

'modules' => [
    ...
    'rbacc' => [
        'class' => 'rbacc\Module',
        'collection' => [
            // Here is a list of your RBAC config classes. Example you can get in /example directory
            'app\modules\user\rbac\Config',
            'app\modules\blog\rbac\Config',
            'app\modules\hobby\rbac\YouCanNameItAsYouWant',
        ]
    ],
    ...
]

以下是一个配置类示例。数组键 - 认证项目名称值 - 认证项目数据

如果值不是数组,收集器将其识别为权限。如果值是数组,则必须指定项目的类型

namespace rbacc\example;

use rbacc\components\ConfigBase;
use rbacc\example\rules\UpdateOwnDataRule;
use yii\rbac\Item;

/**
 * Class Config
 *
 * Example RBAC configuration class
 *
 * @package user\rbac
 */
class Config extends ConfigBase
{
    /**
     * Gets config data as array
     *
     * @return array
     */
    public function getData()
    {
        return [
            'user___user__view_profile' => 'View user profile',
            'user___user__view_own_profile' => [
                'type' => Item::TYPE_PERMISSION,
                'description' => 'View user own profile',
                'rule' => new UpdateOwnDataRule(),
                'children' => ['user___user__view_profile']
            ],
            'user' => [
                'type' => Item::TYPE_ROLE,
                'description' => 'User',
                'children' => ['user___user__view_own_profile'],
            ],
            'admin' => [
                'type' => Item::TYPE_ROLE,
                'description' => 'Admin',
                'children' => ['user___user__view_profile']
            ],
            'owner' => [
                'type' => Item::TYPE_ROLE,
                'description' => 'Application owner',
                'children' => ['admin'],
            ]
        ];
    }
}

在更改后更新 RBAC 配置,只需运行以下命令

$ yii rbacc/update

收集器将读取您的配置类,并使用您的当前 AuthManager - PhpManager 或 DbManager 更新 RBAC 数据

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。