PHP code example of ordermind / logical-permissions

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

    

ordermind / logical-permissions example snippets


use Ordermind\LogicalPermissions\PermissionTypeInterface;

class MyPermissionType implements PermissionTypeInterface {
  public static function getName() {
    return 'role';
  }

  public function checkPermission($role, $context) {
    $access = FALSE;
    if(!empty($context['user']['roles'])) {
      $access = in_array($role, $context['user']['roles']);
    }

    return $access;
  }
}

use Ordermind\LogicalPermissions\AccessChecker;

$permissionType = new MyPermissionType();
$accessChecker = new AccessChecker();
$permissionTypeCollection = $accessChecker->getPermissionTypeCollection();
$permissionTypeCollection->add($permissionType);

$permissions = [
  'role' => 'admin', // The key 'role' here is the name of your permission type
];
$user = ['roles' => ['admin', 'sales']];
$access = $accessChecker->checkAccess($permissions, ['user' => $user]);
// TRUE

$permissions = [
  'role' => 'admin',
];

[
  'OR' => [
    'role' => 'admin',
    'flag' => 'is_author',
  ],
]

use Ordermind\LogicalPermissions\BypassAccessCheckerInterface;

class MyBypassAccessChecker implements BypassAccessCheckerInterface {
  public function checkBypassAccess($context) {
    $bypassAccess = FALSE;
    if($context['user']['id'] == 1) {
      $bypassAccess = TRUE;
    }

    return $bypassAccess;
  }
}

$bypassAccessChecker = new MyBypassAccessChecker();
$accessChecker->setBypassAccessChecker($bypassAccessChecker);

//Disallow access bypassing
[
  'NO_BYPASS' => TRUE,
  'role' => 'editor',
]

//Disallow access bypassing only if the user is an admin
[
  'NO_BYPASS' => [
    'role' => 'admin',
  ],
  'role' => 'editor',
]

//Allow access only if the user is both an editor and a sales person
[
  'role' => [
    'AND' => ['editor', 'sales'],
  ],
]

//Allow access if the user is both a sales person and the author of the document
[
  'AND' => [
    'role' => 'sales',
    'flag' => 'is_author',
  ],
]

//Allow access by anyone except if the user is both an editor and a sales person
[
  'role' => [
    'NAND' => ['editor', 'sales'],
  ],
]

//Allow access by anyone, but not if the user is both a sales person and the author of the document.
[
  'NAND' => [
    'role' => 'sales',
    'flag' => 'is_author',
  ],
]

//Allow access if the user is either an editor or a sales person, or both.
[
  'role' => [
    'OR' => ['editor', 'sales'],
  ],
]

//Allow access if the user is either a sales person or the author of the document, or both
[
  'OR' => [
    'role' => 'sales',
    'flag' => 'is_author',
  ],
]

[
  'role' => ['editor', 'sales'],
]

[
  'role' => [
    'OR' => ['editor', 'sales'],
  ],
]

//Allow access if the user is neither an editor nor a sales person
[
  'role' => [
    'NOR' => ['editor', 'sales'],
  ],
]

//Allow neither sales people nor the author of the document to access it
[
  'NOR' => [
    'role' => 'sales',
    'flag' => 'is_author',
  ],
]

//Allow access if the user is either an editor or a sales person, but not both
[
  'role' => [
    'XOR' => ['editor', 'sales'],
  ],
]

//Allow either sales people or the author of the document to access it, but not if the user is both a sales person and the author
[
  'XOR' => [
    'role' => 'sales',
    'flag' => 'is_author',
  ],
]

//Allow access for anyone except editors
[
  'role' => [
    'NOT' => 'editor',
  ],
]

//Allow access for anyone except the author of the document
[
  'NOT' => [
    'flag' => 'is_author',
  ],
]

//Allow access for anyone
[
  TRUE,
]

//Using a boolean without an array is also permitted
TRUE

//Example with string representation
[
  'TRUE',
]

//Using a string representation without an array is also permitted
'TRUE'

//Deny access for everyone except those with bypass access
[
  FALSE,
]

//Using a boolean without an array is also permitted
FALSE

//Example with string representation
[
  'FALSE',
]

//Using a string representation without an array is also permitted
'FALSE'

//Deny access for everyone including those with bypass access
[
  FALSE,
  'NO_BYPASS' => TRUE,
]

AccessChecker::setPermissionTypeCollection( \Ordermind\LogicalPermissions\PermissionTypeCollectionInterface $permissionTypeCollection ): \Ordermind\LogicalPermissions\AccessCheckerInterface

AccessChecker::getPermissionTypeCollection(  ): \Ordermind\LogicalPermissions\PermissionTypeCollectionInterface|NULL

AccessChecker::setBypassAccessChecker( \Ordermind\LogicalPermissions\BypassAccessCheckerInterface $bypassAccessChecker ): \Ordermind\LogicalPermissions\AccessCheckerInterface

AccessChecker::getBypassAccessChecker(  ): \Ordermind\LogicalPermissions\BypassAccessCheckerInterface|NULL

AccessChecker::getValidPermissionKeys(  ): array

AccessChecker::checkAccess( array|string|boolean $permissions, array|object|NULL $context = NULL, boolean $allowBypass = TRUE ): boolean

PermissionTypeCollection::add( \Ordermind\LogicalPermissions\PermissionTypeInterface $permissionType, boolean $overwriteIfExists = FALSE ): \Ordermind\LogicalPermissions\PermissionTypeCollectionInterface

PermissionTypeCollection::remove( string $name ): \Ordermind\LogicalPermissions\PermissionTypeCollectionInterface

PermissionTypeCollection::has( string $name ): boolean

PermissionTypeCollection::get( string $name ): \Ordermind\LogicalPermissions\PermissionTypeInterface|NULL

PermissionTypeCollection::toArray(  ): array