Download the PHP package awesome166/abac without Composer
On this page you can find all versions of the php package awesome166/abac. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download awesome166/abac
More information about awesome166/abac
Files in awesome166/abac
Package abac
Short Description ABAC (Attribute-Based Access Control plugin for Laravel
License MIT
Informations about the package abac
Laravel ABAC - Attribute-Based Access Control Package
A robust ABAC implementation for Laravel providing attribute-based permission management with caching and automatic permission recaching.
Features
- Attribute-based access control (ABAC)
- Permission caching for high performance
- Multiple permission types (on-off, read-write, crud)
- Account-based resource blocking
- Automatic permission recaching on changes
- Middleware and Policy integration
- Role-based inheritance with user overrides
Requirements
- PHP 8.1+
- Laravel 10+
- Database supporting JSON columns
Installation
- Run
composer require awesome166/abac
. - Publish config:
php artisan vendor:publish --tag="config"
. - Migrate:
php artisan migrate
.
Run the install command:
Publish everything
php artisan vendor:publish --provider="awesome166\abac\ABACServiceProvider" --tag="config,models,migrations,controllers,middleware,commands"
Publish individual components
php artisan vendor:publish --tag="config" # Config php artisan vendor:publish --tag="models" # Models php artisan vendor:publish --tag="migrations" # Migrations php artisan vendor:publish --tag="controllers" # Migrations
Configuration
Publish the config file (optional):
config/abac.php
:
Using Traits
- Default: Use
awesome166\abac\Traits\HasPermissions
directly in your models. - Customization: Publish the trait with
php artisan vendor:publish --tag=traits
and update the namespace.Model Setup
Add the HasPermissions
trait to your User model:
This trait provides permission management functionality including:
- Permission checking methods
- Role relationship definitions
- Permission caching logic
- Account status handling
Usage
Middleware
Protect routes with ABAC middleware:
Gates & Policies
Check permissions in controllers/policies:
Manual Checks
Permission Types
On/Off (on-off):
Read/Write (read-write):
CRUD (crud):
Recaching Permissions
When permissions change:
Account Status Handling
Accounts marked as inactive (is_active = false
) automatically receive:
- 403 Forbidden for all ABAC-protected routes
Permission Assignment Example
Models & Relationships
Key models included:
- PermissionCategory → Permission
- Role ↔ User (via UserRole)
- Account ↔ AssignedPermission
Automatic Recaching
Permissions are automatically recached when:
- User roles change
- Permissions are added/removed
- Account associations change
Additional Examples
Get User's Permissions Through Roles
This snippet retrieves all the permissions associated with the roles assigned to a user. It uses Eloquent's with
method to eager load the permissions
relationship on the roles
model.
Get All Assigned Permissions for an Account
This snippet retrieves all the permissions assigned to a specific account. It uses Eloquent's with
method to eager load the permission
relationship on the assignedPermissions
model.
Check Permission Assignments for a Resource
This snippet checks if a specific permission is assigned to an account by filtering the assignedPermissions
relationship based on the account_id
.
Auto-Assign Default Role & Account
This snippet automatically assigns new users to the default 'user' account type and 'Customer' role when they are created.
Create Admin User Example
Controller Authorization Example
License
MIT License - See LICENSE for details.