Reputation: 346
Using Symfony 5, i've created some roles with hierarchy in "config/packages/security.yaml"
I will create an interface to create/edit/delete roles to users. So, i want to get all roles existing in the security.yaml, to fill a multiselect or dropdown.
Some instructions found are obsolete, like $this->container->getParameter('security.role_hierarchy')
.
Have you got a solution to return this data ?
Upvotes: 1
Views: 3528
Reputation: 27295
I think the correct way in Symfony 5 is the following:
$hierarchy = $this->container->getParameter('security.role_hierarchy.roles');
$roles = [];
array_walk_recursive($hierarchy, function($role) use (&$roles) {
$roles[$role] = $role;
});
You can access the roles over the getParameter
function. The old style is not recommended.
If you are in a controller you get call it directly $this->getParameter
.
Upvotes: 4
Reputation: 49
You can use parameter_bag
as follows:
<?php
declare(strict_types=1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class TestController
* @package App\Controller
*/
class TestController extends AbstractController
{
/**
* @Route(path="test", name="test", methods={"GET"})
* @param Request $request
* @return Response
*/
public function index(Request $request): Response
{
$roles = $this->container->get('parameter_bag')->get('security.role_hierarchy.roles');
return new Response(json_encode($roles));
}
}
Hope this helps!
Upvotes: 0