Package Data | |
---|---|
Maintainer Username: | yasser.mussa |
Maintainer Contact: | yasser.mussa@gmail.com (Yasser Mussa) |
Package Create Date: | 2016-12-20 |
Package Last Update: | 2017-01-02 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:24:33 |
Package Statistics | |
---|---|
Total Downloads: | 36 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
For you can install this package. You should to follow the next steps.
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"
}
}
Add the package to your application service providers in config/app.php
file.
'providers' => [
...
/*
* Package Service Providers...
*/
Yasser\Roles\RolesServiceProvider::class,
/*
* Application Service Providers...
*/
...
],
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
Include HasRolesRelations
trait inside your User
model.
use Yasser\Roles\Traits\HasRolesRelations;
class User extends Authenticatable
{
use Notifiable, HasRolesRelations;
}
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,
],
use Yasser\Roles\Models\Permission;
$permission = Permission::create([
'name' => 'Create Users',
'slug' => 'user.create',
'description' => '', //optional
'model' => '' //optional
]);
use Yasser\Roles\Models\Role;
$adminRole = Role::create([
'name' => 'Admin',
'slug' => 'admin',
'description' => ''//optional
]);
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 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])
$adminRole = Role::create([
'name' => 'Admin',
'slug' => 'admin',
]);
$user->checkRole('admin'); //return true or false
$createPermission = Permission::create([
'name' => 'Create Users',
'slug' => 'user.create'
]);
$user->canDo('user.create');
$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
$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');