php-extended/php-rbac-object

实现php-rbac-interface库的库

7.0.6 2024-07-31 13:35 UTC

This package is auto-updated.

Last update: 2024-08-31 11:52:25 UTC


README

实现php-rbac-interface库的库。

coverage build status

安装

此库的安装通过composer进行,所有类的自动加载都通过它们的自动加载器进行。

  • 他们的网站下载 composer.phar
  • 然后运行以下命令将此库作为依赖项安装
  • php composer.phar require php-extended/php-rbac-object ^7

基本用法

此库可以使用以下方式使用

首先,您应该提供一个 PhpExtended\Rbac\ProviderInterface 的实现。类 PhpExtended\Rbac\RbacMemoryManager 是内存中所需内容的示例。您可能希望根据您使用的存储系统开发自己的实现。


use PhpExtended\Rbac\RbacMemoryManager;

$memory = new RbacMemoryManager();

// STEP 1 : adds the user

$memory->addUserStatus('userstatus', true); // create an active status
$ustatus = $memory->getUserStatus('userstatus'); // get the object
$memory->addUser('userid', 'username', $ustatus); // create an user
$user = $memory->getUser('userid'); // get the object

// STEP 2 : adds the user into a group

$memory->addGroupStatus('groupstatus', true); // create an active status
$gstatus = $memory->getGroupStatus('groupstatus'); // get the object
$memory->addGroup('groupid', 'groupname', $gstatus); // create a group
$group = $memory->getGroup('groupid'); // get the object
$memory->addUserToGroup($user, $group); // put the user to the group

// STEP 3 : adds the role to the group

$memory->addRoleStatus('rolestatus', true); // create an active status
$rstatus = $memory->getRoleStatus('rolestatus'); // get the object
$memory->addRole('roleid', 'rolename', $rstatus); // create a role
$role = $memory->getRole('roleid'); // get the object
$memory->addRoleToGroup($group, $role); // gives the role to the group

// STEP 4 : check whether the user has the role

$accessor = new SimpleAccessor($memory);

$accessor->checkAccess($user, $role); // returns true

此库还支持嵌套组层次结构(如管理员、版主和普通用户)和嵌套角色(如模块访问、控制器访问和方法访问)。

它还支持在应用角色时检查的自定义规则,以便在请求时授予访问权限。

此库受yii2 rbac库的启发,但增加了将用户添加到组中以批量授予访问权限的可能性。

待办事项

  • 添加psr3可记录的提供者
  • 添加psr6可缓存的提供者
  • 添加psr16可缓存的提供者
  • 添加psr7兼容的可缓存的提供者
  • 添加默认的状态库

许可证

MIT(见许可证文件)。