Answer the question
In order to leave comments, you need to log in
How to implement a dynamic role in Symfony?
I welcome colleagues.
I want to increase the versatility of the system being created.
Now I use roles ROLE_USER, ROLE_ADMIN, ...
I also use Voters
.
But the problem lies in the fact that it ROLE_USER, ROLE_ADMIN
is tightly connected with the business logic.
Hence the problems, for example, the introduction of a new role requires additional changes in the code.
Further we go, worse it becomes.
What do I want to do.
I want to implement the ability to create a dynamic role.
That is, the administrator clicks to create a new role and configures it accordingly.
But here one unpleasant problem arises, in the interface I have to know that this particular role is an administrator or someone else in order to show and hide blocks depending on the role.
My Solution:
User
id = 1
name = 'Vasya'
role = 1
Role
id = 1
name = 'Administrator'
permissions = [''user.create, 'user.delete']
And in the controller
$this->denyAccessUnlessGranted(UserVoter::DELETE, $user);
UserVoter
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question