PHP code example of adamnicholson / judge

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

    

adamnicholson / judge example snippets


$judge = new Judge();

// Grant access to edit orders
$judge->allow('[email protected]', 'EDIT_ORDERS');
$judge->check('[email protected]', 'EDIT_ORDERS'); // true

// Revoke access to edit orders
$judge->deny('[email protected]', 'EDIT_ORDERS');
$judge->check('[email protected]', 'EDIT_ORDERS'); // false

$judge->allow('[email protected]', 'EDIT_ORDERS', '5');
$judge->check('[email protected]', 'EDIT_ORDERS', '5'); // true

$judge = new Judge();

// Grant access to edit all orders
$judge->allow('[email protected]', 'EDIT_ORDERS');

// Override the above, revoking access specifically to order "10"
$judge->deny('[email protected]', 'EDIT_ORDERS', '10');

$judge->check('[email protected]', 'EDIT_ORDERS'); // true
$judge->check('[email protected]', 'EDIT_ORDERS', '5'); // true
$judge->check('[email protected]', 'EDIT_ORDERS', '10'); // false

$judge->getRepository()->addRole('{ROLE}', '{PARENT_ROLE}');

$judge = new Judge();

// Setup the "EDIT_ORDERS" role with the parent "ORDERS"
$judge->getRepository()->addRole('EDIT_ORDERS', 'ORDERS');

// Grant access to ORDERS and all child roles
$judge->allow('[email protected]', 'ORDERS');

$judge->check('adam@example', 'EDIT_ORDERS'); // true

$judge = new Judge();

$repo = $judge->getRepository();
$repo->addRole('DELETE_ORDERS', 'CHANGE_ORDERS');
$repo->addRole('CHANGE_ORDERS', 'VIEW_ORDERS');
$repo->addRole('VIEW_ORDERS', 'ORDERS');

// Grant access to ORDERS and all children
$judge->allow('[email protected]', 'ORDERS');

$judge->check('adam@example', 'EDIT_ORDERS'); // true

// Override the above for access to CHANGE_ORDERS and its children recursively
$judge->deny('[email protected]', 'CHANGE_ORDERS');

$judge->check('adam@example', 'ORDERS'); // true
$judge->check('adam@example', 'VIEW_ORDERS'); // true
$judge->check('adam@example', 'CHANGE_ORDERS'); // false
$judge->check('adam@example', 'DELETE_ORDERS'); // false

$judge = new Judge();

$repo = $judge->getRepository();
$repo->addIdentity('adam', 'customer_service');

$judge->allow('customer_service', 'EDIT_ORDERS');

$judge->check('adam', 'EDIT_ORDERS'); // true

$pdo = new PDO('mysql:host=db;dbname=site', 'root');
$repo = new Judge\Repository\PDORepository($pdo);
$judge = new Judge\Judge($repo);

$repo = new Judge\Repository\LazyRepositoryWrapper(function () {
    $pdo = new PDO('mysql:host=db;dbname=site', 'root');
    return new Judge\Repository\PDORepository($pdo);
});
$judge = new Judge\Judge($repo);

$judge = new Judge\Judge(new ArrayRepository);

$storage = new Flatbase\Storage\Filesystem('/some/storage/path');
$flatbase = new Flatbase\Flatbase($storage);
$repo = new Judge\Repository\FlatbaseRepository($pdo);
$judge = new Judge\Judge($repo);