Last active
August 29, 2015 14:18
-
-
Save Sander-Toonen/1e03b4e729bc1d3c982d to your computer and use it in GitHub Desktop.
ReversedRoleHierarchy for getting all parent roles that lead to a specific role.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Security\Core\Role\RoleHierarchy; | |
use Symfony\Component\Security\Core\Role\Role; | |
/** | |
* ReversedRoleHierarchy defines a reversed role hierarchy. | |
* | |
* @author Sander Toonen <s.toonen@gmail.com> | |
*/ | |
class ReversedRoleHierarchy extends RoleHierarchy | |
{ | |
/** | |
* Constructor. | |
* | |
* @param array $hierarchy An array defining the hierarchy | |
*/ | |
public function __construct(array $hierarchy) | |
{ | |
// Reverse the role hierarchy. | |
$reversed = []; | |
foreach ($hierarchy as $main => $roles) { | |
foreach ($roles as $role) { | |
$reversed[$role][] = $main; | |
} | |
} | |
// Use the origional algorithm to build the rolemap. | |
parent::__construct($reversed); | |
} | |
/** | |
* Helper function to get an array of strings | |
* | |
* @param array $roleNames An array of string role names | |
* | |
* @return array An array of string role names | |
*/ | |
public function getParentRoles(array $roleNames) | |
{ | |
$roles = []; | |
foreach ($roleNames as $roleName) { | |
$roles[] = new Role($roleName); | |
} | |
$results = []; | |
foreach ($this->getReachableRoles($roles) as $parent) { | |
$results[] = $parent->getRole(); | |
} | |
return $results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment