您可以使用
然后检查每一个
inheritsRole()
-当且仅当 $role 继承自 $inherit 时返回 true
请参阅http://framework.zend.com/apidoc/core/Zend_Acl/Zend_Acl.htmlZend_Acl
上的 API 文档
例子
$parents = array();
foreach ($acl->getRoles() as $inherit) {
if ($acl->inheritsRole('owner', $inherit)) {
$parents[] = $inherit;
}
}
然后会返回类似
Array
(
[0] => guest
[1] => member
[2] => admin
)
选择
中实际上有一个getParents()
方法Zend_Acl_Role_Registry
,但似乎没有办法通过Zend_Acl
. 查看源代码
Zend_Acl
扩展有一个方法是微不足道的getParentsForRole()
:
class My_Acl extends Zend_Acl
{
public function getParentsForRole($role)
{
return $this->_getRoleRegistry()->getParents($role);
}
}
不过,这只会返回直接父级,例如对于“所有者”,它返回“管理员”,因此您可能希望添加另一种方法以递归方式获取所有父级直到最后一个:
class My_Acl extends Zend_Acl
{
public function getAllParentsForRole($role, $parents = array())
{
foreach ($this->getParentsForRole($role) as $parentName => $parentRole) {
if (FALSE === isset($parents[$parentName])) {
$parents[$parentName] = $parentRole;
$parents = $this->getAllParentsForRole($parentRole, $parents);
}
}
return $parents;
}
public function getParentsForRole($role)
{
return $this->_getRoleRegistry()->getParents($role);
}
}
例子
$acl = new My_Acl;
$acl->addRole('guest');
$acl->addRole('other');
$acl->addRole('member', 'guest');
$acl->addRole('admin', 'member');
$acl->addRole('owner', array('admin', 'other'));
print_r($acl->getAllParentsForRole('owner'));
会导致
Array
(
[admin] => Zend_Acl_Role Object
(
[_roleId:protected] => admin
)
[member] => Zend_Acl_Role Object
(
[_roleId:protected] => member
)
[guest] => Zend_Acl_Role Object
(
[_roleId:protected] => guest
)
[other] => Zend_Acl_Role Object
(
[_roleId:protected] => other
)
)
这与单个调用getParents
将返回的内容一致。如果您不需要,您也可以只使用第一个示例中的 foreach 代码,例如
class My_Acl extends Zend_Acl
{
public function getAllParentsForRole($role)
{
$parents = array();
foreach ($this->getRoles() as $inherit) {
if($this->inheritsRole($role, $inherit)) {
$parents[] = $inherit;
}
}
return $parents;
}
}