PHP code example of metarush / perm

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

    

metarush / perm example snippets


$roleRanks = [
    1 => 1,
    2 => 2,
    3 => 3
]

$roleResources = [
    1 => ['1','2'],
    2 => ['3','4'],
    3 => ['5','6']
];

$roleResources = [
    1 => ['createUser', 'editUser'],
    2 => ['createPost','editPost'],
    3 => ['readPost', 'createComment']
];

$resourceRestrictions = [
    '1' => [
        Perm::RESTRICTION_PERMISSION
    ],
    '2' => [
        Perm::RESTRICTION_PERMISSION,
        Perm::RESTRICTION_OWNER
    ],
    '3' => [
        Perm::RESTRICTION_CUSTOM_RULE
    ],
    '4' => [
        Perm::RESTRICTION_CUSTOM_RULE_AND_OWNER
    ]
    '5' => [
        Perm::RESTRICTION_PERMISSION_AND_CUSTOM_RULE
    ]
];

use MetaRush\Perm;

$perm = (new Perm\Builder)
    ->setRoleRanks($roleRanks)
    ->setRoleResources($roleResources)
    ->setResourceRestrictions($resourceRestrictions)
    ->setOwnerFinderFactoryFqn(Perm\Samples\MyOwnerFinderFactory::class) // optional
    ->setCustomRulesFactoryFqn(Perm\Samples\MyCustomRulesFactory::class) // optional
    ->build();

$userId = 5; // userId of the requesting user
$roleId = 3; // roleId of $userId e.g., "staff"
$resourceId = 'addUser'; // resourceId of the requested resource
$serverRequest = $psr7serverRequest; // optional PSR-7 ServerRequestInterface object
$request = new Perm\Request($userId, $roleId, $resourceId, $serverRequest);

if (!$perm->hasPermission($request))
    exit('Access denied');

// access allowed, continue with the app...