PHPackages                             filippo-toso/resource-permissions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. filippo-toso/resource-permissions

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

filippo-toso/resource-permissions
=================================

An highly opinionated polymorphic role / permission package

v1.2.0(2mo ago)0806↓50%[3 PRs](https://github.com/filippotoso/resource-permissions/pulls)MITPHPPHP ^8.2CI failing

Since Oct 18Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/filippotoso/resource-permissions)[ Packagist](https://packagist.org/packages/filippo-toso/resource-permissions)[ Docs](https://github.com/filippotoso/resource-permissions)[ GitHub Sponsors]()[ RSS](/packages/filippo-toso-resource-permissions/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (12)Versions (32)Used By (0)

An highly opinionated polymorphic role / permission package
===========================================================

[](#an-highly-opinionated-polymorphic-role--permission-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2a5795f6434998cb30802873cb104d01aec13de51ff731d9d12f80bc9d00110f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66696c6970706f746f736f2f7265736f757263652d7065726d697373696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/filippotoso/resource-permissions)[![GitHub Tests Action Status](https://camo.githubusercontent.com/1e5e79397f8ea2d352191bb8143160cfee3bf1342fe5797531ad0d5e9045606b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f66696c6970706f746f736f2f7265736f757263652d7065726d697373696f6e732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/filippotoso/resource-permissions/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/75ba0c8b1c0f8b74e4686efeb8679a3451293fc97b03358213577b63b6ccf540/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f66696c6970706f746f736f2f7265736f757263652d7065726d697373696f6e732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/filippotoso/resource-permissions/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require filippo-toso/resource-permissions
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="resource-permissions-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="resource-permissions-config"
```

This is the contents of the published config file:

```
use Carbon\Carbon;
use Illuminate\Support\Str;

return [
    /**
     * The name of the tables used by the package
     */
    'tables' => [
        'roles' => 'roles',
        'permissions' => 'permissions',
        'permission_role' => 'permission_role',
        'role_user' => 'role_user',
        'permission_user' => 'permission_user',
    ],

    'models' => [
        // @phpstan-ignore-next-line
        'user' => \App\Models\User::class, // Default user class
        'role' => \FilippoToso\ResourcePermissions\Models\Role::class,
        'permission' => \FilippoToso\ResourcePermissions\Models\Permission::class,
    ],

    // Use file finder in production, database finder in development
    'finder' => (env('APP_ENV') == 'production')
        ? \FilippoToso\ResourcePermissions\Finders\Strategies\FileFinder::class
        : \FilippoToso\ResourcePermissions\Finders\Strategies\DatabaseFinder::class,

    // The FileFinder will cache the results into files for a certain amount of time
    'cache' => [
        'folder' => Str::finish(storage_path('app/resource-permissions'), DIRECTORY_SEPARATOR),
        'ttl' => Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR * Carbon::HOURS_PER_DAY,
    ],
];
```

Usage
-----

[](#usage)

Add the `HasRolesAndPermissions` trait to your User model:

```
use FilippoToso\ResourcePermissions\Models\Concerns\HasRolesAndPermissions;

// ...

class User
{
    use HasRolesAndPermissions;

    // ...
}
```

Now you can create a user, a permission, and a role. Then assign the permission to the role and assign the role to the user.

```
$user = User::create([
    'name' => 'John Snow',
    'email' => 'john.snow@nightswatch.local',
    'password' => Hash::make('Ygritte'),
]);

$role = Role::create(['name' => 'lord-commander']);

$permission = Permission::create(['name' => 'lead']);

$role->assignPermission($permission);
$user->assignRole($role);

if ($user->hasPermission($permission)) {
    dump('I shall take no wife, hold no lands, father no children...');
}
```

You can also assign permissions directly to the user:

```
$user = User::create([
    'name' => 'John Snow',
    'email' => 'john.snow@nightswatch.local',
    'password' => Hash::make('Ygritte'),
]);

$permission = Permission::create(['name' => 'lead']);

$user->assignPermission($permission);

if ($user->hasPermission($permission)) {
    dump('I shall wear no crowns and win no glory...');
}
```

Both `hasRole()` and `hasPermission()` methods accept as first parameter:

- a role/permission Model instance
- the numeric key of the role/permission Model instance
- a string with the role/permission name
- a BackednEnum with the role/permission name
- an array with any of the above

When you assign a role or a permission to a user, you can pass a second `$resource` parameter. This is the uniqueness of this package. You can assign a role or a permission to a user relative to a specific Eloquent model. For instance a user can be assigned the `manager` role of an Organization and the `owner` role of a project.

This second parameter can be:

- a Model instance
- a ResourceData object (an internal representation of a resource)
- an array with a single element where the key is the Model class and the value is the numeric key of the model
- an array with any of the above

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Filippo Toso](https://github.com/filippotoso)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance85

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Recently: every ~67 days

Total

28

Last Release

79d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/55d89f2d44fb12225de2119994028ee69e36770bcf33c2b1ddf0d6672d28151b?d=identicon)[filippo.toso](/maintainers/filippo.toso)

---

Top Contributors

[![filippotoso](https://avatars.githubusercontent.com/u/26958813?v=4)](https://github.com/filippotoso "filippotoso (54 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laravelFilippo Tosoresource-permissions

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/filippo-toso-resource-permissions/health.svg)

```
[![Health](https://phpackages.com/badges/filippo-toso-resource-permissions/health.svg)](https://phpackages.com/packages/filippo-toso-resource-permissions)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[jeffgreco13/filament-breezy

A custom package for Filament with login flow, profile and teams support.

1.0k1.7M41](/packages/jeffgreco13-filament-breezy)[spatie/laravel-login-link

Quickly login to your local environment

4381.2M1](/packages/spatie-laravel-login-link)[ryangjchandler/laravel-cloudflare-turnstile

A simple package to help integrate Cloudflare Turnstile.

438896.6k2](/packages/ryangjchandler-laravel-cloudflare-turnstile)[spatie/laravel-passkeys

Use passkeys in your Laravel app

444494.4k16](/packages/spatie-laravel-passkeys)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
