PHP code example of enclave-code / static-auth-manager

1. Go to this page and download the library: Download enclave-code/static-auth-manager 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/ */

    

enclave-code / static-auth-manager example snippets


$user->assignRole('admin');

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

$user->assignRole(['admin','user']);

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


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

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

// config/app.php

'providers' => [
  EnclaveCode\StaticAuthManager\Providers\PermissionServiceProvider::class,
];

  use HasRoles;

// config/permission.php

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

$model->assignRole('admin');

$model->assignRole(['admin','user']);

$model->hasRole('admin');

$model->getRoles(); // return collection(['admin'])


$model->hasRole(['admin','user']);


$model->getRoles(); // return collection(['admin','user']);


$model->assignRole(['admin','user']);
$model->detachRole('admin');


$model->getRoles(); // return collection(['user'])

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

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

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

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

// config/permission.php

'roles' => [
  'role_name' => [
    'users/*'
  ],
  'admin' => [
    'users/create',
  ],
],

class Kernel extends HttpKernel
{
... 
  protected $routeMiddleware = [
    ...
    'permission' => \EnclaveCode\StaticAuthManager\Middleware\HasAnyPermissionMiddleware::class,
    'role' => \EnclaveCode\StaticAuthManager\Middleware\HasRoleMiddleware::class

  ]

}

// If user has 'admin' or 'user' role
Route::group(['middleware' => ['role:admin|user']], function () {
    //
})

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

// If user has 'user/create'
Route::group(['middleware' => ['permission:create/user']], function () {
    //
})

// If user has 'user/create' or 'user/edit'
Route::group(['middleware' => ['permission:create/user|user/edit']], function () {
    //
})


// config/permission.php

return [
    /**
     * DB Column name from model
     */
    'column_name' => env('SAM_ROLE_COLUMN_NAME', 'role'),

    /**
     * Roles with permission as path
     *
     * - `*` Wildcard everything following
     *
     * 'admin' => [
     *      'users/*',
     * ],
     * 'user' => [
     *     'users/create'
     * ]
     *
     */
    'roles' => [],

];

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