/** * Return true if rule was already added. * * @param Rule | mixed $needRule Rule or rule's id * @return bool */ public function hasRule($needRule) { foreach ($this->rules as $rule) { $needRuleId = $needRule instanceof Rule ? $needRule->getId() : $needRule; if ($rule->getId() == $needRuleId) { return $rule; } } return false; }
public function testResultWithNullAction() { $collection = new RuleResultCollection(); $rule = new Rule('Test'); $rule->setAction(null); $result = new RuleResult($rule, 0, 'testNeedRole', 'testNeedResource'); $rule2 = new Rule('Test2'); $rule2->setAction(true); $result2 = new RuleResult($rule2, 0, 'testNeedRole', 'testNeedResource'); $collection->add($result); $this->assertFalse($collection->get()); $collection->add($result2); $this->assertTrue($collection->get()); }
public function testRuleResult() { $roleAggregate = $this->getMock('SimpleAcl\\Role\\RoleAggregateInterface'); $resourceAggregate = $this->getMock('SimpleAcl\\Resource\\ResourceAggregateInterface'); $rule = new Rule('Test'); $rule->setRoleAggregate($roleAggregate); $rule->setResourceAggregate($resourceAggregate); $rule->setAction(true); $result = new RuleResult($rule, 0, 'testNeedRole', 'testNeedResource'); $this->assertSame($rule, $result->getRule()); $this->assertEquals('testNeedRole', $result->getNeedRoleName()); $this->assertEquals('testNeedResource', $result->getNeedResourceName()); $this->assertEquals(0, $result->getPriority()); $this->assertEquals($rule->getAction($result), $result->getAction()); $result->setPriority(10); $this->assertEquals(10, $result->getPriority()); $this->assertSame($roleAggregate, $result->getRoleAggregate()); $this->assertSame($resourceAggregate, $result->getResourceAggregate()); $this->assertNotEmpty($result->getId()); }
public function testNullRoleOrResource() { $rule = new Rule('Rule'); $this->assertNull($rule->isAllowed('NotMatchedRule', 'Role', 'Resource')); $this->assertInstanceOf('SimpleAcl\\RuleResult', $rule->isAllowed('Rule', 'Role', 'Resource')); $rule = new Rule('Rule'); $rule->setRole(new Role('Role')); $this->assertNull($rule->isAllowed('Rule', 'NotMatchedRole', 'Resource')); $this->assertInstanceOf('SimpleAcl\\RuleResult', $rule->isAllowed('Rule', 'Role', 'Resource')); $rule = new Rule('Rule'); $rule->setResource(new Resource('Resource')); $this->assertNull($rule->isAllowed('Rule', 'Role', 'NotMatchedResource')); $this->assertInstanceOf('SimpleAcl\\RuleResult', $rule->isAllowed('Rule', 'Role', 'Resource')); }
public function testRuleApplyPriority() { $acl = new Acl(); $rule = new Rule('View'); $rule->setPriority(1); $u = new Role('U'); $r = new Resource('R'); $acl->addRule($u, $r, $rule, false); $acl->addRule($u, $r, 'View', true); $this->assertFalse($acl->isAllowed('U', 'R', 'View')); $rule->setPriority(0); $this->assertTrue($acl->isAllowed('U', 'R', 'View')); }