Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 get firewall name on login page

I'd want to use a login page to access different firewalls, so I need to get information about the firewall I'm logging in. In my controller I'd use

$this->container->get('security.context')->getToken()->getProviderKey()

but as an anonymous user I don't have access to getProviderKey method. I could also parse

_security.xxx.target_path 

to get xxx firewall but I'm looking for a more general solution if it exists at all.
Any idea?

like image 277
Matteo Rossi Avatar asked Apr 17 '14 08:04

Matteo Rossi


3 Answers

As of symfony 3.2, you can now get the current firewall configuration using the following:

public function indexAction(Request $request)
{
    $firewall = $this->container
        ->get('security.firewall.map')
        ->getFirewallConfig($request)
        ->getName();
}

Ref: http://symfony.com/blog/new-in-symfony-3-2-firewall-config-class-and-profiler

like image 126
chalasr Avatar answered Nov 07 '22 13:11

chalasr


For Symfony 3.4 I wrote this to avoid referencing the non-public "security.firewall.map" service:

$firewallName = null;
if (($firewallContext = trim($request->attributes->get("_firewall_context", null))) && (false !== ($firewallContextNameSplit = strrpos($firewallContext, ".")))) {
    $firewallName = substr($firewallContext, $firewallContextNameSplit + 1);
}

(Referencing "security.firewall.map" on 3.4 will throw an exception.)

Edit: This will not work in a custom exception controller function.

like image 32
Adambean Avatar answered Nov 07 '22 12:11

Adambean


I was doing a little research on this myself recently so that I could send this information in an XACML request as part of the environment.

As far as I can tell from GitHub issues like this one:

https://github.com/symfony/symfony/issues/14435

There is currently no way to reliably get the information out of Symfony except the dirty compiler pass hack suggested on the linked issue. It does appear from the conversation on these issues, they are working on making this available, however, the status is still open, so we will have to be patient and wait for it to be provided.

like image 33
Reid Johnson Avatar answered Nov 07 '22 13:11

Reid Johnson