Define laravel permissions and roles by code

Define laravel permissions and roles by code

README

Latest Version on Packagist
Build Status
Total Downloads

This package allows you to manage user permissions and roles by domain driven rules.

$user->assignRole('admin');

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

You can define roles and permissions by code at 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
  ],
]

You can 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

Older than Laravel 5.5 need a service provider registration.

// config/app.php

'providers' => [
  SourceboatPermissionPermissionServiceProvider::class,
];
php artisan vendor:publish

Usage

Add trait to model

  use HasRoles;

Using roles

You can define the roles in the config/permission.php file.

// config/permission.php

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

Assign role

Add a role to a model.

$model->assignRole('admin');

Check role

You can check the role via:

$model->hasRole('admin');

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

Using permissions

Permissions are based on the MQTT syntax. Permissions are 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

You can define the role permissions in the config/permission.php file.

// config/permission.php

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

Using Blade directives

You can 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

You can use several permissions too.

@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

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

Please see CHANGELOG 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.

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *