Listing roles in Zend Framework ACL
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
I have been working on a interface to allow me to edit Zend Framework’s ACL. The main problem was that currently there are no ways to get the list of roles out of it. To add the functionality at the moment you have to patch one framework file (not nice) and sub-class the Zend_Acl class.
First create a subclass of Zend_Acl and include the following within it.
public function listroles($parents = false) { // Grab the list of roles from the registry (this is via a PATCH) $roles = ($this->_getRoleRegistry()->listroles(); if($parents) { // start with an empty array $list = array(); // then step through all the supplied roles foreach($roles as $role) { $parents = array(); // Grab the parents from the role registry foreach($this->_getRoleRegistry()->getParents($role) as $parent) { // we have the instance then we grab the name $parents[] = $parent->getRoleId(); } // and store it into the list alongside the parents $list[$role] = $parents; } // sort it by the key ksort($list); return $list; } else { // sort by the value sort($roles); // and return the data return $roles; } }
You also need to patch the Zend/Acl/Role/Registry.php file with the following to allow access to the list of roles as it’s protected.
/** * listroles() - PATCH * * This is a quick hack to allow me to view the roles as a list * */ public function listroles() { return array_keys($this->_roles); }
The method listroles within your new sub-classed file can either list the roles or the roles as key values and the parents as array values.