'Symfony 6 multiple UserRoles one LoginForm
I have two user roles and I want them to access the backend with one/same login form. (security.yaml: chain_providers ?)
Login with only one entity works perfectly (security.yaml: entity provider)
To avoid the problem that there can be an admin and user with the same email, I would add a role select field (and that's the biggest problem, how do I do this with symfony, CustomFormType ?)
Entities:
AdminEntity
UserEntity
Roles:
ROLE_ADMIN
ROLE_USER
Backend: /backend
Backend Login: /backend/login
Solution 1:[1]
I am not sure if I fully understand your problem, but I think that you need a different approach for your project.
In fact, in UserEntity I suggest to extend UserInterface and PasswordAuthenticatedUserInterface.
class User implements UserInterface, PasswordAuthenticatedUserInterface
# UserInterface.php
interface UserInterface
{
public function getRoles(): array;
As you might see, roles is an array of strings that you can fill with the values ROLE_ADMIN and ROLE_USER.
With this information you may separate functionality in several ways.
You can check if the current user is
Adminand show admin actions per functionality.
This means that you will have code to check the roles and decide what to show or do depending on the roles the user has, allowing even more flexibility because you can add more roles in time. Also means that you will have to plan what roles can and can't do in a smart way.Separate your entire project by diferent routes establishing different firewalls for
usersandadminsAdmin^/backend/adminUsers^/backend
so you might have diferent paths for login but this does not mean that you will need to duplicate code. This means that you will have to check where is this user at the moment. The attribute route can accept a string or array for paths.
Admin^/backend/admin/loginUsers^/backend/login
So, I does not matter the approach you choose, the main objective is always never to duplicate code or logic.
I hope this helps
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marcelo Gonzalez |
