Symfony sensio/framework-extra-bundle is abandoned, what #[Security] alternative?

741 views Asked by At

I saw "Package sensio/framework-extra-bundle is abandoned, you should avoid using it. Use Symfony instead."

but I use it in my Controller :

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

#[Security("is_granted('ROLE_USER') and user === choosenUser")]

#[Route('/movement/user/{id}', name: 'app_movement_user')]
public function.......

What I have to use now to keep Security ? Thanks

1

There are 1 answers

3
Mick3DIY On

From Symfony 6.2, you can use native attribute #[IsGranted] with expressions.

Replace #[Security] by :

//use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Security\Http\Attribute\IsGranted;
    
#[IsGranted(
   new Expression('is_granted("ROLE_USER") and user== ...')
)]
#[Route('/movement/user/{id}', name: 'app_movement_user')]

Sources :

https://symfony.com/blog/new-in-symfony-6-2-built-in-cache-security-template-and-doctrine-attributes

https://symfony.com/doc/current/security.html#access-control-authorization

Discussion in Symfony gitHub :

https://github.com/symfony/symfony/discussions/48878

Using Expressions in Security Access Controls :

https://symfony.com/doc/current/security/expressions.html

Take time to find and read documentation. :)