bahirul/yii2-rbac-adminlte

此包已被弃用且不再维护。未建议替代包。

Yii2 AdminLTE 的 RBAC 管理模块

安装: 143

依赖者: 0

建议者: 0

安全: 0

星星: 2

关注者: 1

分支: 51

类型:yii2-extension

2.0 2017-02-08 13:25 UTC

This package is auto-updated.

Last update: 2021-01-19 04:01:41 UTC


README

Yii2-rbac-adminlte 提供了一个用于高级访问控制的 Web 界面,并包含以下功能:

  • 允许对角色、权限、规则进行 CRUD 操作
  • 允许将多个角色或权限分配给用户
  • 允许创建控制台迁移
  • yii2-adminlte-asset 集成

安装

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

运行以下命令:

php composer.phar require --prefer-dist bahirul/yii2-rbac-adminlte:"dev-master"

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

"bahirul/yii2-rbac": "dev-master"

使用

扩展安装后,只需按如下方式修改您的应用程序配置:

return [
    //....
    'modules' => [
        'admin' => [
            'class' => 'app\modules\admin\Module',
            'modules' => [
                'rbac' => [
                    'class' => 'bahirul\yii2rbac\adminlte\Module',
                ],
            ],
        ],
    ],
  'components' => [
         'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest', 'user']
        ],
    ]
];

在下载并配置 Yii2-rbac 之后,您需要做的最后一件事是通过应用迁移来更新您的数据库模式

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

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

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

应用规则

  1. 仅针对 controller 应用规则,请添加以下代码:
use bahirul\yii2rbac\adminlte\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 bahirul\yii2rbac\adminlte\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' => 'bahirul\yii2rbac\adminlte\Module',
        'as access' => [
            'class' => bahirul\yii2rbac\adminlte\filters\AccessControl::class
        ],
    ]
]

// or apply globally for whole application

'modules' => [
    ...
],
'components' => [
    ...
],
'as access' => [
    'class' => bahirul\yii2rbac\adminlte\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.
    ]
 ],

国际化

本扩展中引入的所有文本和信息都可在 'yii2mod.rbac' 类别下进行翻译。您可以使用以下应用程序配置使用本扩展提供的翻译:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'yii2mod.rbac' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii2mod/rbac/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

迁移

您可以创建控制台迁移来创建/更新 RBAC 项目。

模块设置

为了能够创建迁移,您需要将以下代码添加到您的控制台应用程序配置中:

// console.php
'modules' => [
    'rbac' => [
        'class' => 'bahirul\yii2rbac\adminlte\ConsoleModule'
    ]
]

方法

  1. createPermission():创建权限
  2. updatePermission():更新权限
  3. removePermission():删除权限
  4. createRole():创建角色
  5. updateRole():更新角色
  6. removeRole():删除角色
  7. createRule():创建规则
  8. updateRule():更新规则
  9. removeRule():删除规则
  10. addChild():创建子项
  11. removeChild():删除子项
  12. assign():将角色分配给用户

创建迁移

要创建新的迁移,请运行以下命令:

$ php yii rbac/migrate/create <name>

所需的 name 参数给出了新迁移的简要描述。例如,如果迁移是关于创建名为 admin 的新角色,您可以使用名称 create_role_admin 并运行以下命令:

$ php yii rbac/migrate/create create_role_admin

上述命令将在 @app/rbac/migrations 目录中创建一个名为 m160817_085702_create_role_admin.php 的新 PHP 类文件。该文件包含以下代码,主要声明了一个名为 m160817_085702_create_role_admin 的迁移类及其骨架代码

<?php

use bahirul\yii2rbac\adminlte\migrations\Migration;

class m160817_085702_create_role_admin extends Migration
{
    public function safeUp()
    {

    }

    public function safeDown()
    {
        echo "m160817_085702_create_role_admin cannot be reverted.\n";

        return false;
    }
}

以下代码展示了如何实现迁移类以创建一个 admin 角色

<?php

use bahirul\yii2rbac\adminlte\migrations\Migration;

class m160817_085702_create_role_admin extends Migration
{
    public function safeUp()
    {
        $this->createRole('admin', 'admin has all available permissions.');
    }

    public function safeDown()
    {
        $this->removeRole('admin');
    }
}

您可以在以下链接中查看一个复杂的迁移示例:这里。

应用迁移

要将数据库升级到最新结构,您应该使用以下命令应用所有可用的新迁移:

$ php yii rbac/migrate

撤销迁移

要撤销(撤销)之前应用的一个或多个迁移,您可以运行以下命令:

$ php yii rbac/migrate/down     # revert the most recently applied migration
$ php yii rbac/migrate/down 3   # revert the most 3 recently applied migrations

重新执行迁移

重新执行迁移意味着首先撤销指定的迁移,然后再次应用。这可以按照以下方式完成:

$ php yii rbac/migrate/redo     # redo the last applied migration
$ php yii rbac/migrate/redo 3   # redo the last 3 applied migrations