How to check voter role with Symfony?

How to check voting role with Symfony?

I have tried using the following

$this->get('security.context')->isGranted('ROLE_ADMIN');

      

To do this, I need to inject a service security.context

that will invoke a circular reference.

  • Should I be introducing a container which is considered bad practice?
  • Should I do $user->getRoles()

    and check if the "ROLE_ADMIN" role belongs to $ user?
  • How can I manage hierarchy and inherit role?
+3


source to share


2 answers


You can try to enter RoleHierarchyInterface and go with number 3.



0


source


Alternatively, you can use AccessDecisionManagerInterface

:

class UserCheckerSomething
{
    private $decisionManager;

    public function __construct(AccessDecisionManagerInterface $decisionManager)
    {
        $this->decisionManager = $decisionManager;
    }

    public function checkAccess(TokenInterface $token)
    {
        return $this->decisionManager->decide($token, ["ROLE_ADMIN"]);
    }
}

      



And inject "@security.access.decision_manager"

into service :)

0


source







All Articles