RBAC Auth manager for Yii2
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiier/yii2-rbac "*"
or add
"yiier/yii2-rbac": "*"
to the require section of your composer.json
file.
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
]
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 权限列表
。
另外操作会自动立即保存,无需再点保存。
待补充
Get√Yii Web development has never been so fun! https://getyii.com/
Comments