techgonia/pbac
Composer 安装命令:
composer require techgonia/pbac
包简介
A powerful Policy-Based Access Control (PBAC) system for Laravel. Combines RBAC, ABAC, and ACL into a unified, fine-grained permission system.
README 文档
README
A powerful, flexible, and Policy-Based Access Control (PBAC) system for Laravel 1+. Combines the best of RBAC (Role-Based), ABAC (Attribute-Based), and ACL (Access Control List) into a unified, fine-grained permission system.
✨ Features
- Fine-Grained Permissions - Control access at user, group, team, and resource levels
- Deny-First Security - Explicit deny rules always override allow rules
- Flexible Targeting - Apply rules to individual users, groups, teams, or any combination
- Priority-Based Rules - Control rule evaluation order with priority levels
- Attribute-Based Conditions - Dynamic permissions based on runtime attributes (IP, user level, resource state)
- High Performance - Optimized queries with caching support
- Laravel Integration - Seamless integration with Laravel's Gate and Blade directives
- Super Admin Bypass - Built-in super admin support
- 100% Test Coverage - Comprehensive test suite with 212 tests
📋 Requirements
- PHP 8.1 or higher
- Laravel 11.0 or 12.0
- Database: MySQL 8.0+, PostgreSQL 12+, or SQLite 3.35+
Quick Start
# Install via Composer composer require techgonia/pbac # Publish configuration and migrations php artisan vendor:publish --tag="pbac-config" php artisan vendor:publish --tag="pbac-migrations" # Run migrations php artisan migrate
Add Traits to Your User Model
use Pbac\Traits\HasPbacAccessControl; use Pbac\Traits\HasPbacGroups; use Pbac\Traits\HasPbacTeams; class User extends Authenticatable { use HasPbacAccessControl, HasPbacGroups, HasPbacTeams; }
Basic Example
use Pbac\Models\PBACAccessControl; use Pbac\Facades\Pbac; // Grant permission PBACAccessControl::factory() ->allow() ->forUser($user) ->forResource(Post::class, $post->id) ->withAction('edit') ->create(); // Or use the Facade Pbac::allow() ->forUser($user) ->forResource(Post::class, $post->id) ->withAction('edit') ->create(); // Check permission if ($user->can('edit', $post)) { // User can edit this post } // Or use the Facade if (Pbac::can($user, 'edit', $post)) { // User can edit this post }
📚 Documentation
Getting Started
- Installation Guide - Step-by-step installation instructions
- Overview - What is PBAC and why use it
- Core Concepts - Understanding targets, resources, actions, and rules
Usage Guides
- Basic Usage - Creating and checking permissions
- Facade Usage - Using the Pbac facade (recommended)
- Use Cases - Real-world application patterns and examples
- Configuration - Complete configuration reference
Technical Reference
- Architecture - Internal architecture and design decisions
- API Reference - Complete API documentation
💡 Core Concepts
The PBAC Model
PBAC uses a rule-based system where each rule defines:
- Target: Who (user, group, team)
- Resource: What (post, file, setting, user, impersonation)
- Action: How (view, edit, delete, custom actions)
- Effect: Allow or Deny
- Conditions(optional): When (IP restrictions, attribute checks)
Security Model: Deny-First
Deny rules ALWAYS override allow rules, regardless of priority:
// Even with high priority allow... PBACAccessControl::factory()->allow()->withPriority(1000)->create(); // ...a low priority deny wins PBACAccessControl::factory()->deny()->withPriority(1)->create(); // Result: Access DENIED (secure by default)
🎯 Common Use Cases
1. Group-Based Permissions
$editors = PBACAccessGroup::create(['name' => 'Editors']); $user->groups()->attach($editors->id); PBACAccessControl::factory() ->allow() ->forGroup($editors) ->forResource(Post::class, null) // All posts ->withAction(['view', 'edit', 'publish']) ->create();
2. IP-Based Restrictions
PBACAccessControl::factory() ->allow() ->forUser($admin) ->forResource(AdminPanel::class, null) ->withAction('access') ->create([ 'extras' => ['allowed_ips' => ['192.168.1.0/24']] ]);
3. Attribute-Based Access
PBACAccessControl::factory() ->allow() ->forUser($user) ->forResource(Post::class, null) ->withAction('edit') ->create([ 'extras' => [ 'requires_attribute_value' => ['status' => 'draft'] ] ]);
4. Team Isolation
$team = PBACAccessTeam::create(['name' => 'Team Alpha']); $user->teams()->attach($team->id); PBACAccessControl::factory() ->allow() ->forTeam($team) ->forResource(Document::class, null) ->withAction('*') ->create();
🔥 Advanced Features
Super Admin Bypass
$user->is_super_admin = true; $user->can('anything', $anything); // always true
Laravel Gate Integration
Gate::allows('edit', $post); Gate::authorize('publish', $post);
Blade Directives
@pbacCan('edit', $post) <button>Edit</button> @endpbacCan
Factory Helpers
PBACAccessControl::factory() ->allow() // Set effect ->forUser($user) // Set target ->forResource(Post::class, $id) // Set resource ->withAction(['view', 'edit']) // Set actions ->withPriority(10) // Set priority ->create(['extras' => [...]]); // Add conditions
🧪 Testing
# Run all tests ./vendor/bin/phpunit # Run with Pest ./vendor/bin/pest # Test coverage ./vendor/bin/phpunit --coverage-html coverage
Test Suite: 212 tests
- 133 Unit tests
- 70 Integration tests
- 68 Regression tests
🤝 Contributing
Contributions welcome! Please see CONTRIBUTING.md.
📝 License
MIT License - see LICENSE.md
🙏 Credits
Built with ❤️ for Laravel developers
techgonia/pbac 适用场景与选型建议
techgonia/pbac 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「security」 「acl」 「laravel」 「Policy」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 techgonia/pbac 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 techgonia/pbac 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 techgonia/pbac 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
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.
Provide a way to secure accesses to all routes of an symfony application.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
It's a barebone security class written on PHP
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-19