2017 © Pedro Peláez
 

library rbac

Simple RBAC/ACL for Laravel 5.5 and more with caching and permission groups.

image

yaroslavmolchan/rbac

Simple RBAC/ACL for Laravel 5.5 and more with caching and permission groups.

  • Wednesday, September 27, 2017
  • by YaroslavMolchan
  • Repository
  • 3 Watchers
  • 19 Stars
  • 2,195 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 5 Open issues
  • 14 Versions
  • 24 % Grown

The README.md

Laravel RBAC

Simple RBAC/ACL for Laravel 8 and more with caching permissions and permission groups for better convenience., (*1)

Installation

Install this package with composer using the following command:, (*2)

composer require yaroslavmolchan/rbac

or you can add to your composer.json for Laravel 8.0, (*3)

"require": {
    ...
    "yaroslavmolchan/rbac": "^2.0"
}

or if you use Laravel 5.5 use:, (*4)

"require": {
    ...
    "yaroslavmolchan/rbac": "^1.0"
}

then run composer update., (*5)

Add Service Provider to providers array in config/app.php file., (*6)

'providers' => [
    ...
    /*
     * Package Service Providers...
     */
    YaroslavMolchan\Rbac\RbacServiceProvider::class,
    ...
],

Publish migration files, (*7)

$ php artisan vendor:publish --provider="YaroslavMolchan\Rbac\RbacServiceProvider" --tag=migrations

And then run migrations, (*8)

$ php artisan migrate

Add middleware to your app/Http/Kernel.php file., (*9)

protected $routeMiddleware = [
    ...
    'role' => \YaroslavMolchan\Rbac\Middleware\CheckRole::class,
    'permission' => \YaroslavMolchan\Rbac\Middleware\CheckPermission::class
];

Add Rbac trait to your User model, (*10)

use \YaroslavMolchan\Rbac\Traits\Rbac;

class User extends Authenticatable
{
    use Rbac;
    ...

}

Usage

Roles

Creating roles

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::create([
    'name' => 'Administrator',
    'slug' => 'admin'
]);

$managerRole = Role::create([
    'name' => 'Manager',
    'slug' => 'manager'
]);

Attaching And Detaching Roles

You can simple attach role to user:, (*11)

use App\User;

$user = User::find(1);
$user->attachRole($adminRole);
//or you can insert only id
$user->attachRole($adminRole->id);

And the same if you want to detach role:, (*12)

use App\User;

$user = User::find(1);
$user->detachRole($adminRole);
//or you can insert only id
$user->detachRole($adminRole->id);

Checking for roles

You can simple check if user has role:, (*13)

use App\User;

$user = User::find(1);
if ($user->hasRole('admin')) {

}

Permissions

Creating permissions

use \YaroslavMolchan\Rbac\Models\Permission;

$createPermission = Permission::create([
    'name' => 'Create product',
    'slug' => 'product.create'
]);

$removePermission = Permission::create([
    'name' => 'Delete product',
    'slug' => 'product.remove'
]);

Attaching And Detaching permissions

You can attach permission to role very simple:, (*14)

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->attachPermission($createPermission);
//or you can insert only id
$adminRole->attachPermission($createPermission->id);

And the same to detach permission:, (*15)

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->detachPermission($createPermission);
//or you can insert only id
$adminRole->detachPermission($createPermission->id);

If you want attach or detach array of permissions you can do it:, (*16)

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->attachPermissions([$createPermission, $removePermission]);
//or you can insert only id
$adminRole->detachPermission([$createPermission->id, $removePermission->id]);

```php use \YaroslavMolchan\Rbac\Models\Role;, (*17)

$adminRole = Role::find(1); $adminRole->detachPermissions([$createPermission, $removePermission]); //or you can insert only id $adminRole->detachPermissions([$createPermission->id, $removePermission->id]);, (*18)


### Checking for permissions You can simple check if user has permission: ```php use App\User; $user = User::find(1); if ($user->canDo('product.create')) { }

All permissions for each role store in cache, and when you check for permission - it take information from cache, that`s why its works quickly., (*19)

Permission groups

Permission groups created for group some permissions in one main group, and then you can attach permission group to role and all permissions in this group attach to this role to. Its very useful thing., (*20)

Creating permission groups

use \YaroslavMolchan\Rbac\Models\PermissionGroup;

$productManagementPermissionGroup = PermissionGroup::create([
    'name' => 'Product management',
    'module' => 'main' // optional
]);

Attaching And Detaching Permissions to Permission group

You can add permission to group very simple:, (*21)

use \YaroslavMolchan\Rbac\Models\Permission;

$createPermission = Permission::find(1);
$productManagementPermissionGroup->attachPermission($createPermission);
//or you can insert only id
$productManagementPermissionGroup->attachPermission($createPermission->id);

And the same to remove permission from group:, (*22)

use \YaroslavMolchan\Rbac\Models\Permission;

$createPermission = Permission::find(1);
$productManagementPermissionGroup->detachPermission($createPermission);
//or you can insert only id
$productManagementPermissionGroup->detachPermission($createPermission->id);

Attaching And Detaching Permission groups to Role

You can attach permission group to role very simple:, (*23)

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->attachGroup($productManagementPermissionGroup);

And the same to detach permission group:, (*24)

use \YaroslavMolchan\Rbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->detachGroup($productManagementPermissionGroup);

Protected routes

You can easily protect your routes with role and permission params:, (*25)

Route::get('/admin', [
    'uses' => 'AdminController@index',
    'middleware' => 'role:admin'
]);

Route::get('/products/create', [
    'uses' => 'ProductsController@create',
    'middleware' => 'permission:product.create'
]);

Blade Extensions

You can check roles and permissions in Blade like this:, (*26)

@ifUserIs('admin')
    // show content only for admin
@else
    // show content for other roles
@endif

@ifUserCan('product.create')
    // show product create content
@endif

License

Laravel RBAC is open-sourced software licensed under the MIT license, (*27)

The Versions

27/09 2017

dev-master

9999999-dev

Simple RBAC/ACL for Laravel 5.5 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

27/09 2017

1.0.3

1.0.3.0

Simple RBAC/ACL for Laravel 5.5 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

27/09 2017

1.0.2

1.0.2.0

Simple RBAC/ACL for Laravel 5.5 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

12/07 2017

0.9.x-dev

0.9.9999999.9999999-dev

Simple RBAC/ACL for Laravel 5.3 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

12/07 2017

0.9.9

0.9.9.0

Simple RBAC/ACL for Laravel 5.3 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

21/04 2017

1.0.1

1.0.1.0

Simple RBAC/ACL for Laravel 5.3 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

15/04 2017

1.0.0

1.0.0.0

Simple RBAC/ACL for Laravel 5.3 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

14/04 2017

0.9.8

0.9.8.0

Simple RBAC/ACL for Laravel 5.3 and more with caching and permission groups.

  Sources   Download

MIT

The Requires

 

laravel acl rbac

05/04 2017

0.9.7

0.9.7.0

Simple RBAC for Laravel 5.3 and more

  Sources   Download

MIT

The Requires

 

laravel acl rbac

06/01 2017

0.9.5

0.9.5.0

Simple RBAC for Laravel 5.3

  Sources   Download

MIT

The Requires

 

laravel acl rbac

09/12 2016

0.9.4

0.9.4.0

Simple RBAC for Laravel 5.3

  Sources   Download

MIT

The Requires

 

laravel acl rbac

09/12 2016

0.9.3

0.9.3.0

Simple RBAC for Laravel 5.3

  Sources   Download

MIT

The Requires

 

laravel acl rbac

25/10 2016

0.9.1

0.9.1.0

Simple RBAC for Laravel 5.3

  Sources   Download

MIT

The Requires

 

laravel acl rbac

25/10 2016

0.9.0

0.9.0.0

Simple RBAC for Laravel 5.3

  Sources   Download

MIT

The Requires

 

laravel acl rbac