Laravel permission, role based rights
, (*1)
Contents
Key features
- You have roles and permissions. Permissions can be attached to a role, and roles can be attached to a single user, but also you can attach or detach permission/permissions from a specific user.
- Permissions and roles are stored in the cache, editing them automatically refreshes it. We use cache tags, so regular file or database cache drivers doesn't work, please use memcached instead.
- Inspired by Zizaco/entrust
- Please note, the package is under development, only tested with Laravel 5.2.
Installation
Add the following line to your composer.json (Laravel 5.4 and below):, (*2)
"xdroidteam/xtrust": "0.1.*"
Add the following line to your composer.json (Laravel 5.5):, (*3)
"xdroidteam/xtrust": "0.2.*"
Then run composer update
., (*4)
or you can run the composer require
command from your terminal:, (*5)
composer require xdroidteam/xtrust
Then in your config/app.php
add to the providers
array (not needed after Laravel 5.5):, (*6)
XdroidTeam\XTrust\XTrustServiceProvider::class,
and to aliases
array (not needed after Laravel 5.5):, (*7)
'XTrust' => XdroidTeam\XTrust\XTrust::class,
If you are going to use Middleware (requires Laravel 5.1 or later) you also need to add, (*8)
'permission' => \XdroidTeam\XTrust\Middleware\XTrustPermissionMiddleware::class,
to routeMiddleware
array in app/Http/Kernel.php
., (*9)
Migration
Deploy migration file:, (*10)
php artisan vendor:publish --tag=xdroidteam-xtrust
You may now run it with the artisan migrate command:, (*11)
php artisan migrate
Models
Role
Create a Role model inside app/models/Role.php
using the following example:, (*12)
<?php namespace App\Models;
use XdroidTeam\XTrust\Models\XTrustRole;
class Role extends XTrustRole
{
...
}
Permission
Create a Permission model inside app/models/Permission.php
using the following example:, (*13)
<?php namespace App\Models;
use XdroidTeam\XTrust\Models\XTrustPermission;
class Permission extends XTrustPermission
{
...
}
User
Next, use the XTrustUserTrait
trait in your existing User
model. For example:, (*14)
<?php
use XdroidTeam\XTrust\Traits\XTrustUserTrait;
class User extends Eloquent
{
use XTrustUserTrait; // add this trait to your user model
...
}
Don't forget to dump composer autoload, (*15)
composer dump-autoload
Concept
There are roles and permissions. You can attach many permissions to a role, and attach many roles to a user, like in Zizaco/entrust. The main difference, that you can directly attach or detach permissions to a user., (*16)
For example
You have four permissions:
1. can_show
2. can_create
3. can_edit
4. can_delete, (*17)
You have two roles, with permissions:
1. admin:
1. can_show
2. can_create
3. can_edit
4. can_delete
2. user:
1. can_show, (*18)
You have two users, with roles:
1. Adam Admin:
1. admin
2. Super User
1. user, (*19)
If you don't want Adam Admin, to be able to delete, you can simply detach the can_delete permission from him. The admin group can still have the can_delete permission, but Adam will not.
If you want Super User to be able to edit, you can attach this permisson (can_edit) to her. The other users in the user role will still be unable to edit, except her., (*20)
Because of this logic, you can't check the user roles, only the permissions!, (*21)
Example for UI:
, (*22)
Usage
Permission checking
Simple checking
Check one permission:, (*23)
XTrust::hasPermission('can_delete');
Returns true, if the authanticated user has the permission, returns false if not., (*24)
Check multiple permissions:, (*25)
XTrust::hasPermissions(['can_delete', 'can_edit']);
Returns true, if the authanticated user has all the permissions, returns false if not., (*26)
Or:, (*27)
XTrust::hasOneOfPermissions(['can_delete', 'can_edit']);
Returns true, if the authanticated user has one of the permissions, returns false if not., (*28)
You can also check within the user model:, (*29)
$user = User::find(1);
$user->hasPermission('can_delete');
// OR
$user->hasPermissions(['can_delete', 'can_edit']);
// OR
$user->hasOneOfPermissions(['can_delete', 'can_edit']);
Middleware
Route::group(['middleware' => ['auth', 'permission:can_show']], function(){
Route::get('/', 'HomeController@index');
});
For multiple permission check use pipe symbol as OR operator:, (*30)
Route::group([
'middleware' => [
'auth',
'permission:can_show|can_create|can_edit|can_delete'
]
], function(){
Route::get('/admin', 'AdminController@index');
});
To emulate AND functionality just use multiple instances of middleware
For multiple permission check use pipe symbol as OR operator:, (*31)
Route::group([
'middleware' => [
'auth',
'permission:can_show',
'permission:can_create'
]
], function(){
Route::get('/admin', 'AdminController@index');
});
Blade
@permission('can_delete')
{!! Form::open([
'url' => '/users/'.$user->id,
'method'=> "DELETE"
] ) !!}
<button class="btn btn-sm btn-danger">
Delete
</button>
{!! Form::close() !!}
@endpermission
Multiple permissions:, (*32)
@permissions(['can_show', 'can_delete'])
<span>Something</span>
@endpermissions
Returns true, if the authanticated user has all the permissions, returns false if not., (*33)
Or:, (*34)
@oneofpermissions(['can_show', 'can_delete'])
<span>Something</span>
@endoneofpermissions
Returns true, if the authanticated user has one of the permissions, returns false if not., (*35)
Role checking
Simple checking
Check one role:, (*36)
XTrust::hasRole('can_delete');
Returns true, if the authanticated user has the role, returns false if not., (*37)
Check multiple roles:, (*38)
XTrust::hasRoles(['can_delete', 'can_edit']);
Returns true, if the authanticated user has all the roles, returns false if not., (*39)
Or:, (*40)
XTrust::hasOneOfRoles(['can_delete', 'can_edit']);
Returns true, if the authanticated user has one of the roles, returns false if not., (*41)
You can also check within the user model:, (*42)
$user = User::find(1);
$user->hasRole('can_delete');
// OR
$user->hasRoles(['can_delete', 'can_edit']);
// OR
$user->hasOneOfRoles(['can_delete', 'can_edit']);
Attaching detaching
Always use the the id of the role or permission for attaching/detaching!, (*43)
Attach to a user
Attach one role to a user:, (*44)
$user->attachRole(1);
Attach multiple roles to a user:, (*45)
$user->attachRoles([1,2]);
Attach one permission to a user:, (*46)
$user->attachPermission(1);
Attach multiple permissions to a user:, (*47)
$user->attachPermissions([1,2]);
Detach from a user
Detach one role from a user:, (*48)
$user->detachRole(1);
Detach multiple roles from a user:, (*49)
$user->detachRoles([1,2]);
Detach one permission from a user:, (*50)
$user->detachPermission(1);
Detach multiple permissions from a user:, (*51)
$user->detachPermissions([1,2]);
Attach to a role
Attach one permission to a role:, (*52)
$role->attachPermission(1);
Attach multiple permissions to a role:, (*53)
$role->attachPermissions([1,2]);
Detach from a role
Detach one permission from a role:, (*54)
$role->detachPermission(1);
Detach multiple permissions from a role:, (*55)
$role->detachPermissions([1,2]);