Mirage
Mirage

Reputation: 31548

Is it possible to secure whole Controller in Symfony 2?

I am using JMSSecurityExtra bundle for securing methods in my contoller. But is there any way that i can secure the whole controller with @Secure?

Upvotes: 2

Views: 2142

Answers (2)

Mirage
Mirage

Reputation: 31548

This can be done as per Documentaion

https://github.com/schmittjoh/JMSSecurityExtraBundle/issues/50

Tip: If you like to secure all actions of the controller with the same rule, you may also specify @PreAuthorize on the class itself. Caution though, this rule is only applied to the methods which are declared in the class.

use JMS\SecurityExtraBundle\Annotation\PreAuthorize;

 /** @PreAuthorize("hasRole('A') or (hasRole('B') and hasRole('C'))") */
class MyService
{

    public function secureMethod()
    {
        // ...
    }
}

Upvotes: 6

AdrienBrault
AdrienBrault

Reputation: 7745

This annotation can only be applied to methods.

You could do it like this though (it's a regex):

jms_security_extra:
    method_access_control:
        'AcmeDemoBundle:AdminController:.*Action': 'hasRole("ROLE_ADMIN")'

Read the documentation: http://jmsyst.com/bundles/JMSSecurityExtraBundle/master/method_security_authorization

Upvotes: 1

Related Questions