Psecio\PropAuth\Enforcer::evaluate PHP Method

evaluate() public method

Given a subject and a policy, evaluate the pass/fail result of the matching
public evaluate ( object $subject, Policy $policy, array $addl = [] ) : boolean
$subject object Subject to match against
$policy Policy Policy to evaluate
$addl array
return boolean Pass/fail status of evaluation
    public function evaluate($subject, Policy $policy, array $addl = array())
    {
        $addl = array_merge([$subject], $addl);
        $checks = $policy->getChecks();
        if (empty($checks)) {
            trigger_error('Policy evaluation was perfomed with no checks', E_USER_WARNING);
        }
        foreach ($checks as $type => $value) {
            $propertyValue = $this->getPropertyValue($type, $subject);
            if ($propertyValue == null && $type !== 'closure') {
                throw new \InvalidArgumentException('Invalid property value for "' . $type . '"!');
            }
            $result = $this->executeTests($value, $type, $propertyValue, $addl);
            // If we have a failure, return false...
            if ($result === false) {
                return false;
            }
        }
        return true;
    }

Usage Example

Example #1
0
 public function evaluate($policyName = null, $type = Policy::ANY)
 {
     if ($this->subject->isAuthed() === false) {
         throw new \InvalidArgumentException('You cannot perform policy evaluations on a non-authenticated subject.');
     }
     $enforcer = new Enforcer();
     if ($policyName !== null) {
         // Find the policy by name
         $policy = $this->policies[$policyName];
         if ($policy === null) {
             throw new \InvalidArgumentException('Invalid policy: ' . $policyName);
         }
         return $enforcer->evaluate($this->subject, $policy);
     } else {
         // evaluate all policies and combine using the type
         foreach ($this->policies as $policy) {
             $result = $enforcer->evaluate($this->subject, $policy);
             if ($type === Policy::ANY && $result === true) {
                 return true;
             }
             if ($type === Policy::ALL && $result === false) {
                 // Just one didn't pass, fail out
                 return false;
             }
         }
         return true;
     }
 }
All Usage Examples Of Psecio\PropAuth\Enforcer::evaluate