PHP code example of enygma / xacmlphp

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

    

enygma / xacmlphp example snippets




rcer = new \Xacmlphp\Enforcer();

$decider = new \Xacmlphp\Decider();
$enforcer->setDecider($decider);

// Create some Matches
$match1 = new \Xacmlphp\Match('StringEqual', 'property1', 'TestMatch1', 'test');
$match2 = new \Xacmlphp\Match('StringEqual', 'property1', 'TestMatch2', 'test1234');

// Create a Target container for our Matches
$target = new \Xacmlphp\Target();
$target->addMatches(array($match1, $match2));

// Make a new Rule and add the Target to it
$rule1 = new \Xacmlphp\Rule();
$rule1->setTarget($target)
    ->setId('TestRule')
    ->setEffect('Permit')
    ->setDescription(
        'Test to see if there is an attribute on the subject'
        .'that exactly matches the word "test"'
    )
    ->setAlgorithm(new \Xacmlphp\Algorithm\DenyOverrides());

// Make two new policies and add the Rule to it (with our Match)
$policy1 = new \Xacmlphp\Policy();
$policy1->setAlgorithm('AllowOverrides')->setId('Policy1')->addRule($rule1);

$policy2 = new \Xacmlphp\Policy();
$policy2->setAlgorithm('DenyOverrides')->setId('Policy2')->addRule($rule1);


// Create the subject with its own Attribute
$subject = new \Xacmlphp\Subject();
$subject->addAttribute(
    new \Xacmlphp\Attribute('property1', 'test')
);

// Link the Policies to the Resource
$resource = new \Xacmlphp\Resource();
$resource
    ->addPolicy($policy1)
    ->addPolicy($policy2);


$environment = null;
$action = null;

$result = $enforcer->isAuthorized($subject, $resource);

/**
 * The Subject does have a property that's equal to "test" on the "property1"
 * attribute, but the default Operation is to "fail closed". The other Match,
 * for "test1234" failed and DenyOverrides wins so the return is false.
 */

echo "\n\n".' END RESULT: '.var_export($result, true);
echo "\n\n";