Search code examples
symfonysymfony4

Authenticate a user by role


I have a user with ROLE_ADMIN and I want to connect him as "ROLE_USER" when calling http://localhost/login but connect him as "ROLE_ADMIN" when calling http://localhost/login?role=admin. Is it possible to do it in Symfony ?


Solution

  • You can just check if the "role" GET parameter is set and defined as "admin" and then write your code.

    If there is a role inheritance and that your admin has ROLE_USER and ROLE_ADMIN, just define custom actions if you detect that it's an admin:

            if ($this->isGranted('ROLE_ADMIN')) {
                //adminCode
            } else {
               //userCode
            }