Skip to content

Instantly share code, notes, and snippets.

@Sander-Toonen
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sander-Toonen/1e03b4e729bc1d3c982d to your computer and use it in GitHub Desktop.
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.
<?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