hscstudio/yii2-mimin

简单的 RBAC 管理器

安装次数: 9,305

依赖: 4

建议者: 0

安全性: 0

星标: 51

关注者: 9

分支: 33

开放问题: 3

类型:yii2-extension

v1.1.5 2019-03-20 07:02 UTC

This package is auto-updated.

Last update: 2024-09-25 21:07:06 UTC


README

为 Yii 2.0.0 开发的简单 RBAC 管理器。对 yii2-admin 扩展的精简版,具有出色功能

Latest Stable Version Total Downloads Latest Unstable Version License

注意

在安装和使用此扩展之前,请确保您的应用程序已经使用数据库进行登录认证。特别是对于 yii 基本模板。因为没有它,此扩展将产生错误且无用。

安装

安装此扩展的最佳方式是通过 composer

运行

php composer.phar require --prefer-dist hscstudio/yii2-mimin "~1.1.5"

或者添加

"hscstudio/yii2-mimin": "~1.1.5"

或者添加此行以安装最后更改

"hscstudio/yii2-mimin": "~1.*"

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

配置

一旦安装了扩展,您只需在代码中通过

在配置中

'as access' => [
     'class' => '\hscstudio\mimin\components\AccessControl',
	 'allowActions' => [
		// add wildcard allowed action here!
		'site/*',
		'debug/*',
		'mimin/*', // only in dev mode
	],
],
...,
'modules' => [
	'mimin' => [
		'class' => '\hscstudio\mimin\Module',
	],
	...
],
'components' => [
	'authManager' => [
		'class' => 'yii\rbac\DbManager', // only support DbManager
	],
],

因为此扩展使用 'yii\rbac\DbManager' 作为认证管理器,所以您应该首先迁移 rbac sql

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

如果您使用 Yii 2.0.6 版本或更高版本,那么迁移此扩展的自定义表

yii migrate --migrationPath=@hscstudio/mimin/migrations

但是如果您安装 Yii 2.0.5 版本或更低版本,那么迁移此扩展的自定义表

yii migrate --migrationPath=@hscstudio/mimin/migrations/old

使用方法

此 RBAC 管理器有三个主要页面,它们是

路由

从应用程序获取所有操作路由。在此处,您可以开启/关闭权限以便不在菜单角色中显示,重命名操作路由的别名/类型,以便易于用户阅读。您可以然后通过以下 URL 访问 路由

https:///path/to/index.php?r=mimin/route

角色

定义用户的访问级别,他是不是超级管理员?员工?收银员?等等。在此菜单中,您可以分配权限/操作路由(应用程序中的操作,例如创建、更新、删除等)到角色。您可以然后通过以下 URL 访问 角色

https:///path/to/index.php?r=mimin/role

以下屏幕截图显示了将路由分配给角色的示例 角色屏幕截图

用户

对于标准用户管理,创建/更新/删除用户,并将角色分配给用户。您可以然后通过以下 URL 访问 用户

https:///path/to/index.php?r=mimin/user

我们建议您激活漂亮的 URL。

小部件上的实现

示例动态按钮

用于检查是否有访问路由的权限

if ((Mimin::checkRoute($this->context->id.'/create'))){
    echo Html::a('Create Note', ['create'], ['class' => 'btn btn-success']);
}

// in other case maybe You want ensure same of route so You can add parameter strict true
if ((Mimin::checkRoute($this->context->id.'/create',true))){

示例动态菜单

用于过滤可访问的菜单权限

use hscstudio\mimin\components\Mimin;
$menuItems = [
    ['label' => 'Home', 'url' => ['/site/index']],
    ['label' => 'About', 'url' => ['/site/about']],
    ['label' => 'Contact', 'url' => ['/site/contact']],
];

if (\Yii::$app->user->isGuest){
    $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
}
else{
    $menuItems[] = ['label' => 'App', 'items' => [
        ['label' => 'Category', 'url' => ['/category/index']],
        ['label' => 'Product', 'url' => ['/product/index']],
        ['label' => 'Cart', 'url' => ['/cart/index']],
    ]];
    $menuItems[] = [
        'label' => 'Logout (' . \Yii::$app->user->identity->username . ')',
        'url' => ['/site/logout'],
        'linkOptions' => ['data-method' => 'post']
    ];
}

$menuItems = Mimin::filterMenu($menuItems);
// in other case maybe You want ensure same of route so You can add parameter strict true
// $menuItems = Mimin::filterMenu($menuItems,true); 

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => $menuItems,
]);

示例动态操作列模板

用于过滤 Gridview 操作列的模板

use hscstudio\mimin\components\Mimin;
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ...,
        [
          'class' => 'yii\grid\ActionColumn',
          'template' => Mimin::filterActionColumn([
              'update','delete','download'
          ],$this->context->route),
          ...
        ]
    ]
]);

如何贡献

这是一个开源项目,所以您的贡献非常受欢迎。

为了开始

  • 在本地安装此(请参阅安装部分)
  • 克隆此存储库。
  • 查看 README.md
  • 发送 pull requests

除了通过 pull requests 贡献之外,您还可以 提交问题

我们的团队

我们感谢我们的 贡献者 提高了这个工具。谢谢!

雅加达 - 印度尼西亚

手册

本书全面讨论了基于Yii 2的RBAC(基于角色的访问控制)和yii2-mimin的教程。有关更多信息,请参阅我的书籍:https://hscstudio.github.io/yii2-book-id