Skip to content

Instantly share code, notes, and snippets.

@shinesoftware
Created March 24, 2014 09:06
Show Gist options
  • Save shinesoftware/9736775 to your computer and use it in GitHub Desktop.
Save shinesoftware/9736775 to your computer and use it in GitHub Desktop.
BjyAuthorize Module Fixed -Wrong Rule issue #233
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace BjyAuthorize\Provider\Role;
use BjyAuthorize\Acl\Role;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Role provider based on a {@see \Zend\Db\Adaper\Adapter}
*
* @author Ben Youngblood <[email protected]>
*/
class ZendDb implements ProviderInterface
{
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* @var string
*/
protected $identifierFieldName = 'id';
/**
* @var string
*/
protected $adapterName = 'bjyauthorize_zend_db_adapter';
/**
* @var string
*/
protected $tableName = 'user_role';
/**
* @var string
*/
protected $roleIdFieldName = 'role_id';
/**
* @var string
*/
protected $parentRoleFieldName = 'parent';
/**
* @param $options
* @param ServiceLocatorInterface $serviceLocator
*/
public function __construct($options, ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
if (isset($options['adapter'])) {
$this->adapterName = $options['adapter'];
}
if (isset($options['table'])) {
$this->tableName = $options['table'];
}
if (isset($options['role_id_field'])) {
$this->roleIdFieldName = $options['role_id_field'];
}
if (isset($options['parent_role_field'])) {
$this->parentRoleFieldName = $options['parent_role_field'];
}
}
public function getRoles()
{
/* @var $adapter \Zend\Db\Adapter\Adapter */
$adapter = $this->serviceLocator->get($this->adapterName);
$tableGateway = new TableGateway($this->tableName, $adapter);
$sql = new Select();
$sql->from($this->tableName);
/* @var $roles Role[] */
$roles = array();
$indexedRows = array();
$rowset = $tableGateway->selectWith($sql);
// Pass 1: collect all rows and index them by PK
foreach ($rowset as $row) {
$indexedRows[$row[$this->identifierFieldName]] = $row;
}
// Pass 2: build a role for each indexed row
foreach ($indexedRows as $row) {
$parentRoleId = isset($row[$this->parentRoleFieldName])
? $indexedRows[$row[$this->parentRoleFieldName]][$this->roleIdFieldName] : null;
$roleId = $row[$this->roleIdFieldName];
$roles[$roleId] = new Role($roleId, $parentRoleId);
}
// Pass 3: Re-inject parent objects to preserve hierarchy
foreach ($roles as $role) {
$parentRoleObj = $role->getParent();
if ($parentRoleObj && $parentRoleObj->getRoleId()) {
$role->setParent($roles[$parentRoleObj->getRoleId()]);
}
}
return array_values($roles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment