jackh/yii2-admin

Yii2 的 RBAC(基于角色的访问控制)认证管理器

安装: 4

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 2

类型:yii2-extension

1.1.0 2016-04-14 06:31 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:49:06 UTC


README

为 RABC(基于角色的访问控制)Yii2 提供的 GUI 管理器。易于管理用户授权。

Latest Stable Version Total Downloads Reference Status Dependency Status HHVM Status Code Climate

文档

重要:如果您安装的是 3.x 版本,请参阅此 README

安装

使用 Composer 安装

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

运行

php composer.phar require jackhsoft/yii2-admin "~1.0"
or
php composer.phar require jackhsoft/yii2-admin "~2.0"

或者对于 dev-master 版本

php composer.phar require jackhsoft/yii2-admin "2.x-dev"

或者,您可以将

"jackhsoft/yii2-admin": "~2.0"

添加到您的 composer.json 文件的 require 部分并执行 php composer.phar update

从存档安装

从这里下载最新版本 releases,然后将其解压到您的项目中。在您的应用程序配置中,添加此扩展的路径别名。

return [
    ...
    'aliases' => [
        '@jackh/admin' => 'path/to/your/extracted',
        // for example: '@jackh/admin' => '@app/extensions/mdm/yii2-admin-2.0.0',
        ...
    ]
];

使用

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

return [
    'modules' => [
        'admin' => [
            'class' => 'jackh\admin\Module',
            ...
        ]
        ...
    ],
    ...
    'components' => [
        ...
        'authManager' => [
            'class' => 'yii\rbac\PhpManager', // or use 'yii\rbac\DbManager'
        ]
    ],
    'as access' => [
        'class' => 'jackh\admin\components\AccessControl',
        'allowActions' => [
            'site/*',
            'admin/*',
            '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.
        ]
    ],
];

有关更多详细信息,请参阅 Yii RBAC。您可以通过以下 URL 访问认证管理器

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

要使用菜单管理器(可选),在此处执行迁移

yii migrate --migrationPath=@jackh/admin/migrations

如果您使用数据库(类 'yii\rbac\DbManager')来保存 rbac 数据,在此处执行迁移

yii migrate --migrationPath=@yii/rbac/migrations

自定义分配控制器

分配控制器属性可能需要调整到您的应用程序中的 User 模型。要执行此操作,通过 controllerMap 属性更改它们。例如

    'modules' => [
        'admin' => [
            ...
            'controllerMap' => [
                 'assignment' => [
                    'class' => 'jackh\admin\controllers\AssignmentController',
                    /* 'userClassName' => 'app\models\User', */
                    'idField' => 'user_id',
                    'usernameField' => 'username',
                    'fullnameField' => 'profile.full_name',
                    'extraColumns' => [
                        [
                            'attribute' => 'full_name',
                            'label' => 'Full Name',
                            'value' => function($model, $key, $index, $column) {
                                return $model->profile->full_name;
                            },
                        ],
                        [
                            'attribute' => 'dept_name',
                            'label' => 'Department',
                            'value' => function($model, $key, $index, $column) {
                                return $model->profile->dept->name;
                            },
                        ],
                        [
                            'attribute' => 'post_name',
                            'label' => 'Post',
                            'value' => function($model, $key, $index, $column) {
                                return $model->profile->post->name;
                            },
                        ],
                    ],
                    'searchClass' => 'app\models\UserSearch'
                ],
            ],
            ...
        ]
        ...
    ],
  • 必需属性
    • userClassName 您的用户模型的完全限定类名
      通常您不需要明确指定它,因为模块将自动检测它
    • idField 您的用户模型的 ID 字段
      对应于 Yii::$app->user->id 的字段。
      默认值为 'id'。
    • usernameField 您的用户模型的用户名字段
      默认值为 'username'。
  • 可选属性
    • fullnameField 在 "查看" 页面中指定用户全名的字段
      这可以是用户模型或相关模型(例如,配置文件模型)的字段。
      当字段是相关模型时,名称应使用点分隔符表示法指定(例如,'profile.full_name')。
      默认值为 null。
    • extraColumns "索引" 页面中使用的额外列的定义
      这应该是一个网格视图列定义的数组。
      您可以像上面示例中那样包含相关模型的属性。
      默认值为空数组。
    • searchClass 用于搜索用户模型的模型的完全限定类名
      您必须提供适当的搜索模型,以便启用通过额外列的过滤和排序。
      默认值为 null。

自定义布局

默认情况下,管理员模块将使用应用程序级别的布局。在这种情况下,您必须自己编写此模块的菜单。

通过指定 layout 属性,您可以使用模块的内置布局之一:'left-menu'、'right-menu' 或 'top-menu',所有这些都配备了此模块的菜单。

    'modules' => [
        'admin' => [
            ...
            'layout' => 'left-menu', // defaults to null, using the application's layout without the menu
                                     // other avaliable values are 'right-menu' and 'top-menu'
        ],
        ...
    ],

如果您使用其中之一,您还可以自定义菜单。您可以更改菜单标签或禁用它。

    'modules' => [
        'admin' => [
            ...
            'layout' => 'left-menu',
            'menus' => [
                'assignment' => [
                    'label' => 'Grant Access' // change label
                ],
                'route' => null, // disable menu
            ],
        ],
        ...
    ],

在使用模块的专用布局时,您可能仍然希望将其包裹在具有您的应用程序导航栏和品牌标志的主布局中。您可以通过指定具有应用程序主布局的 mainLayout 属性来实现。例如

    'modules' => [
        'admin' => [
            ...
            'layout' => 'left-menu',
            'mainLayout' => '@app/views/layouts/main.php',
            ...
        ],
        ...
    ],

屏幕截图