public function testMostSpecificRuleAppliesIfNoExactRuleIsFound() { $this->repository->addRule($this->role1, $this->resource1, true); $this->repository->addRule($this->role1, $this->resource2, false); $rule = $this->repository->getMostApplyingRule($this->role2, $this->resource2); $this->assertSame($this->role1->getRoleId(), $rule->getRoleId()); $this->assertSame($this->resource2->getResourceId(), $rule->getResourceId()); }
/** * Returns the rules associated with a Resource and a Role, or null if no such rules exist * * If either $resource or $role is null, this means that the rules returned are for all Resources or all Roles, * respectively. Both can be null to return the default rule set for all Resources and all Roles. * * If the $create parameter is true, then a rule set is first created and then returned to the caller. * * @param Zend\Acl\Resource $resource * @param Zend\Acl\Role $role * @param boolean $create * @return array|null */ protected function &_getRules(Resource $resource = null, Role $role = null, $create = false) { // create a reference to null $null = null; $nullRef =& $null; // follow $resource do { if (null === $resource) { $visitor =& $this->_rules['allResources']; break; } $resourceId = $resource->getResourceId(); if (!isset($this->_rules['byResourceId'][$resourceId])) { if (!$create) { return $nullRef; } $this->_rules['byResourceId'][$resourceId] = array(); } $visitor =& $this->_rules['byResourceId'][$resourceId]; } while (false); // follow $role if (null === $role) { if (!isset($visitor['allRoles'])) { if (!$create) { return $nullRef; } $visitor['allRoles']['byPrivilegeId'] = array(); } return $visitor['allRoles']; } $roleId = $role->getRoleId(); if (!isset($visitor['byRoleId'][$roleId])) { if (!$create) { return $nullRef; } $visitor['byRoleId'][$roleId]['byPrivilegeId'] = array(); } return $visitor['byRoleId'][$roleId]; }
/** * Adds a new rule, that allows/denies access on $resource to $role * * @param Role $role Role for the new rule * @param Resource $resource Resource for the new rule * @param bool $allowed Status of the new rule * @return void */ public function addRule(Role $role, Resource $resource, $allowed) { $this->rules[] = new Rule($role->getRoleId(), $resource->getResourceId(), $allowed); }