sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

[RFC] Allow admin mappers to restrict their contents by role #5584

Closed phansys closed 5 years ago

phansys commented 5 years ago

Feature Request

Allow the following methods to restrict their contents based on a new "role" option passed in their 3rd argument:

I think the easiest approach to achieve this behavior is to extend this condition https://github.com/sonata-project/SonataAdminBundle/blob/64b83e290566dc609a7a30d6a419c1359430dfc9/src/Form/FormMapper.php#L59 with the following check

if (null !== $this->apply && !$this->apply || (isset($fieldDescriptionOptions['role']) && !$this->admin->isGranted($fieldDescriptionOptions['role']))) {

So, if the authenticated user doesn't have the required role, the call to any of these methods will be ignored:

 $showMapper->add('someProp', null, ['role' => 'ROLE_VIEW_SOME_PROP']);
greg0ire commented 5 years ago

Hey Javier, nothing to do with your issue, but can you please check the sf slack? https://symfony-devs.slack.com

Regarding your issue, why not, it might be useful in some situations where you have many properties and you want to hide some of them and they are not sequential. Might spare the user from writing a bunch of if statements.