Authority\Rule::isAllowed PHP Метод

isAllowed() публичный Метод

Determine if current rule allows access
public isAllowed ( ) : boolean
Результат boolean
    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;
    }

Usage Example

Пример #1
0
 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));
 }