PHP code example of sourceboat / laravel-static-permission

1. Go to this page and download the library: Download sourceboat/laravel-static-permission library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sourceboat / laravel-static-permission example snippets


$user->assignRole('admin');

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

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

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

  use HasRoles;

// config/permission.php

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

$model->assignRole('admin');

$model->hasRole('admin');

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

$model->hasPermission('users/show/email');

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

$model->hasAnyPermission('users/show/email');

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

// config/permission.php

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

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

}

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



// 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' => [],

];

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