Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - detect if user is inside a secure firewall

Tags:

php

symfony

I'm trying to find out if a user is inside a secure firewall.

security.yml:

firewalls:
    non_secure_area:
        pattern: ^/
        anonymous: true
    secure_area:
        pattern: ^/admin
        form_login:
            #etc.
        logout:
            #etc.

So I need to know if the user is inside the 'secure_area' secure part of the site.

I have used this, but of course it only tells me if somebody is 'logged in' AND on a HTTPS page. There must be a better way:

if( $request->isSecure() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ) {

} 
like image 559
user2143356 Avatar asked Jul 26 '13 16:07

user2143356


1 Answers

You can get security token and access provider key on it.

$token = $securityContext->getToken();
$providerKey = $token->getProviderKey(); // secured_area

Dont forget to check that token exist and its not an instance of AnonymousToken

like image 116
Alexey B. Avatar answered Oct 21 '22 04:10

Alexey B.