arogachev/yii2-rbac
Composer 安装命令:
composer require arogachev/yii2-rbac
包简介
RBAC management for Yii 2 framework
README 文档
README
RBAC management for Yii 2 framework.
The main purpose of this extension is to provide management of RBAC roles, permissions, rules and relations between them through configuration arrays.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist arogachev/yii2-rbac
or add
"arogachev/yii2-rbac": "*"
to the require section of your composer.json file.
Features
- Adding new roles and permissions with descriptions
- Updating permissions names, descriptions and rules
- Deleting permissions, rules
- Updating roles descriptions
- Assigning rules to permissions
- Assigning permissions to roles
Configuration arrays
First of all, you need to create three files for storing RBAC data:
roles.php. Used for storing roles.permissions.php. Used for storing permissions and relations between permissions and rules.children.php. Used for storing relations between roles and permissions.
You can place it anywhere you want. If you are using advanced application, it's recommended to place them in
common/rbac/data folder.
Example of roles.php content:
<?php return [ [ 'name' => 'default', 'description' => 'Default', ], [ 'name' => 'admin', 'description' => 'Administrator', ], [ 'name' => 'operator', 'description' => 'Operator', ], ];
Both name and description are required for filling.
default is not required, but most of the times is needed because some permissions require check without assigning.
In this case make sure you have include it in your application config:
'authManager' => [ 'class' => 'yii\rbac\DbManager', 'defaultRoles' => ['default'], ],
Example of permissions.php content:
<?php return [ [ 'name' => 'users.manage', 'description' => 'Users management', ], [ 'name' => 'users.avatar.upload', 'description' => 'Upload avatar for user', 'rule' => 'arogachev\rbac\rules\CorrespondingUserRule', ], [ 'name' => 'users.avatar.upload.all', 'description' => 'Upload avatar for any user', ], [ 'name' => 'users.password.change', 'description' => 'Change password for user', 'rule' => 'arogachev\rbac\rules\CorrespondingUserRule', ], [ 'name' => 'users.password.change.all', 'description' => 'Change password for any user', ], [ 'name' => 'dispatching-room.access', 'description' => 'Access to dispatching room', ], [ 'name' => 'settings.manage', 'description' => 'Settings management', ], [ 'name' => 'sessions.access', 'description' => 'Sessions management', ], ];
Both name and description are required for filling, rule is optional.
Example of children.php content:
<?php return [ 'default' => [ 'users.password.change', ], 'admin' => [ 'users.manage', 'users.avatar.upload.all', 'users.password.change.all', 'settings.manage', 'sessions.access', ], 'operator' => [ 'users.avatar.upload', 'dispatching-room.access', 'chat.access', ], ];
Data synchronization
To synchronize actual RBAC data with configuration arrays data add this to your console application config
(config/console.php for basic application and console/config/main.php for advanced application):
'controllerMap' => [ 'rbac' => [ 'class' => 'arogachev\rbac\controllers\RbacController', 'parserOptions' => [ 'configPath' => '@common/rbac/data', ], ], ],
Then you need to run command:
php yii rbac
List of available options in parserOptions:
$configPath- full path to folder with config files. Aliases are supported. Required for filling.
Rules
Extension provides arogachev\rbac\rules\CorrespondingUserRule that can be used to only allow
user to edit his own posts, etc. It's similar to AuthorRule described in official docs here.
You can attach it to permission as shown above, and use it in action as follows:
/** * @param integer $id * @return string|\yii\web\Response * @throws BadRequestHttpException * @throws NotFoundHttpException */ public function actionUploadAvatar($id) { $model = $this->findModel($id); if (!Yii::$app->user->can('users.avatar.upload.all') && !Yii::$app->user->can('users.avatar.upload', [ 'model' => $model, 'attribute' => 'id', ])) { throw new BadRequestHttpException('You are not allowed to upload avatar for this user.'); } ... }
Use the related permission after the model was found.
Available params:
$model- Model used for checking. Required for filling.attribute- The attribute name containing user id. Defaults toauthor_id.
In case of using advanced application it's recommended to place common rules like that in common/rbac/rules.
More specific rules can be placed inside of according modules.
GUI
You can use AssignRoleToUserForm for assigning role to user.
Example of action (you can place it in UsersController):
use arogachev\rbac\models\AssignRoleToUserForm; ... /** * Assign RBAC role to user * @param integer $id * @return string|\yii\web\Response * @throws NotFoundHttpException */ public function actionAssignRole($id) { $user = $this->findModel($id); $model = new AssignRoleToUserForm(['user' => $user]); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $model->assignRole(); return $this->redirect('index'); } return $this->render('@rbac/views/users/assign-role', ['model' => $model]); }
There are also assign-role and _assign-role-form (partial) views that you can use. It's for Bootstrap,
if it don't fit your needs you can copy it and modify how you want, it's just a template.
To create a link for that action, most of the times, extending GridView ActionColumn is enough:
[
'class' => ActionColumn::className(),
'template' => '{view} {update} {assign-role} {delete}',
'buttons' => [
'assign-role' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-link"></span>', $url, [
'title' => 'Assign role',
'aria-label' => 'Assign role',
'data-pjax' => '0',
]);
},
],
],
arogachev/yii2-rbac 适用场景与选型建议
arogachev/yii2-rbac 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rbac」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arogachev/yii2-rbac 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arogachev/yii2-rbac 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arogachev/yii2-rbac 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Access management from UI
SCEditor, a lightweight WYSIWYG BBCode & HTML editor extension for Yii 2.0 Framework
Redirect all not authenticated web user to login page.
Permission handling for lumen 6.x and up
Role-based Access Control: Roles, Permissions and ACL
统计信息
- 总下载量: 1.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-12-22