Package Data | |
---|---|
Maintainer Username: | pierresilva |
Maintainer Contact: | pierre.silva@lab3studio.com (Pierre Silva) |
Package Create Date: | 2017-02-11 |
Package Last Update: | 2017-02-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 03:12:08 |
Package Statistics | |
---|---|
Total Downloads: | 140 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Sentinel brings a simple and light-weight role-based permissions system to Laravel's built in Auth system.
Sentinel brings support for the following ACL structure:
Permissions are then inherited to the user through the user's assigned roles.
Begin by installing the package through Composer. The best way to do this is through your terminal via Composer itself:
composer require pierresilva/laravel-sentinel
Once this operation is complete, simply add the service provider to your project's config/app.php
file and run the provided migrations against your database.
pierresilva\Sentinel\SentinelServiceProvider::class,
'Sentinel' => pierresilva\Sentinel\Facades\Sentinel::class,
Then open the Kernel.php file in app\Http\Kernel.php and add this to user it as middleware :
'roles' => \pierresilva\Sentinel\Middleware\UserHasRole::class,
'permissions' => \pierresilva\Sentinel\Middleware\UserHasPermission::class,
You'll need to run the provided migrations against your database. Publish the migration files using the vendor:publish
Artisan command and run migrate
:
php artisan vendor:publish
php artisan migrate
Sentinel package comes bundled with a SentinelTrait file to be used within your User's Model file. This trait file provides all the necessary functions to tie your users in with roles and permissions.
<?php
namespace App;
use pierresilva\Sentinel\Traits\SentinelTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
use SentinelTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'gender', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
The following methods will become available from your User model.
Checks if the user is under the given role.
Auth::user()->isRole('administrator');
You may also use magic methods:
Auth::user()->isAdministrator();
Checks if the user has the given permission(s). You may pass either a string or an array of permissions to check for. In the case of an array, ALL permissions must be accountable for in order for this to return true.
Auth::user()->can('access.admin');
or
Auth::user()->can(['access.admin', 'view.users']);
Assign the given role to the user.
Auth::user()->assignRole(1);
revokeRole($roleId)
Revokes the given role from the user.
Auth::user()->revokeRole(1);
Revokes all roles from the user.
Auth::user()->revokeAllRoles();
Syncs the given roles with the user. This will revoke any roles not supplied.
Auth::user()->syncRoles([1, 2, 3]);
The bundled Role model has easy to use methods to manage and assign permissions.
Checks if the role has the given permission(s). You may pass either a string or an array of permissions to check for. In the case of an array, ALL permissions must be accounted for in order for this to return true.
$role = Role::find(1);
return $role->can('access.admin');
Retrieves an array of assigned permission slugs for the role.
$role = Role::find(1);
return $role->getPermissions();
Assigns the given permission to the role.
$role = Role::find(1);
$role->assignPermission(1);
$role->save();
Revokes the given permission from the role.
$role = Role::find(1);
$role->revokePermission(1);
$role->save();
Revokes all permissions from the role.
$role = Role::find(1);
$role->revokeAllPermissions();
$role->save();
Syncs the given permissions with the role. This will revoke any permissions not supplied.
$role = Role::find(1);
$role->syncPermissions([1, 2, 3]);
$role->save();
Sentinel package comes with a handy Facade to make checking for roles and permissions easy within your code.
Checks if the current user can perform the provided permissions.
Parameters
if (Sentinel::can('create.blog.post')) {
// Do whatever
}
if (Sentinel::can(['access.admin', 'create.blog.post'])) {
// Do whatever
}
Checks if the current user can at least perform one of the provided permissions.
Parameters
if (Sentinel::canAtLeast(['edit.blog.post', 'create.blog.post'])) {
// Can at least do one of the tasks
}
Checks if the current user has the given role.
Parameters
if (Sentinel::isRole('admin')) {
// Do whatever
}
Sentinel comes bundled with custom template methods to easily check for permissions and roles from within your view files for both Blade.
Blade
@can('create.blog.post')
Do whatever
@else
Do something else
@endcan
@canatleast(['edit.blog.post', 'create.blog.post'])
Do whatever
@else
Do something else
@endcanatleast
@role('admin')
Do whatever
@else
Do something else
@endrole