ordain/delegation
Composer 安装命令:
composer require ordain/delegation
包简介
Scoped authority delegation for Laravel with native escalation prevention
README 文档
README
Scoped authority delegation for Laravel. Enforce hierarchical permission boundaries where authority flows downward—users delegate subsets of their own grants, never more. Native escalation prevention with spatie/laravel-permission integration.
The Problem
Traditional RBAC answers: "What can this user do?"
This package answers: "What can this user grant to others?"
Without delegation control, a team lead could assign admin roles, create unlimited users, or manage users outside their hierarchy. This package prevents that.
Features
- Hierarchical user management - Users only manage users they created
- Role & permission delegation - Control which roles/permissions users can assign
- User creation quotas - Limit how many users each manager can create
- Native escalation prevention - Cannot grant more than you have
- Root admin bypass - Configurable super-user override
- Comprehensive audit logging - Track all delegation actions
- Domain events - React to delegation changes
- Built-in caching - Optimized for performance
- Multi-guard support - Works with custom authentication guards
- Blade directives & route macros - Convenient view and routing helpers
- Artisan commands - CLI tools for management
- Octane compatible - Ready for high-performance deployments
Requirements
- PHP 8.2+
- Laravel 11.x or 12.x
- spatie/laravel-permission ^6.0
Installation
Install the package via Composer:
composer require ordain/delegation
Publish and run the migrations:
php artisan vendor:publish --tag=delegation-migrations php artisan migrate
Publish the configuration file:
php artisan vendor:publish --tag=delegation-config
Add the trait to your User model:
use Ordain\Delegation\Contracts\DelegatableUserInterface; use Ordain\Delegation\Traits\HasDelegation; class User extends Authenticatable implements DelegatableUserInterface { use HasDelegation; protected $fillable = [ // ... your fields 'can_manage_users', 'max_manageable_users', 'created_by_user_id', ]; }
Quick Start
Check Authorization
use Ordain\Delegation\Facades\Delegation; // Can this user assign a role to another user? if (Delegation::canAssignRole($delegator, $role, $target)) { Delegation::delegateRole($delegator, $target, $role); } // Can this user create new users? if (Delegation::canCreateUsers($user)) { // Create user... } // What roles can this user assign? $assignableRoles = Delegation::getAssignableRoles($user);
Set Delegation Scope
use Ordain\Delegation\Domain\ValueObjects\DelegationScope; // Define what a manager can delegate $scope = new DelegationScope( canManageUsers: true, maxManageableUsers: 10, assignableRoleIds: [1, 2, 3], assignablePermissionIds: [4, 5], ); Delegation::setDelegationScope($manager, $scope);
Protect Routes
// Using middleware Route::middleware('can.delegate')->group(function () { Route::post('/users', [UserController::class, 'store']); }); Route::middleware('can.assign.role:editor,moderator') ->post('/users/{user}/roles', [RoleController::class, 'store']); // Using route macros Route::post('/users', [UserController::class, 'store']) ->canDelegate(); Route::post('/users/{user}/roles', [RoleController::class, 'store']) ->canAssignRole(['editor', 'moderator']);
Blade Directives
@canDelegate <a href="{{ route('users.create') }}">Create User</a> @endCanDelegate @canAssignRole('admin') <option value="admin">Administrator</option> @endCanAssignRole
Documentation
| Documentation | Description |
|---|---|
| Installation | Detailed installation and setup guide |
| Configuration | All configuration options explained |
| Core Concepts | Understanding hierarchical delegation |
| Basic Usage | Common usage patterns |
| Advanced Usage | Batch operations, validation, caching |
| Middleware | Route protection middleware |
| Blade & Routes | Blade directives and route macros |
| Events | Domain events and listeners |
| Commands | Artisan console commands |
| Customization | Extending the package |
| API Reference | Complete method reference |
| Testing | Testing your implementation |
| Troubleshooting | Common issues and solutions |
Artisan Commands
# Interactive installation wizard php artisan delegation:install # Display user's delegation scope php artisan delegation:show {user} # Assign role via CLI php artisan delegation:assign {delegator} {target} {role} # Clear delegation cache php artisan delegation:cache-reset {user?} # Health check php artisan delegation:health
Testing
composer test
With coverage:
composer test-coverage
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
ordain/delegation 适用场景与选型建议
ordain/delegation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 92 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「laravel」 「delegation」 「hierarchy」 「permission」 「roles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ordain/delegation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ordain/delegation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ordain/delegation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Authenticate your website (not your users) with Google API. Can be used instead as Domain-Wide Delegation of Authority
XAJAX alternative, phery.js is a library in PHP that maps to all jQuery functions, DOM manipulation, meta arguments and serialization, seamless ajax integration, RESTful emulation, form submission and partial rendering views, plus its PSR-0 compatible
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel JWT auth service package
Inheritance-based, scoped authorization for Laravel: each role delegates a subset of its own permissions to its children, and revoking cascades down the tree.
统计信息
- 总下载量: 92
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05