2017 © Pedro Peláez
 

yii2-extension yii2-rbac

RBAC Auth manager for Yii2

image

yiier/yii2-rbac

RBAC Auth manager for Yii2

  • Friday, September 25, 2015
  • by forecho
  • Repository
  • 14 Watchers
  • 4 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

RBAC Manager for Yii 2

RBAC Auth manager for Yii2, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

php composer.phar require --prefer-dist yiier/yii2-rbac "*"

or add, (*5)

"yiier/yii2-rbac": "*"

to the require section of your composer.json file., (*6)

Usage

配置

  • 修改配置文件,比如 修改 config/main.php, 添加:
return [
    'modules' => [
        'rbac' => [
            'class' => 'yiier\rbac\Module',
            // 'ignoreModules' => ['gii', 'debug', 'rbac'],
            // 'menus' => [
            //    'rbac' =>'RBAC',
            //    'permissions' =>'Permissions',
            //    'roles' =>'Roles',
            //    'users' =>'Users',
            //    'rules' =>'Rules',
            // ],
            // 'safeDelete' => false, // 默认为 true,当为 true 时,角色有用户时,不能直接删除角色,要先去掉角色的所有用户
            // 'rulesPath' => ['@app/rbac/rules'], // rules 路径
            // 'userClassName' => 'app\models\User',
            // 'idField' => 'id',
            // 'usernameField' => 'shop_name',
            // 'mainLayout' => '@app/views/layout/rbac.php',
            // 'isAdvanced' => true, // 如果使用的是高级模板
            // 'advancedConfigs' = [ //如果你还有其他应用需要加入控制
            //     [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@frontend/config/main.php',
            //           '@frontend/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@api/config/main.php',
            //           '@api/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@backend/config/main.php',
            //           '@backend/config/main-local.php',
            //      ],
            //    ],
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];
  • 执行数据库操作,生成表,命令如下:
php yii migrate --migrationPath=@yii/rbac/migrations/
  • 控制访问,修改配置文件,比如 修改 config/main.php,添加:
'components' => [
    // ...
],
'as access' => [
    'class' => 'yiier\rbac\components\AccessControl',
],

可选操作

  • 修改默认参数,修改 config/params.php 添加:
'yiier.rbac.config' => [
    'cacheDuration' => 3600, // 缓存时间,单位是秒。默认是 30 天
    'superManId' => 12 // 拥有所有权限的用户 ID
    'freeAccessRoutes' => ['/site/login', '/site/signup', '/site/error', '/site/captcha', '/site/logout', '/order/*'], // 白名单 URL
]
  • 使用 rbac 模块的自带菜单
echo  \yii\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);
  • 使用 \yiier\rbac\helpers\Html::a ,判断是否有权限,如果没有权限自动隐藏链接
<?= \yiier\rbac\helpers\Html::a(Yii::t('rbac', 'Clear Cache'), ['clear-cache']) ?>
  • 使用 yiier\rbac\widgets\Menu::widget ,判断是否有权限,如果没有权限自动隐藏菜单中的链接
echo  yiier\rbac\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);
  • 使用 yiier\rbac\widgets\SpuerMenu::widget ,判断是否有权限,如果没有权限自动隐藏菜单中的链接,并且能实现自动高亮:
echo  yiier\rbac\widgets\SpuerMenu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'dashboard',
            ],
            [
                'label' => Yii::t('app', 'Create Product'), 
                'icon' => 'plus', 
                'url' => ['/product/create'], 
                'strict' => true // 严格模式
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);

界面操作教程

  • 在『权限列表』中勾选你要控制的菜单,不勾选说明不打算加入权限控制

你也可以添加菜单的描述,更方便使用。描述会默认读取对应 actionXXX 方法中注释的 @title 权限列表。, (*7)

另外操作会自动立即保存,无需再点保存。, (*8)

, (*9)

  • 在『角色管理』中,添加完角色之后再可以分别给角色分配用户和权限。操作的时候点击对应的权限/用户会立即自动保存。

, (*10)

, (*11)

, (*12)

  • 『用户列表』:只能看某个用户拥有的所有角色和所有权限

, (*13)

  • 『规则列表』:

待补充, (*14)

Get√Yii Web development has never been so fun! https://getyii.com/, (*15)

The Versions

25/09 2015

dev-master

9999999-dev

RBAC Auth manager for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2

25/09 2015

v0.2

0.2.0.0

RBAC Auth manager for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2

24/09 2015

v0.1

0.1.0.0

RBAC Auth manager for Yii2

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2