/** * Adds a Role having an identifier unique to the registry * * The $parents parameter may be a reference to, or the string identifier for, * a Role existing in the registry, or $parents may be passed as an array of * these - mixing string identifiers and objects is ok - to indicate the Roles * from which the newly added Role will directly inherit. * * In order to resolve potential ambiguities with conflicting rules inherited * from different parents, the most recently added parent takes precedence over * parents that were previously added. In other words, the first parent added * will have the least priority, and the last parent added will have the * highest priority. * * @param Zend\Acl\Role $role * @param Zend\Acl\Role|string|array $parents * @throws Zend\Acl\Exception\InvalidArgumentException * @return Zend\Acl\Role\Registry Provides a fluent interface */ public function add(Role $role, $parents = null) { $roleId = $role->getRoleId(); if ($this->has($roleId)) { throw new Acl\Exception\InvalidArgumentException("Role id '{$roleId}' already exists in the registry"); } $roleParents = array(); if (null !== $parents) { if (!is_array($parents)) { $parents = array($parents); } foreach ($parents as $parent) { try { if ($parent instanceof Role) { $roleParentId = $parent->getRoleId(); } else { $roleParentId = $parent; } $roleParent = $this->get($roleParentId); } catch (Exception $e) { throw new Acl\Exception\InvalidArgumentException("Parent Role id '{$roleParentId}' does not exist", 0, $e); } $roleParents[$roleParentId] = $roleParent; $this->_roles[$roleParentId]['children'][$roleId] = $role; } } $this->_roles[$roleId] = array('instance' => $role, 'parents' => $roleParents, 'children' => array()); return $this; }
/** * Returns true if and only if the Role exists in the registry * * The $role parameter can either be a Role or a Role identifier. * * @param Zend\Acl\Role|string $role * @return boolean */ public function has($role) { if ($role instanceof Role) { $roleId = $role->getRoleId(); } else { $roleId = (string) $role; } return isset($this->_roles[$roleId]); }