I
I
Igor2021-02-16 15:31:10
symfony
Igor, 2021-02-16 15:31:10

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_ADMINis 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);


And in I UserVoter
check if the given role allows deleting users.

In general, already advice.
Maybe someone has a better solution.

Zayuzal ACL but in this project it is redundant, and the roles in the classical implementation are not enough.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question