yasser / laravel-roles
Powerful package for handling roles and permissions in Laravel 5.3
Requires
- php: >=5.6.4
- illuminate/database: ^5.3
Requires (Dev)
- mockery/mockery: ^0.9.6
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2024-11-23 22:01:52 UTC
README
Installation
For you can install this package. You should to follow the next steps.
Composer
For a installation package you can use the composer command
composer require yasser/laravel-roles
or you can pull this package in through Composer file
{ "require": { ... "yasser/laravel-roles": "^0.1.2" } }
Service Provider
Add the package to your application service providers in config/app.php
file.
'providers' => [ ... /* * Package Service Providers... */ Yasser\Roles\RolesServiceProvider::class, /* * Application Service Providers... */ ... ],
Migrations
Excecute this command in your console to add migrations files to a project
php artisan vendor:publish --provider="Yasser\Roles\RolesServiceProvider" --tag=migrations
and also run the migrations
php aritsan migrate
User trait
Include HasRolesRelations
trait inside your User
model.
use Yasser\Roles\Traits\HasRolesRelations; class User extends Authenticatable { use Notifiable, HasRolesRelations; }
Middleware
Add the middleware VerifyPermission
into app/Http/kernel.php file.
/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ ... 'verify' => \Yasser\Roles\Middlewares\VerifyPermission::class, 'check' => \Yasser\Roles\Middlewares\VerifyRole::class, ],
Usage
Create Permissions
use Yasser\Roles\Models\Permission; $permission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create', 'description' => '', //optional 'model' => '' //optional ]);
Create Roles
use Yasser\Roles\Models\Role; $adminRole = Role::create([ 'name' => 'Admin', 'slug' => 'admin', 'description' => ''//optional ]);
Attach and Detach Permissions to a role
You can attach one permition to a role
$createPermission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create', 'description' => '', //optional 'model' => '' //optional ]); $role = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); $role->attachPermission($createPermission);
or you can attach many permitions to a role
$createPermission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create' ]); $deletePermission = Permission::create([ 'name' => 'Delete user', 'slug' => 'user.delete' ]); $role = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); $role->attachPermissions([$createPermission, $deletePermission]);
Detach a one permission from a role
$role->detachPermission($createRole);
or you can detach many permissions from a role
$role->detachPermissions([$createPermission, $deletePermission])
Attach and Detach Role to a user
Attach a Role to a user
$role = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); $user->attachRole($role)
Attach many roles to a user
$adminRole = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); $operatorRole = Role::create([ 'name' => 'Operator', 'slug' => 'operator', ]); $user->attachRoles([$adminRole, $operatorRole]);
Detach a role from a user
$user->detachRole($role)
Detach many roles from a user
$user->detachRoles([$adminRole, $operatorRole])
Check if a user has a role
$adminRole = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); $user->checkRole('admin'); //return true or false
Check if a user has a permission
$createPermission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create' ]); $user->canDo('user.create');
Blade directives
$role = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); @checkRole('admin') ... @endCheckRole $createPermission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create' ]); @canDo('user.create') <a href="/users/create">Create a user</a> @endCanDo
Middleware functions
$role = Role::create([ 'name' => 'Admin', 'slug' => 'admin', ]); Route::get('user/create', ...)->middleware('ckeck:admin');
$createPermission = Permission::create([ 'name' => 'Create Users', 'slug' => 'user.create' ]); Route::get('/user/create', ... )->middleware('verify:user.create');