public function isAllowed()
{
$args = func_get_args();
if ($this->isPrivilege()) {
$allow = array_reduce($this->conditions, function ($results, $condition) use($args) {
return $results && call_user_func_array($condition, $args);
}, true);
} else {
$allow = false;
if ($this->conditions) {
$allow = !array_reduce($this->conditions, function ($results, $condition) use($args) {
return $results || call_user_func_array($condition, $args);
}, false);
}
}
return $allow;
}
public function testCanSetAndCheckRestrictionAgainstConditions() { $object1 = new stdClass(); $object1->id = 1; $object2 = new stdClass(); $object2->id = 2; $rule = new Rule(false, 'read', 'stdClass', function ($obj) { return $obj->id == 1; }); $this->assertFalse($rule->isAllowed($object1)); $this->assertTrue($rule->isAllowed($object2)); $rule->when(function ($obj) { return 1 == 2; }); $this->assertFalse($rule->isAllowed($object1)); $this->assertTrue($rule->isAllowed($object2)); $rule->when(function ($obj) { return 1 == 1; }); $this->assertFalse($rule->isAllowed($object1)); $this->assertFalse($rule->isAllowed($object2)); }