Skip to content

Instantly share code, notes, and snippets.

@abhinavkumar940
Forked from cystbear/DynamicRoleHierarchy.php
Last active August 29, 2015 14:14
Show Gist options
  • Save abhinavkumar940/4c7eba1dd8f75757535b to your computer and use it in GitHub Desktop.
Save abhinavkumar940/4c7eba1dd8f75757535b to your computer and use it in GitHub Desktop.
<?php
namespace Acme\SecurityBundle\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Acme\SecurityBundle\Entity\RoleRepository;
class DynamicRoleHierarchy implements RoleHierarchyInterface
{
protected $roleRepository;
protected $roleHierarchy = null;
public function __construct(RoleRepository $roleRepository)
{
$this->roleRepository = $roleRepository;
}
public function getReachableRoles(array $roles)
{
if (null === $this->roleHierarchy) {
$this->roleHierarchy = new RoleHierarchy($this->fetchRoleHierarchy());
}
return $this->roleHierarchy->getReachableRoles($roles);
}
protected function fetchRoleHierarchy()
{
$hierarchy = array();
$this->roleRepository;
// do your stuff with $this->roleRepository
return $hierarchy;
}
}
<!--
Probably you should not overwrite native Symfony services.
There will be better add your DynamicRoleHierarchy to RoleHierarchyVoter via CompilerPass
-->
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="security.role_hierarchy" class="Acme\SecurityBundle\Role\DynamicRoleHierarchy" public="false">
<argument type="service" id="your.role.repository" />
</service>
</services>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment