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
//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',
],
]