Answer the question
In order to leave comments, you need to log in
SonataAdminBundle. What is the best way to check the user's role on a certain action?
The task is next.
There is a news section where there are moderators and administrators. Moderators can add news, but cannot publish them. Administrators, of course, can do this.
The most interesting thing is that it's just a checkmark in the add/edit form. It turns out that you need to show / hide this checkbox when the user has / does not have the "publish" role.
It turns out the question arises: which way to go to add a check for the role of "publishing"?.
As a perversion, you can create a listener that will intercept prePersist / preUpdate and check if there is a "publish" checkbox and look at the user's role.
How would you implement something like this?
Answer the question
In order to leave comments, you need to log in
if you are doing it through forms, you can check the role and not add this field if this check fails
here is a piece of code for inspiration (only security.context will need to be passed to the form if you have them in a separate file):
symfony.com/doc /current/book/security.html#securin...
well, according to the correct check, you need to do this:
and implement the check itself through SecurityVoter:
symfony.com/doc/current/cookbook/security/voters.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question