Associate users with permissions and roles
|
If you want to quickly add authentication and authorization to Laravel projects, feel free to check Auth0's Laravel SDK and free plan at https://auth0.com/overview. |
, (*1)
This package allows you to manage user permissions and roles in a database., (*2)
Once installed you can do stuff like this:, (*3)
// Adding permissions to a user
$user->givePermissionTo('edit articles');
// Adding permissions via a role
$user->assignRole('writer');
$role->givePermissionTo('edit articles');
If you're using multiple guards we've got you covered as well. Every guard will have its own set of permissions and roles that can be assigned to the guard's users. Read about it in the using multiple guards section of the readme., (*4)
Because all permissions will be registered on Laravel's gate, you can check if a user has a permission with Laravel's default can
function:, (*5)
$user->can('edit articles');
Spatie is a web design agency in Antwerp, Belgium. You'll find an overview of all
our open source projects on our website., (*6)
Installation
Laravel
This package can be used in Laravel 5.4 or higher. If you are using an older version of Laravel, take a look at the v1 branch of this package., (*7)
You can install the package via composer:, (*8)
``` bash
composer require spatie/laravel-permission, (*9)
The service provider will automatically get registered. Or you may manually add the service provider in your `config/app.php` file:
```php
'providers' => [
// ...
Spatie\Permission\PermissionServiceProvider::class,
];
You can publish the migration with:, (*10)
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"
If you're using UUIDs or GUIDs for your User
models you can update the create_permission_tables.php
migration and replace $table->unsignedBigInteger($columnNames['model_morph_key'])
with $table->uuid($columnNames['model_morph_key'])
.
For consistency, you can also update the package configuration file to use the model_uuid
column name instead of the default model_id
column., (*11)
After the migration has been published you can create the role- and permission-tables by running the migrations:, (*12)
php artisan migrate
You can publish the config file with:, (*13)
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
When published, the config/permission.php
config file contains:, (*14)
return [
'models' => [
/*
* When using the "HasPermissions" trait from this package, we need to know which
* Eloquent model should be used to retrieve your permissions. Of course, it
* is often just the "Permission" model but you may use whatever you like.
*
* The model you want to use as a Permission model needs to implement the
* `Spatie\Permission\Contracts\Permission` contract.
*/
'permission' => Spatie\Permission\Models\Permission::class,
/*
* When using the "HasRoles" trait from this package, we need to know which
* Eloquent model should be used to retrieve your roles. Of course, it
* is often just the "Role" model but you may use whatever you like.
*
* The model you want to use as a Role model needs to implement the
* `Spatie\Permission\Contracts\Role` contract.
*/
'role' => Spatie\Permission\Models\Role::class,
],
'table_names' => [
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your roles. We have chosen a basic
* default value but you may easily change it to any table you like.
*/
'roles' => 'roles',
/*
* When using the "HasPermissions" trait from this package, we need to know which
* table should be used to retrieve your permissions. We have chosen a basic
* default value but you may easily change it to any table you like.
*/
'permissions' => 'permissions',
/*
* When using the "HasPermissions" trait from this package, we need to know which
* table should be used to retrieve your models permissions. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'model_has_permissions' => 'model_has_permissions',
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your models roles. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'model_has_roles' => 'model_has_roles',
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your roles permissions. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'role_has_permissions' => 'role_has_permissions',
],
'column_names' => [
/*
* Change this if you want to name the related model primary key other than
* `model_id`.
*
* For example, this would be nice if your primary keys are all UUIDs. In
* that case, name this `model_uuid`.
*/
'model_morph_key' => 'model_id',
],
/*
* When set to true, the required permission/role names are added to the exception
* message. This could be considered an information leak in some contexts, so
* the default setting is false here for optimum safety.
*/
'display_permission_in_exception' => false,
'cache' => [
/*
* By default all permissions are cached for 24 hours to speed up performance.
* When permissions or roles are updated the cache is flushed automatically.
*/
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
/*
* The cache key used to store all permissions.
*/
'key' => 'spatie.permission.cache',
/*
* When checking for a permission against a model by passing a Permission
* instance to the check, this key determines what attribute on the
* Permissions model is used to cache against.
*
* Ideally, this should match your preferred way of checking permissions, eg:
* `$user->can('view-posts')` would be 'name'.
*/
'model_key' => 'name',
/*
* You may optionally indicate a specific cache driver to use for permission and
* role caching using any of the `store` drivers listed in the cache.php config
* file. Using 'default' here means to use the `default` set in cache.php.
*/
'store' => 'default',
],
];
Lumen
You can install the package via Composer:, (*15)
``` bash
composer require spatie/laravel-permission, (*16)
Copy the required files:
```bash
mkdir -p config
cp vendor/spatie/laravel-permission/config/permission.php config/permission.php
cp vendor/spatie/laravel-permission/database/migrations/create_permission_tables.php.stub database/migrations/2018_01_01_000000_create_permission_tables.php
You will also need to create another configuration file at config/auth.php
. Get it on the Laravel repository or just run the following command:, (*17)
curl -Ls https://raw.githubusercontent.com/laravel/lumen-framework/5.7/config/auth.php -o config/auth.php
Then, in bootstrap/app.php
, register the middlewares:, (*18)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'permission' => Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role' => Spatie\Permission\Middlewares\RoleMiddleware::class,
]);
As well as the config file, service provider, and cache alias:, (*19)
$app->configure('permission');
$app->alias('cache', \Illuminate\Cache\CacheManager::class); // if you don't have this already
$app->register(Spatie\Permission\PermissionServiceProvider::class);
Now, run your migrations:, (*20)
php artisan migrate
Usage
First, add the Spatie\Permission\Traits\HasRoles
trait to your User
model(s):, (*21)
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
- note that if you need to use
HasRoles
trait with another model ex.Page
you will also need to add protected $guard_name = 'web';
as well to that model or you would get an error
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;
class Page extends Model
{
use HasRoles;
protected $guard_name = 'web'; // or whatever guard you want to use
// ...
}
This package allows for users to be associated with permissions and roles. Every role is associated with multiple permissions.
A Role
and a Permission
are regular Eloquent models. They require a name
and can be created like this:, (*22)
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);
A permission can be assigned to a role using 1 of these methods:, (*23)
$role->givePermissionTo($permission);
$permission->assignRole($role);
Multiple permissions can be synced to a role using 1 of these methods:, (*24)
$role->syncPermissions($permissions);
$permission->syncRoles($roles);
A permission can be removed from a role using 1 of these methods:, (*25)
$role->revokePermissionTo($permission);
$permission->removeRole($role);
If you're using multiple guards the guard_name
attribute needs to be set as well. Read about it in the using multiple guards section of the readme., (*26)
The HasRoles
trait adds Eloquent relationships to your models, which can be accessed directly or used as a base query:, (*27)
// get a list of all permissions directly assigned to the user
$permissionNames = $user->getPermissionNames(); // collection of name strings
$permissions = $user->permissions; // collection of permission objects
// get all permissions for the user, either directly, or from roles, or from both
$permissions = $user->getDirectPermissions();
$permissions = $user->getPermissionsViaRoles();
$permissions = $user->getAllPermissions();
// get the names of the user's roles
$roles = $user->getRoleNames(); // Returns a collection
The HasRoles
trait also adds a role
scope to your models to scope the query to certain roles or permissions:, (*28)
$users = User::role('writer')->get(); // Returns only users with the role 'writer'
The role
scope can accept a string, a \Spatie\Permission\Models\Role
object or an \Illuminate\Support\Collection
object., (*29)
The same trait also adds a scope to only get users that have a certain permission., (*30)
$users = User::permission('edit articles')->get(); // Returns only users with the permission 'edit articles' (inherited or directly)
The scope can accept a string, a \Spatie\Permission\Models\Permission
object or an \Illuminate\Support\Collection
object., (*31)
Using "direct" permissions (see below to use both roles and permissions)
A permission can be given to any user:, (*32)
$user->givePermissionTo('edit articles');
// You can also give multiple permission at once
$user->givePermissionTo('edit articles', 'delete articles');
// You may also pass an array
$user->givePermissionTo(['edit articles', 'delete articles']);
A permission can be revoked from a user:, (*33)
$user->revokePermissionTo('edit articles');
Or revoke & add new permissions in one go:, (*34)
$user->syncPermissions(['edit articles', 'delete articles']);
You can check if a user has a permission:, (*35)
$user->hasPermissionTo('edit articles');
Or you may pass an integer representing the permission id, (*36)
$user->hasPermissionTo('1');
$user->hasPermissionTo(Permission::find(1)->id);
$user->hasPermissionTo($somePermission->id);
You can check if a user has Any of an array of permissions:, (*37)
$user->hasAnyPermission(['edit articles', 'publish articles', 'unpublish articles']);
...or if a user has All of an array of permissions:, (*38)
$user->hasAllPermissions(['edit articles', 'publish articles', 'unpublish articles']);
You may also pass integers to lookup by permission id, (*39)
$user->hasAnyPermission(['edit articles', 1, 5]);
Saved permissions will be registered with the Illuminate\Auth\Access\Gate
class for the default guard. So you can
check if a user has a permission with Laravel's default can
function:, (*40)
$user->can('edit articles');
Using permissions via roles
A role can be assigned to any user:, (*41)
$user->assignRole('writer');
// You can also assign multiple roles at once
$user->assignRole('writer', 'admin');
// or as an array
$user->assignRole(['writer', 'admin']);
A role can be removed from a user:, (*42)
$user->removeRole('writer');
Roles can also be synced:, (*43)
// All current roles will be removed from the user and replaced by the array given
$user->syncRoles(['writer', 'admin']);
You can determine if a user has a certain role:, (*44)
$user->hasRole('writer');
You can also determine if a user has any of a given list of roles:, (*45)
$user->hasAnyRole(Role::all());
You can also determine if a user has all of a given list of roles:, (*46)
$user->hasAllRoles(Role::all());
The assignRole
, hasRole
, hasAnyRole
, hasAllRoles
and removeRole
functions can accept a
string, a \Spatie\Permission\Models\Role
object or an \Illuminate\Support\Collection
object., (*47)
A permission can be given to a role:, (*48)
$role->givePermissionTo('edit articles');
You can determine if a role has a certain permission:, (*49)
$role->hasPermissionTo('edit articles');
A permission can be revoked from a role:, (*50)
$role->revokePermissionTo('edit articles');
The givePermissionTo
and revokePermissionTo
functions can accept a
string or a Spatie\Permission\Models\Permission
object., (*51)
Permissions are inherited from roles automatically.
Additionally, individual permissions can be assigned to the user too.
For instance:, (*52)
$role = Role::findByName('writer');
$role->givePermissionTo('edit articles');
$user->assignRole('writer');
$user->givePermissionTo('delete articles');
In the above example, a role is given permission to edit articles and this role is assigned to a user.
Now the user can edit articles and additionally delete articles. The permission of 'delete articles' is the user's direct permission because it is assigned directly to them.
When we call $user->hasDirectPermission('delete articles')
it returns true
,
but false
for $user->hasDirectPermission('edit articles')
., (*53)
This method is useful if one builds a form for setting permissions for roles and users in an application and wants to restrict or change inherited permissions of roles of the user, i.e. allowing to change only direct permissions of the user., (*54)
You can list all of these permissions:, (*55)
// Direct permissions
$user->getDirectPermissions() // Or $user->permissions;
// Permissions inherited from the user's roles
$user->getPermissionsViaRoles();
// All permissions which apply on the user (inherited and direct)
$user->getAllPermissions();
All these responses are collections of Spatie\Permission\Models\Permission
objects., (*56)
If we follow the previous example, the first response will be a collection with the delete article
permission and
the second will be a collection with the edit article
permission and the third will contain both., (*57)
Using Blade directives
This package also adds Blade directives to verify whether the currently logged in user has all or any of a given list of roles., (*58)
Optionally you can pass in the guard
that the check will be performed on as a second argument., (*59)
Blade and Roles
Check for a specific role:, (*60)
@role('writer')
I am a writer!
@else
I am not a writer...
@endrole
is the same as, (*61)
@hasrole('writer')
I am a writer!
@else
I am not a writer...
@endhasrole
Check for any role in a list:, (*62)
@hasanyrole($collectionOfRoles)
I have one or more of these roles!
@else
I have none of these roles...
@endhasanyrole
// or
@hasanyrole('writer|admin')
I am either a writer or an admin or both!
@else
I have none of these roles...
@endhasanyrole
Check for all roles:, (*63)
@hasallroles($collectionOfRoles)
I have all of these roles!
@else
I do not have all of these roles...
@endhasallroles
// or
@hasallroles('writer|admin')
I am both a writer and an admin!
@else
I do not have all of these roles...
@endhasallroles
Alternatively, @unlessrole
gives the reverse for checking a singular role, like this:, (*64)
@unlessrole('does not have this role')
I do not have the role
@else
I do have the role
@endunlessrole
Blade and Permissions
This package doesn't add any permission-specific Blade directives. Instead, use Laravel's native @can
directive to check if a user has a certain permission., (*65)
@can('edit articles')
//
@endcan
or, (*66)
@if(auth()->user()->can('edit articles') && $some_other_condition)
//
@endif
Defining a Super-Admin
We strongly recommend that a Super-Admin be handled by setting a global Gate::before
rule which checks for the desired role., (*67)
Then you can implement the best-practice of primarily using permission-based controls throughout your app, without always having to check for "is this a super-admin" everywhere., (*68)
See this wiki article on Defining a Super-Admin Gate rule in your app., (*69)
Best Practices -- roles vs permissions
It is generally best to code your app around permissions
only. That way you can always use the native Laravel @can
and can()
directives everywhere in your app., (*70)
Roles can still be used to group permissions for easy assignment, and you can still use the role-based helper methods if truly necessary. But most app-related logic can usually be best controlled using the can
methods, which allows Laravel's Gate layer to do all the heavy lifting., (*71)
Using multiple guards
When using the default Laravel auth configuration all of the above methods will work out of the box, no extra configuration required., (*72)
However, when using multiple guards they will act like namespaces for your permissions and roles. Meaning every guard has its own set of permissions and roles that can be assigned to their user model., (*73)
Using permissions and roles with multiple guards
When creating new permissions and roles, if no guard is specified, then the first defined guard in auth.guards
config array will be used. When creating permissions and roles for specific guards you'll have to specify their guard_name
on the model:, (*74)
// Create a superadmin role for the admin users
$role = Role::create(['guard_name' => 'admin', 'name' => 'superadmin']);
// Define a `publish articles` permission for the admin users belonging to the admin guard
$permission = Permission::create(['guard_name' => 'admin', 'name' => 'publish articles']);
// Define a *different* `publish articles` permission for the regular users belonging to the web guard
$permission = Permission::create(['guard_name' => 'web', 'name' => 'publish articles']);
To check if a user has permission for a specific guard:, (*75)
$user->hasPermissionTo('publish articles', 'admin');
Note: When determining whether a role/permission is valid on a given model, it chooses the guard in this order: first the $guard_name
property of the model; then the guard in the config (through a provider); then the first-defined guard in the auth.guards
config array; then the auth.defaults.guard
config., (*76)
Note: When using other than the default web
guard, you will need to declare which guard_name
you wish each model to use by setting the $guard_name
property in your model. One per model is simplest., (*77)
Note: If your app uses only a single guard, but is not web
then change the order of your listed guards in your config/auth.php
to list your primary guard as the default and as the first in the list of defined guards., (*78)
Assigning permissions and roles to guard users
You can use the same methods to assign permissions and roles to users as described above in using permissions via roles. Just make sure the guard_name
on the permission or role matches the guard of the user, otherwise a GuardDoesNotMatch
exception will be thrown., (*79)
Using blade directives with multiple guards
You can use all of the blade directives listed in using blade directives by passing in the guard you wish to use as the second argument to the directive:, (*80)
@role('super-admin', 'admin')
I am a super-admin!
@else
I am not a super-admin...
@endrole
Using a middleware
This package comes with RoleMiddleware
, PermissionMiddleware
and RoleOrPermissionMiddleware
middleware. You can add them inside your app/Http/Kernel.php
file., (*81)
protected $routeMiddleware = [
// ...
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];
Then you can protect your routes using middleware rules:, (*82)
Route::group(['middleware' => ['role:super-admin']], function () {
//
});
Route::group(['middleware' => ['permission:publish articles']], function () {
//
});
Route::group(['middleware' => ['role:super-admin','permission:publish articles']], function () {
//
});
Route::group(['middleware' => ['role_or_permission:super-admin']], function () {
//
});
Route::group(['middleware' => ['role_or_permission:publish articles']], function () {
//
});
Alternatively, you can separate multiple roles or permission with a |
(pipe) character:, (*83)
Route::group(['middleware' => ['role:super-admin|writer']], function () {
//
});
Route::group(['middleware' => ['permission:publish articles|edit articles']], function () {
//
});
Route::group(['middleware' => ['role_or_permission:super-admin|edit articles']], function () {
//
});
You can protect your controllers similarly, by setting desired middleware in the constructor:, (*84)
public function __construct()
{
$this->middleware(['role:super-admin','permission:publish articles|edit articles']);
}
public function __construct()
{
$this->middleware(['role_or_permission:super-admin|edit articles']);
}
Catching role and permission failures
If you want to override the default 403
response, you can catch the UnauthorizedException
using your app's exception handler:, (*85)
public function render($request, Exception $exception)
{
if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
// Code here ...
}
return parent::render($request, $exception);
}
Using artisan commands
You can create a role or permission from a console with artisan commands., (*86)
php artisan permission:create-role writer
php artisan permission:create-permission "edit articles"
When creating permissions/roles for specific guards you can specify the guard names as a second argument:, (*87)
php artisan permission:create-role writer web
php artisan permission:create-permission "edit articles" web
When creating roles you can also create and link permissions at the same time:, (*88)
php artisan permission:create-role writer web "create articles|edit articles"
Unit Testing
In your application's tests, if you are not seeding roles and permissions as part of your test setUp()
then you may run into a chicken/egg situation where roles and permissions aren't registered with the gate (because your tests create them after that gate registration is done). Working around this is simple: In your tests simply add a setUp()
instruction to re-register the permissions, like this:, (*89)
public function setUp()
{
// first include all the normal setUp operations
parent::setUp();
// now re-register all the roles and permissions
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();
}
Database Seeding
You may discover that it is best to flush this package's cache before seeding, to avoid cache conflict errors. This can be done directly in a seeder class. Here is a sample seeder, which first clears the cache, creates permissions and then assigns permissions to roles (the order of these steps is intentional):, (*90)
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class RolesAndPermissionsSeeder extends Seeder
{
public function run()
{
// Reset cached roles and permissions
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
// create permissions
Permission::create(['name' => 'edit articles']);
Permission::create(['name' => 'delete articles']);
Permission::create(['name' => 'publish articles']);
Permission::create(['name' => 'unpublish articles']);
// create roles and assign created permissions
// this can be done as separate statements
$role = Role::create(['name' => 'writer']);
$role->givePermissionTo('edit articles');
// or may be done by chaining
$role = Role::create(['name' => 'moderator'])
->givePermissionTo(['publish articles', 'unpublish articles']);
$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());
}
}
Extending
If you need to EXTEND the existing Role
or Permission
models note that:, (*91)
- Your
Role
model needs to extend the Spatie\Permission\Models\Role
model
- Your
Permission
model needs to extend the Spatie\Permission\Models\Permission
model
If you need to REPLACE the existing Role
or Permission
models you need to keep the
following things in mind:, (*92)
- Your
Role
model needs to implement the Spatie\Permission\Contracts\Role
contract
- Your
Permission
model needs to implement the Spatie\Permission\Contracts\Permission
contract
In BOTH cases, whether extending or replacing, you will need to specify your new models in the configuration. To do this you must update the models.role
and models.permission
values in the configuration file after publishing the configuration with this command:, (*93)
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
Cache
Role and Permission data are cached to speed up performance., (*94)
While we recommend not changing the cache "key" name, if you wish to alter the expiration time you may do so in the config/permission.php
file, in the cache
array. Note that as of v2.26.0 the cache
entry here is now an array, and expiration_time
is a sub-array entry., (*95)
When you use the built-in functions for manipulating roles and permissions, the cache is automatically reset for you, and relations are automatically reloaded for the current model record:, (*96)
$user->assignRole('writer');
$user->removeRole('writer');
$user->syncRoles(params);
$role->givePermissionTo('edit articles');
$role->revokePermissionTo('edit articles');
$role->syncPermissions(params);
$permission->assignRole('writer');
$permission->removeRole('writer');
$permission->syncRoles(params);
HOWEVER, if you manipulate permission/role data directly in the database instead of calling the supplied methods, then you will not see the changes reflected in the application unless you manually reset the cache., (*97)
Manual cache reset
To manually reset the cache for this package, you can run the following in your app code:, (*98)
app()->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
Or you can use an Artisan command:, (*99)
php artisan permission:cache-reset
Cache Identifier
TIP: If you are leveraging a caching service such as redis
or memcached
and there are other sites
running on your server, you could run into cache clashes between apps. It is prudent to set your own
cache prefix
in Laravel's /config/cache.php
to something unique for each application.
This will prevent other applications from accidentally using/changing your cached data., (*100)
Need a UI?
The package doesn't come with any screens out of the box, you should build that yourself. Here are some options to get you started:, (*101)
Testing
bash
composer test
, (*106)
Upgrading
If you're upgrading from v1 to v2, @fabricecw prepared a gist which may make your data migration easier.
You will also need to remove your old laravel-permission.php
config file and publish the new one permission.php
, and edit accordingly., (*107)
Changelog
Please see CHANGELOG for more information what has changed recently., (*108)
Contributing
Please see CONTRIBUTING for details., (*109)
Security
If you discover any security-related issues, please email freek@spatie.be instead of using the issue tracker., (*110)
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*111)
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*112)
We publish all received postcards on our company website., (*113)
Credits
This package is heavily based on Jeffrey Way's awesome Laracasts lessons
on permissions and roles. His original code
can be found in this repo on GitHub., (*114)
Special thanks to Alex Vanderbist who greatly helped with v2
, and to Chris Brown for his longtime support helping us maintain the package., (*115)
Alternatives
Support us
Spatie is a web design agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*116)
Does your business depend on our contributions? Reach out and support us on Patreon.
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*117)
License
The MIT License (MIT). Please see License File for more information., (*118)