Skip to content

Manage user permissions and roles in your Laravel application by domain driven rules.

License

Notifications You must be signed in to change notification settings

sourceboat/laravel-static-permission

Repository files navigation

laravel-static-permission

Tests Latest Version on Packagist Total Downloads

Manage user permissions and roles in your Laravel application by domain driven rules.

Example

$user->assignRole('admin');

$user->hasRole('admin'); // true

Define roles and permissions in config/permission.php.

'role' => [
  'admin' => [
    'news/#', // Allow all paths beginning with news/
  ],
  'editor' => [
    'news/#',
    '!news/delete', // Explicitly forbid news/delete
  ],
  'user' => [
    'news/show', // Explicitly allow news/show
  ],
]

Check permissions by

$admin->hasPermission('news/delete'); // true
$editor->hasPermission('news/delete'); // false
$user->hasPermission('news/delete'); // false

Installation

composer require sourceboat/laravel-static-permission

Usage

Add trait to model

  use HasRoles;

Using roles

Define roles in config/permission.php.

// config/permission.php

'roles' => [
  'role_name' => [],
  'admin' => [],
],

Assign role

Add a role to a model.

$model->assignRole('admin');

Check role

Check role via:

$model->hasRole('admin');

$model->getRoleName(); // return admin

Using permissions

Permissions are based on the MQTT syntax and specified as path. Thus, individual security levels can be mapped and generally released via wildcards.

Check permissions

$model->hasPermission('users/show/email');
$model->hasPermission(['users/show', 'users/edit']);
$model->hasAnyPermission('users/show/email');
$model->hasAnyPermission(['users/show', 'users/edit']);

Configuration

  • + Wildcard for one level
  • # Wildcard for everything following
  • ! Before the permission - prohibits permission

Define roles and permissions in config/permission.php.

// config/permission.php

'roles' => [
  'role_name' => [
    'users/+/foo'
  ],
  'admin' => [
    'users/#',
    '!users/create',
  ],
],

Using Blade directives

Use Blade directives in your views.

Role

@role('admin')
  Show if user is admin
@endrole
@unlessrole('admin')
  Show if user is not admin
@endunlessrole

Permission

@permission('user/edit')
  Show if user has rights to user/edit
@endpermission

Use several permissions.

@permission('user/edit|user/create')
  Show if user has rights to user/edit AND user/create
@endpermission
@anypermission('user/edit|user/create')
 Show if user has rights to user/edit OR user/create
@endanypermission

Middleware

Add the middleware to your src/Http/Kernel.php

use Sourceboat\Permission\Middlewares\RoleMiddleware;
class Kernel extends HttpKernel
{
... 
  protected $routeMiddleware = [
    ...
    'role' => RoleMiddleware::class
  ]

}

And use it like

Route::group(['middleware' => ['role:admin']], function () {
    //
})

Config

Example Config

<?php
// config/permission.php

return [
    /**
     * Column name of the model
     */
    'column_name' => 'role',

    /**
     * Roles with permissions
     *
     * - `+` Wildcard one level
     * - `#` Wildcard everything following
     * - `!` Before the permission - prohibits permission
     *
     * 'admin' => [
     *     'users/#',
     *     'users/+/field',
     *     '!users/create'
     * ]
     */
    'roles' => [],

];

Testing

composer test

Changelog

See releases for details.

Contributing

composer lint:phpcs
composer lint:phpmd

Credits

This package is heavily inspired by Spatie / laravel-permission.

License

The MIT License (MIT). Please see License File for more information.