PHP code example of gusmanwidodo / auth-kit-permissions

1. Go to this page and download the library: Download gusmanwidodo/auth-kit-permissions 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/ */

    

gusmanwidodo / auth-kit-permissions example snippets


'statement' => [
    'post'   => ['create', 'read', 'update', 'delete'],
    'member' => ['create', 'update', 'delete'],
],

'roles' => [
    'admin'  => ['post' => '*', 'member' => '*'], // '*' = every action
    'author' => ['post' => ['create', 'read', 'update']],
    'viewer' => ['post' => ['read']],
],

use Gusmanwidodo\AuthKitPermissions\PermissionManager;
use Gusmanwidodo\AuthKitPermissions\Support\Scope;

$pm = app(PermissionManager::class);

// Static, in-memory, ZERO queries:
$pm->check('App\\User', $user->id, 'post.update', staticRoles: ['author']); // true

// Dynamic (DB) role, resolved once then memoized:
$pm->check('App\\User', $user->id, 'post.delete'); // 1 query, then 0

use Gusmanwidodo\AuthKitPermissions\HasRoles;

class User extends Authenticatable
{
    use HasRoles; // reads static roles from a `roles` attribute by default
}

$user->can('post.update'); // hybrid static + dynamic check

use Gusmanwidodo\AuthKitPermissions\RoleService;

$svc  = app(RoleService::class);
$role = $svc->createRole('editor', ['post.delete']); // global scope
$svc->assign('App\\User', $user->id, $role);

use Gusmanwidodo\AuthKitPermissions\Support\Scope;

$org = Scope::for('organization', $organization->id);

$role = $svc->createRole('org-admin', ['member.delete'], $org);
$svc->assign('App\\User', $user->id, $role, $org);

$pm->check('App\\User', $user->id, 'member.delete', scope: $org); // true inside org only

// In any plugin implementing HasHooks:
public function beforeHooks(): array
{
    return [
        'permission.check' => function ($ctx) {
            if (isSuspended($ctx->get('subject_id'))) {
                $ctx->set('allow', false)->stop(); // hard deny
            }
        },
    ];
}