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

Documentation talks about extending final RoleSecurityHandler #7533

Closed AntoineRoue closed 3 years ago

AntoineRoue commented 3 years ago

Hello,

I just noticed today that the Security documentation says that "If you want to change the handler behavior, extend Sonata\AdminBundle\Security\Handler\RoleSecurityHandler, and override the isGranted method." However, this class is final in 4.x and marked final since sonata-project/admin-bundle 3.52 in 3.x. So it is impossible.

VincentLanglet commented 3 years ago

Hi, can you provide a PR to update the doc ?

Instead of extending the class, you need to create your own handler, implementing https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Security/Handler/SecurityHandlerInterface.php

I think you can use service decoration https://symfony.com/doc/current/service_container/service_decoration.html but you can also just copy the logic you need and override the logic you want to change

AntoineRoue commented 3 years ago

I'll try your suggestions and update the doc. If I create my own handler implementing SecurityHandlerInterface, how is it going to be called instead of RoleSecurityHandler ?

VincentLanglet commented 3 years ago

You can override the admin config to use your own securityHandler by following https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/cookbook/recipe_overwrite_admin_configuration/#overwrite-admin-configuration

with the key security_handler

AntoineRoue commented 3 years ago

Ok. I'll explain this way in the doc then, I think it is a bit simpler than with a decorator. Also, the documentation says "If you want to change the handler behavior (for example, to pass the current object to voters)". The example does not make sense because the current object is already passed to voters. So I'll also remove it.