sinemacula / laravel-authorization
Role-based access control and IAM-style policy evaluation for Laravel 12 and 13.
Package info
github.com/sinemacula/laravel-authorization
pkg:composer/sinemacula/laravel-authorization
Requires
- php: ^8.3
- ext-mbstring: *
- laravel/framework: ^12.40|^13.3
Requires (Dev)
- brianium/paratest: ^7.20
- doctrine/coding-standard: ^14.0
- friendsofphp/php-cs-fixer: ^3.95.19
- infection/infection: ^0.33.2
- larastan/larastan: ^3.9
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.2|^11.0
- phpbench/phpbench: ^1.6.1
- phpstan/extension-installer: ^1.4
- phpstan/phpdoc-parser: ^2.3
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-mockery: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.5
- sinemacula/coding-standards-laravel: ^1.7.1
- slevomat/coding-standard: ^8.28
- squizlabs/php_codesniffer: ^3.13.6 || ^4.0.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-25 01:12:45 UTC
README
Role-based access control and IAM-style policy evaluation for Laravel. Decides what a principal may do: roles and permissions granted to any Eloquent model, policy documents that allow or deny actions on resources under conditions, and one evaluator that combines them.
Drops into Laravel's existing authorisation machinery: the package is the authority of Laravel's Gate for every
permission it declares, so Gate::allows(), @can, can: middleware and $user->can() work unchanged and decide
through it. Adds role, permission and authorize route middleware, Blade directives, console commands and test
assertions.
Core Concept
| Concept | What it is |
|---|---|
| Principal | Who a decision is about: any Eloquent model that implements AuthorizableGrantee, usually your user model. |
| Role | A named set of permissions, granted as one. Roles can inherit from a parent role. |
| Permission | A named action, such as posts:update or posts.update, or a wildcard pattern of them, such as posts:*. |
| Policy | A JSON document of statements, each allowing or denying actions on resources, optionally under conditions. |
| Boundary | A policy set on a principal or an identity that caps what it may do, whatever its grants allow. |
| Guardrail | A policy set on a tenant or the platform that caps what anyone may do there, whatever their grants allow. |
| Session cap | A set of actions, a credential's scopes or the caller's, that bounds what one check may allow. |
| Machine | A principal that acts for no person, such as a service account, capped by its credential's scopes. |
| Decision | The answer to one check: allowed or not, with the reason and a trace of the statements that decided it. |
A check weighs the subject, the resource and its tenant first, then any explicit deny, then the caps (guardrails, permission boundaries and session caps), each of which must allow it, then an allow statement or a permission held. Anything left is denied. How a check is decided sets out the stages.
Installation
composer require sinemacula/laravel-authorization
Configuration
Publish the config and the migrations, then migrate:
php artisan vendor:publish --tag=authorization-config php artisan vendor:publish --tag=authorization-migrations php artisan migrate
A check that names no subject is about the user Laravel authenticated on its default guard. An application with
principals or tenants binds its own resolver, a class implementing
SineMacula\Laravel\Authorization\Contracts\SubjectResolver, in config/authorization.php:
'subject_resolver' => App\Authorization\CurrentSubject::class,
Booting never touches the database. Sync the permissions you declare in your deployment, after the migrations:
php artisan migrate --force php artisan authorization:sync
See Configuration for every option.
Usage
Give your user model the contract and the trait:
namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use SineMacula\Laravel\Authorization\Concerns\HasAuthorization; use SineMacula\Laravel\Authorization\Contracts\AuthorizableGrantee; class User extends Authenticatable implements AuthorizableGrantee { use HasAuthorization; }
Define permissions as a string-backed enum, and list it under permission_enums in config/authorization.php. Each
case's value is the permission name:
namespace App\Authorization; use SineMacula\Laravel\Authorization\Contracts\PermissionEnum; enum PostPermission: string implements PermissionEnum { case UPDATE = 'posts:update'; case DELETE = 'posts:delete'; }
Write them to the database with php artisan authorization:sync, then grant roles and permissions, and check them as
with any Laravel ability:
use Illuminate\Support\Facades\Gate; use SineMacula\Laravel\Authorization\Models\Role; Role::create(['name' => 'editor'])->givePermission('posts:update'); $user->assignRole('editor'); $user->givePermission('posts:delete', now()->addDay()); $user->hasRole('editor'); // true $user->hasPermission('posts:update'); // true, through the role Gate::forUser($user)->allows('posts:update');
Attach a policy to refine what the grants allow. An explicit deny wins over any role:
use SineMacula\Laravel\Authorization\Facades\Authorization; use SineMacula\Laravel\Authorization\Models\Policy; $user->attachPolicy(Policy::create([ 'name' => 'no-deletes-off-network', 'document' => [ 'statements' => [ [ 'effect' => 'deny', 'actions' => ['posts:delete'], 'conditions' => ['ip_not_in' => ['request.ip' => ['10.0.0.0/8', '192.168.0.0/16']]], ], ], ], ])); Authorization::for($user)->can('posts:delete', 'posts/42'); // false off the network
Guides
| Guide | Covers |
|---|---|
| Configuration | Publishing, the connection and table prefix, binding seams, every setting and its checks |
| Seams and the suite | Each contract, its default and its adapter under the suite, the middleware order |
| Roles, permissions and grants | The user model, declaring and syncing permissions, the management services, tenants |
| Checking access | The facade, Laravel's Gate and policy classes, middleware and Blade, bulk checks |
| Policies | How a check is decided, policy documents, role, inline and resource-based policies |
| Context, attributes and authentication requirements | The check's context, subject and resource attributes, authentication requirements |
| Boundaries, guardrails and session caps | Permission boundaries, guardrails, session caps and machines |
| Separation of duties | Mutually exclusive roles, the conflict report and enforcement |
| Break-glass | Emergency access: settings, activation, surrender, revocation and reporting |
| Delegated administration and grant hooks | Administration actions, acting contexts, grant hooks, the grant scope query |
| Query scoping | whereAuthorized(), declaring a model, what compiles, the model-level scope |
| Access reviews | Holders and holdings, expiring grants, paging, exports and usage tracking |
| Preview and explanations | What another subject may do, and why a check was decided as it was |
| Events | Every event, reading a change for an audit log, which decisions are reported |
| Caching | The resolution cache's tiers, keys and versions, invalidation and a failing store |
| Octane and long-lived workers | What is scoped to a request or job, your own bindings, long-running commands |
| Localisation | The denial and unauthenticated messages and their translation |
| Console commands | Every command, the configuration check, the diagnostic boot, authorization:why |
| Testing | Assertions, acting as a subject and the tenant tree test kit |
The contracts behind them, for maintainers and reviewers, are in docs/design.
Requirements
- PHP ^8.3 (extension: mbstring)
- Laravel ^12.40 || ^13.3
Testing
composer test # all suites in parallel (Paratest) composer test:coverage # all suites with clover coverage composer test:unit # unit suite only composer test:feature # feature suite only composer test:integration # integration suite only composer test:performance # performance budget suite (serial) composer test:mutation # scoped mutation gate composer test:mutation:full # full mutation suite (no thresholds) composer bench # PHPBench hot-path benchmarks composer check # static analysis + style composer format # format the codebase composer smells # advisory code smells (duplication, complexity)
The performance suite holds query, row and memory budgets in every run. Its wall-clock budgets are compared only when
code coverage is not being collected, since a coverage driver slows the code it measures: composer test:performance
enforces them, and a run with coverage still runs those tests but skips their timing comparisons.
Changelog
See CHANGELOG.md for a list of notable changes.
Contributing
Contributions are welcome. Please read CONTRIBUTING.md for guidelines on branching, commits, code quality, and pull requests.
Security
If you discover a security vulnerability, please report it responsibly. See SECURITY.md for the disclosure policy and contact details.
License
Licensed under the Apache License, Version 2.0.