PHP code example of webiik / privileges

1. Go to this page and download the library: Download webiik/privileges 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/ */

    

webiik / privileges example snippets


$privileges = new \Webiik\Privileges\Privileges();

// Add roles
$privileges->addRole('user');
$privileges->addRole('admin');

// Add resources
$privileges->addResource('article', ['create', 'read', 'update', 'delete']);

// Allow access to resources
$privileges->allow('user', 'article', ['read']);
$privileges->allow('admin', 'article', ['all']);

// Test access to resources
if ($privileges->isAllowed('admin', 'article', 'update')) {
    // Admin can update an article
}

addRole(string $role): void

$privileges->addRole('user');

addResource(string $resource, array $privileges): void

$privileges->addResource('article', ['create', 'read', 'update', 'delete']);

allow(string $role, string $resource, array $privileges): void

$privileges->allow('user', 'article', ['read']);

isAllowed(string $role, string $resource, string $privilege): bool

if ($privileges->isAllowed('user', 'article', 'read')) {
    // User can read an article
}