Answer the question
In order to leave comments, you need to log in
Why does one user have many roles?
Why does one user have many roles in Symfony?
How can we say for sure who is in front of us?
public function getRoles(): array
{
$roles = $this->roles ?? [];
// guarantee every user at least has ROLE_OPERATOR
$roles[] = 'ROLE_OPERATOR';
return array_unique($roles);
}
0: "ROLE_ADMIN"
1: "ROLE_OPERATOR"
Answer the question
In order to leave comments, you need to log in
1. In the symphony, the roles are more advanced. They are, as it were, permissions at the same time, which allows you to work more flexibly with them out of the box. That is, somewhere you can simply use ROLE_ADMIN, ROLE_USER and this will be enough, but more often in projects more complex access rights, where you can have an admin on a blog, an admin in a store, etc. You can also use a role hierarchy where having some roles automatically gives you other roles.
To determine who is in front of you is the answer to your domain, and these roles show what rights he has. In this case, the operator and the administrator.
2. How to give information to the front? Check if there is a given role in the array, here, I think, everything is simple. Again, I repeat that these are more sophisticated roles, that's why. At the front, in general, a specific role is not always needed. Most often, permissions are sent to the front, according to which they check what is available for display and what is not.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question