A powerful Policy-Based Access Control (PBAC) system for Laravel. Combines RBAC, ABAC, and ACL into a unified, fine-grained permission system.

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/techgonia/pbac

0.0.1 2025-10-19 22:15 UTC

This package is auto-updated.

Last update: 2025-10-19 22:21:04 UTC


README

Tests License

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;

// Grant permission
PBACAccessControl::factory()
    ->allow()
    ->forUser($user)
    ->forResource(Post::class, $post->id)
    ->withAction('edit')
    ->create();

// Check permission
if ($user->can('edit', $post)) {
    // User can edit this post
}

๐Ÿ“š Documentation

Getting Started

Usage Guides

Technical Reference

๐Ÿ’ก 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