2017 © Pedro Peláez
 

library roleper

This package provides a flexible way to add Role-based Permissions to Laravel

image

dangkien/roleper

This package provides a flexible way to add Role-based Permissions to Laravel

  • Thursday, July 12, 2018
  • by DangKien
  • Repository
  • 0 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

RolePermission

Role-Permission Laravel Packget, (*1)

Installation

1)Just add the following to your composer.json. Then run composer update:, (*2)

composer require dangkien/roleper --no-scripts

2) Open your config/app.php and add the following to the providers array:, (*3)

DangKien\RolePer\RolePerServiceProvider::class,

3) In the same config/app.php and add the following to the aliases array:, (*4)

'RolePer'   => DangKien\RolePer\RolePerFacade::class,

4) Run the command below to publish the package config file config/roleper.php:, (*5)

php artisan vendor:publish

5) If you want to use Middleware (requires Laravel 5.1 or later) you also need to add the following:, (*6)

    'role' => \DangKien\RolePer\Middleware\RolePerRole::class,
    'permission' => \DangKien\RolePer\Middleware\RolePerPermission::class,
    'ability' => \DangKien\RolePer\Middleware\RolePerAbility::class,

to routeMiddleware array in app/Http/Kernel.php., (*7)

copy to User.php, (*8)

    use \DangKien\RolePer\Traits\RolePerUserTrait;

Checking for Roles & Permissions

Now we can check for roles and permissions simply by doing:, (*9)

$user->hasRole('owner');   // false
$user->hasRole('admin');   // true
$user->can('edit-user');   // false
$user->can('create-post'); // true

Both hasRole() and can() can receive an array of roles & permissions to check:, (*10)

$user->hasRole(['owner', 'admin']);       // true
$user->can(['edit-user', 'create-post']); // true

By default, if any of the roles or permissions are present for a user then the method will return true. Passing true as a second parameter instructs the method to require all of the items:, (*11)

$user->hasRole(['owner', 'admin']);             // true
$user->hasRole(['owner', 'admin'], true);       // false, user does not have admin role
$user->can(['edit-user', 'create-post']);       // true
$user->can(['edit-user', 'create-post'], true); // false, user does not have edit-user permission

You can have as many Roles as you want for each User and vice versa., (*12)

Route

/// ROLE PERMISSION ROUTE
    Route::group(['prefix' => 'admin/users'], function() {
    Route::get('user-permission/{id}', '\DangKien\RolePer\Controllers\UserRoleController@index')->name('user-permission.index');
    Route::post('user-permission/{id}', '\DangKien\RolePer\Controllers\UserRoleController@store')->name('user-permission.store');
    Route::get('role-permission/{id}', '\DangKien\RolePer\Controllers\RolePermissionController@index')->name('roles-permission.index');
    Route::post('role-permission/{id}', '\DangKien\RolePer\Controllers\RolePermissionController@store')->name('roles-permission.store');
});

Route::resource('admin/roles', '\DangKien\RolePer\Controllers\RoleController');
Route::group(['prefix' => '', 'middleware' => 'role:superadmin'], function() {
    Route::resource('admin/permissions', '\DangKien\RolePer\Controllers\PermissionController');
    Route::resource('admin/permissions-group', '\DangKien\RolePer\Controllers\PermissionGroupController');
});

User

Next, use the RolePerUserTrait trait in your existing User model. For example:, (*13)

<?php

use DangKien\RolePer\Traits\RolePerUserTrait;

class User extends Eloquent
{
    use RolePerUserTrait; // add this trait to your user model

    ...
}

### Middleware

You can use a middleware to filter routes and route groups by permission or role
```php
Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
    Route::get('/', 'AdminController@welcome');
    Route::get('/manage', ['middleware' => ['permission:manage-admins'], 'uses' => 'AdminController@manageAdmins']);
});

It is possible to use pipe symbol as OR operator:, (*14)

'middleware' => ['role:admin|root']

To emulate AND functionality just use multiple instances of middleware, (*15)

'middleware' => ['role:owner', 'role:writer']

For more complex situations use ability middleware which accepts 3 parameters: roles, permissions, validate_all, (*16)

'middleware' => ['ability:admin|owner,create-post|edit-user,true']

The Versions

12/07 2018

dev-master

9999999-dev

This package provides a flexible way to add Role-based Permissions to Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl auth roles illuminate permission

30/06 2018

1.0.1

1.0.1.0

This package provides a flexible way to add Role-based Permissions to Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl auth roles illuminate permission

30/06 2018

1.0.0

1.0.0.0

This package provides a flexible way to add Role-based Permissions to Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel acl auth roles illuminate permission