PHP code example of craftcamp / php-abac

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

    

craftcamp / php-abac example snippets




use PhpAbac\AbacFactory;

class User{
    protected $id;

    protected $isBanned;

    public function getId() {
        return $this->id;
    }

    public function setIsBanned($isBanned) {
        $this->isBanned = $isBanned;

        return $this;
    }

    public function getIsBanned() {
        return $this->isBanned;
    }
}

$user = new User();
$user->setIsBanned(true);

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$abac->enforce('create-group', $user);

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('read-public-group', $user, $group);



use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('edit-group', $user, $group, [
    'dynamic-attributes' => [
        'group-owner' => $user->getId()
    ]
]);



use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('remove-group', $user, $group);

$check = $abac->enforce('edit-group', $user, $group, [
    'cache_result' => true,
    'cache_ttl' => 3600, // Time To Live in seconds
    'cache_driver' => 'memory' // memory is the default driver, you can avoid this option
]);



use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'alcoollaw.yml'
]);
$check = $abac->enforce('alcoollaw', $user);



use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'user_def.yml',
    'gunlaw.yml',
],[],'rest/conf/policy/');
$check = $abac->enforce('gunlaw', $user);