Permalink
Browse files

Update Doctrine role provider to build role hierarchy properly

 (1) Build every role object first
 (2) Perform parent object injections where necessary
  • Loading branch information...
1 parent 372e72f commit 7c6093eefaaf553c0de93b5d9fa49400071ad2a8 @adamlundrigan adamlundrigan committed Oct 25, 2012
Showing with 13 additions and 4 deletions.
  1. +13 −4 src/BjyAuthorize/Provider/Role/Doctrine.php
@@ -37,12 +37,21 @@ public function getRoles()
$builder->select($this->roleIdFieldName,$this->parentRoleFieldName)
->from($this->tableName, $this->tableName);
- $result = $builder->execute();
+ $rowset = $builder->execute();
$roles = array();
- foreach($result as $row) {
- $roles[] = new Role($row[$this->roleIdFieldName], $row[$this->parentRoleFieldName]);
+ // Pass One: Build each object
+ foreach ($rowset as $row) {
+ $roleId = $row[$this->roleIdFieldName];
+ $roles[$roleId] = new Role($roleId, $row[$this->parentRoleFieldName]);
}
- return $roles;
+ // Pass Two: Re-inject parent objects to preserve hierarchy
+ foreach ($roles as $roleId=>$roleObj) {
+ $parentRoleObj = $roleObj->getParent();
+ if ($parentRoleObj && $parentRoleObj->getRoleId()) {
+ $roleObj->setParent($roles[$parentRoleObj->getRoleId()]);
+ }
+ }
+ return array_values($roles);
}
}

0 comments on commit 7c6093e

Please sign in to comment.