yiisoft/rbac-php
Composer 安装命令:
composer require yiisoft/rbac-php
包简介
Yii RBAC PHP File Storage
README 文档
README
Yii RBAC PHP File Storage
This package provides PHP file-based storage for RBAC (Role-Based Access Control) package.
Requirements
- PHP 8.1 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/rbac-php
See yiisoft/rbac for RBAC package installation instructions.
General usage
The storage is suitable for authorization data that is not too big (for example, the authorization data for a personal blog system) or for fairly static RBAC hierarchy.
Authorization data is stored in PHP files. PHP should be able to read and write these files. Non-existing files will be created automatically on any write operation.
Using storages
The storages are not intended to be used directly. Instead, use them with Manager from
Yii RBAC package:
use Yiisoft\Rbac\Manager; use Yiisoft\Rbac\Permission; use Yiisoft\Rbac\Php\AssignmentsStorage; use Yiisoft\Rbac\Php\ItemsStorage; use Yiisoft\Rbac\RuleFactoryInterface; $directory = __DIR__ . '/rbac'; $itemsStorage = new ItemsStorage($directory . '/items.php'); $assignmentsStorage = new AssignmentsStorage($directory . '/assignments.php'); /** @var RuleFactoryInterface $rulesContainer */ $manager = new Manager( itemsStorage: $itemsStorage, assignmentsStorage: $assignmentsStorage, // Requires https://github.com/yiisoft/rbac-rules-container or other compatible factory. ruleFactory: $rulesContainer, ), $manager->addPermission(new Permission('posts.create'));
Note that it's not necessary to use both PHP storages. Combining different implementations is possible. A quite popular case is to manage items via PHP file while store assignments in database (see Cycle and Yiisoft DB implementations).
More examples can be found in Yii RBAC documentation.
File structure
In case you decide to manually edit the files, make sure to keep the following structure.
Items
Required and optional fields:
return [ [ 'name' => 'posts.update', 'description' => 'Update a post', // Optional 'rule_name' => 'is_author', // Optional 'type' => 'permission', // or 'role' 'created_at' => 1683707079, // UNIX timestamp, optional 'updated_at' => 1683707079, // UNIX timestamp, optional ], ];
While it's recommended to maintain created and updated timestamps, if any is missing, the file modification time will be used instead as a fallback.
The structure for an item with children:
return [ [ 'name' => 'posts.redactor', 'type' => 'role', 'created_at' => 1683707079, 'updated_at' => 1683707079, 'children' => [ 'posts.viewer', 'posts.create', 'posts.update', ], ], ];
The complete example for managing posts:
return [ [ 'name' => 'posts.admin', 'type' => 'role', 'created_at' => 1683707079, 'updated_at' => 1683707079, 'children' => [ 'posts.redactor', 'posts.delete', 'posts.update.all', ], ], [ 'name' => 'posts.redactor', 'type' => 'role', 'created_at' => 1683707079, 'updated_at' => 1683707079, 'children' => [ 'posts.viewer', 'posts.create', 'posts.update', ], ], [ 'name' => 'posts.viewer', 'type' => 'role', 'created_at' => 1683707079, 'updated_at' => 1683707079, 'children' => [ 'posts.view', ], ], [ 'name' => 'posts.view', 'type' => 'permission', 'created_at' => 1683707079, 'updated_at' => 1683707079, ], [ 'name' => 'posts.create', 'type' => 'permission', 'created_at' => 1683707079, 'updated_at' => 1683707079, ], [ 'name' => 'posts.update', 'rule_name' => 'is_author', 'type' => 'permission', 'created_at' => 1683707079, 'updated_at' => 1683707079, ], [ 'name' => 'posts.delete', 'type' => 'permission', 'created_at' => 1683707079, 'updated_at' => 1683707079, ], [ 'name' => 'posts.update.all', 'type' => 'permission', 'created_at' => 1683707079, 'updated_at' => 1683707079, ], ];
Assignments
return [ [ 'item_name' => 'posts.redactor', 'user_id' => 'john', 'created_at' => 1683707079, // Optional ], // ... [ 'item_name' => 'posts.admin', 'user_id' => 'jack', 'created_at' => 1683707079, ], ];
While it's recommended to maintain created timestamps, if it is missing, the file modification time will be used instead as a fallback.
Concurrency
By default, working with PHP storage does not support concurrency. This might be OK if you store its files under VCS for example. If your scenario is different and, let's say, some kind of web interface is used - then, to enable concurrency, do not use the storage directly - wrap it with decorator instead:
use Yiisoft\Rbac\Manager; use Yiisoft\Rbac\Permission; use Yiisoft\Rbac\Php\AssignmentsStorage; use Yiisoft\Rbac\Php\ConcurrentAssignmentsStorageDecorator; use Yiisoft\Rbac\Php\ConcurrentItemsStorageDecorator; use Yiisoft\Rbac\Php\ItemsStorage; use Yiisoft\Rbac\RuleFactoryInterface; $directory = __DIR__ . DIRECTORY_SEPARATOR . 'rbac'; $itemsSstorage = new ConcurrentItemsStorageDecorator(ItemsStorage($directory)); $assignmentsStorage = new ConcurrentAssignmentsStorageDecorator(AssignmentsStorage($directory)); /** @var RuleFactoryInterface $rulesContainer */ $manager = new Manager( itemsStorage: $itemsStorage, assignmentsStorage: $assignmentsStorage, // Requires https://github.com/yiisoft/rbac-rules-container or other compatible factory. ruleFactory: $rulesContainer, ),
Note that it will have an impact on performance so don't use it unless you really have to.
Configuring file updated time
A closure can be used to customize getting file modification time:
use Yiisoft\Rbac\Php\AssignmentsStorage; use Yiisoft\Rbac\Php\ItemsStorage; $directory = __DIR__ . '/rbac', $getFileUpdatedAt = static fn (string $filePath): int|false => @filemtime($filePath) $itemsStorage = new ItemsStorage( $directory . '/items.php', getFileUpdatedAt: static fn (string $filePath): int|false => @filemtime($filePath), ); $itemsStorage = new AssignmentsStorage( $directory . '/assignments.php', getFileUpdatedAt: static fn (string $filePath): int|false => @filemtime($filePath), );
This is useful for 2 things:
- Using for empty timestamps when files are edited manually.
- Detection of file changes when concurrency is enabled. This helps to optimize perfomance by preventing of unnecessary loads (when file contents has not been changed).
Syncing storages manually
The storages stay synced thanks to manager, but there can be situations where you need to sync them manually. One of them is editing storage manually.
Let's say PHP files are used for both items and assignments and some items were deleted.
return [
[
'name' => 'posts.admin',
'type' => 'role',
'created_at' => 1683707079,
'updated_at' => 1683707079,
'children' => [
'posts.redactor',
'posts.delete',
'posts.update.all',
],
],
- [
- 'name' => 'posts.redactor',
- 'type' => 'role',
- 'created_at' => 1683707079,
- 'updated_at' => 1683707079,
- 'children' => [
- 'posts.viewer',
- 'posts.create',
- 'posts.update',
- ],
- ],
[
'name' => 'posts.viewer',
'type' => 'role',
'created_at' => 1683707079,
'updated_at' => 1683707079,
'children' => [
'posts.view',
],
],
[
'name' => 'posts.view',
'type' => 'permission',
'created_at' => 1683707079,
'updated_at' => 1683707079,
],
[
'name' => 'posts.create',
'type' => 'permission',
'created_at' => 1683707079,
'updated_at' => 1683707079,
],
- [
- 'name' => 'posts.update',
- 'rule_name' => 'is_author',
- 'type' => 'permission',
- 'created_at' => 1683707079,
- 'updated_at' => 1683707079,
- ],
[
'name' => 'posts.delete',
'type' => 'permission',
'created_at' => 1683707079,
'updated_at' => 1683707079,
],
[
'name' => 'posts.update.all',
'type' => 'permission',
'created_at' => 1683707079,
'updated_at' => 1683707079,
],
];
Then related entries in assignments storage needs to be deleted as well:
return [ - [ - 'item_name' => 'posts.redactor', - 'user_id' => 'john', - 'created_at' => 1683707079, - ], [ 'item_name' => 'posts.admin', 'user_id' => 'jack', 'created_at' => 1683707079, ], ];
When using database as a second storage, this can be done within a migration. Depending on chosen implementation, refer to either RBAC Cycle example or RBAC DB example.
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii RBAC PHP File Storage is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/rbac-php 适用场景与选型建议
yiisoft/rbac-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.66k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2020 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「storage」 「yii」 「rbac」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/rbac-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/rbac-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/rbac-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Priveate for SkeekS CMS
Access management from UI
C3JS Chart for Yii Framework 2.0
统计信息
- 总下载量: 78.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 13
- 依赖项目数: 6
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-06-08