v1.0.0 2023-04-12 19:15 UTC

This package is auto-updated.

Last update: 2024-09-12 22:40:17 UTC


README

描述

该库包含权限和角色组件,用于管理访问。

需求

  • 脚本语言:PHP:版本 7 或 8

安装

有几种可能的安装方式

Composer

  1. 需求

    需要安装 composer。更多信息请参阅:https://getcomposer.org

  2. 命令:移动到项目根路径

     cd "<project_root_path>"
    
  3. 命令:安装

     php composer.phar require liberty_code/role ["<version>"]
    
  4. 注意

    • 包含 vendor

      如果项目使用 composer,则必须包含 vendor

        require_once('<project_root_path>/vendor/autoload.php');
      
    • 配置

      安装命令允许在 composer 文件中添加

        {
            "require": {
                "liberty_code/role": "<version>"
            }
        }
      

包含

  1. 下载

    • 下载以下仓库。
    • 将其放置在仓库根路径。
  2. 包含源码

     require_once('<repository_root_path>/include/Include.php');
    

用法

主题

主题允许设计一个项目,其中可以检查权限以访问特定操作。

元素

  • 主题

    允许设计一个主题,其中可以检查权限启用状态。

权限规范

权限规范允许在特定访问系统中指定一组特定的权限键。

元素

  • PermSpec

    允许提供在特定访问系统中使用的权限键的范围。

  • StandardPermSpec

    扩展权限规范功能。包含提供权限键范围所需的所有信息。

示例

// Get permission specification
use liberty_code\role\permission\specification\standard\model\StandardPermSpec;
$permSpec = new StandardPermSpec(array(...));
...
// Get array of permission keys
$permSpec->getTabKey();
...

权限

权限是在特定访问系统中授权特定操作的具体可检查的权利。

元素

  • 权限

    允许设计一个权限,以授权特定操作。

  • PermissionCollection

    扩展主题功能。允许设计权限集合。允许检查指定的权限是否已启用。

  • PermissionFactory

    允许设计权限工厂,从指定配置中提供新的或指定的权限实例。

  • StandardPermissionFactory

    扩展权限工厂功能。提供权限实例。

  • 构建器

    构建器允许用权限填充权限集合。

  • PermSpecBuilder

    扩展构建器功能。使用权限规范实例来填充权限集合。

  • PermissionSubject

    扩展主题功能。允许设计一个权限主题,其中可以分配权限。

// Get permission factory
use liberty_code\role\permission\factory\standard\model\StandardPermissionFactory;
$permissionFactory = new StandardPermissionFactory();
...
// Get permission builder
use liberty_code\role\permission\build\specification\model\PermSpecBuilder;
$permissionBuilder = new PermSpecBuilder($permSpec, $permissionFactory);
...
// Get permission collection
use liberty_code\role\permission\model\DefaultPermissionCollection;
$permissionCollection = new DefaultPermissionCollection();
...
// Hydrate permission collection
$permissionBuilder->hydratePermissionCollection($permissionCollection);
...
foreach($permissionCollection->getTabPermissionKey() as $permissionKey) {
    echo($permissionCollection->getObjPermission($permissionKey)->getStrPermissionKey() .'<br />');
}
/**
 * Show: 
 * Permission key 1
 * ...
 * Permission key N
 */
...

权限主题

权限主题允许设计一个主题,其中可以分配权限,以访问特定操作。

元素

  • PermissionSubject

    扩展主题功能。允许设计一个权限主题,其中可以分配权限。

// Get subject
use liberty_code\role\permission\subject\model\DefaultPermissionSubject;
$subject = new DefaultPermissionSubject(
    $permissionCollection
);
...
// Check specific permission enabled, for subject
$subject->checkPermissionEnable(...'string permission key');
...

角色

角色允许设计一个命名权限组。

元素

  • 角色

    扩展权限主题功能。允许设计一个角色,它是一个命名权限组,可以检查指定的权限是否已启用。

  • RoleCollection

    扩展主题功能。允许设计角色集合。允许检查在指定的至少一个角色上是否启用了指定的权限。

  • RoleFactory

    允许设计角色工厂,从指定配置中提供新的或指定的角色实例。

  • StandardRoleFactory

    扩展角色工厂功能。提供角色实例。

  • 构建器

    构建器允许用角色填充角色集合。

// Get role factory
use liberty_code\role\role\factory\standard\model\StandardRoleFactory;
$roleFactory = new StandardRoleFactory();
...
// Get role builder
use liberty_code\role\role\build\model\DefaultBuilder;
$roleBuilder = new DefaultBuilder(
    $roleFactory, 
    array(
        $permissionBuilder
    )
);
...
// Get role collection
use liberty_code\role\role\model\DefaultRoleCollection;
$roleCollection = new DefaultRoleCollection();
...
// Hydrate role collection
$roleBuilder->hydrateRoleCollection($roleCollection);
...
foreach($roleCollection->getTabRoleName() as $roleName) {
    echo($roleCollection->getObjRole($roleName)->getStrRoleName() .'<br />');
}
/**
 * Show: 
 * Role name 1
 * ...
 * Role name N
 */
...

角色主题

角色主题允许设计一个主题,其中可以分配角色,以访问特定操作。

元素

  • RoleSubject

    扩展主题功能。允许设计一个角色主题,其中可以分配角色。

  • RolePermissionSubject

    扩展权限主题功能。允许设计一个角色权限主题,其中可以分配角色。

// Get subject
use liberty_code\role\role\subject\model\DefaultRolePermissionSubject;
$subject = new DefaultRolePermissionSubject(
    $permissionCollection,
    $roleCollection
);
...
// Check specific permission enabled, for subject
$subject->checkPermissionEnable(...'string permission key');
...
// Check specific role exists, for subject
$subject->checkRoleExists(...'string role name');
...