lliure/permission 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lliure/permission

最新稳定版本:v1.1.0

Composer 安装命令:

composer require lliure/permission

包简介

Role-based permission system with scope and level support

README 文档

README

Role-based permission system with scope and level support for PHP 8.1+.

Install

composer require lliure/permission

Scope-based permissions (boolean)

Traditional scope model where each permission is an independent boolean flag.

1. Create a permission class

class CampaignPermission
{
    use \LlPermission\Permission;
}

2. Build the permission set

$permissions = (new \LlPermission\PermissionSet())
    ->setRole('staff');

$permissions->setClass(CampaignPermission::class)
    ->setScope('read')
    ->setScopes(['create', 'update']);

3. Check permissions

$campaign = new CampaignPermission($permissions);

$campaign->read();   // true
$campaign->create(); // true
$campaign->update(); // true
$campaign->delete(); // false (not granted)

The __call magic method enables checking any scope by calling it as a method.

Role hierarchy

The Permission trait includes built-in role checks:

$campaign->isDev();   // true if role === 'dev'
$campaign->isAdmin(); // true if role === 'dev' or 'admin'
$campaign->isUser();  // true if role === 'dev', 'admin', or 'user'

Users with role dev bypass all scope checks automatically.

Level-based permissions (hierarchical)

For systems where access levels are cumulative (e.g., view < edit < full).

1. Define the hierarchy

$permissions = (new \LlPermission\PermissionSet())
    ->setRole('operator')
    ->setLevelHierarchy(['none', 'view', 'edit', 'full']);

2. Assign levels per resource

$permissions->setClass('customer.360')->setLevel('edit');
$permissions->setClass('orders')->setLevel('view');
$permissions->setClass('audit')->setLevel('full');

setLevel('edit') automatically grants all lower levels (view and edit).

3. Check levels

$permissions->hasLevel('customer.360', 'view'); // true  (edit >= view)
$permissions->hasLevel('customer.360', 'edit'); // true
$permissions->hasLevel('customer.360', 'full'); // false (edit < full)

$permissions->hasLevel('orders', 'view');       // true
$permissions->hasLevel('orders', 'edit');       // false

$permissions->hasLevel('audit', 'full');        // true

Mixing scopes and levels

You can combine both approaches in the same PermissionSet:

$permissions = (new \LlPermission\PermissionSet())
    ->setRole('manager')
    ->setLevelHierarchy(['none', 'view', 'edit', 'full']);

// Level-based resource
$permissions->setClass('customer.360')->setLevel('edit');

// Scope-based class
$permissions->setClass(CampaignPermission::class)
    ->setScopes(['read', 'create', 'update']);

API

PermissionSet

MethodDescription
setRole(string $role): selfSet the user's role
getRole(): stringGet the current role
setLevelHierarchy(array $levels): selfDefine ordered levels for setLevel()
setClass(string $class): objectStart building permissions for a class/resource
getPermissions(string $class, string $scope): boolCheck if a scope is granted
hasLevel(string $class, string $level): boolCheck if a level is granted (alias for getPermissions)
getClassPermissions(string $class): arrayGet all scopes for a class
getClasses(): arrayList all registered classes/resources

Class builder (returned by setClass())

MethodDescription
setScope(string $scope): selfGrant a single scope
setScopes(array $scopes): selfGrant multiple scopes
setLevel(string $level): selfGrant a level and all lower levels

Permission trait

MethodDescription
isDev(): boolCheck if role is dev
isAdmin(): boolCheck if role is dev or admin
isUser(): boolCheck if role is dev, admin, or user
__call($name): boolCheck scope via dynamic method call

统计信息

  • 总下载量: 87
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固