-
-
Save abhinavkumar940/4c7eba1dd8f75757535b to your computer and use it in GitHub Desktop.
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 | |
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; | |
} | |
} |
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
<!-- | |
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