Note that there are some explanatory texts on larger screens.

plurals
  1. POSilex/Symfony Security Firewall Access user token outside the secured area
    primarykey
    data
    text
    <p>I use Silex and the SecurityProvider, my firewall :</p> <pre><code>$app-&gt;register(new Silex\Provider\SecurityServiceProvider(), array( 'security.firewalls' =&gt; array( 'user' =&gt; array( 'pattern' =&gt; '^/user/', 'form' =&gt; array( 'login_path' =&gt; '/connexion', 'check_path' =&gt; '/user/login_check', 'default_target_path' =&gt; 'homepage_user' ), 'logout' =&gt; array('logout_path' =&gt; '/user/deconnexion') ... ) ) )); </code></pre> <p>It works ! But I didn't find any way to access to the user object in the template, the symfony synthax doesn't work :</p> <pre><code>{{ app.user }} </code></pre> <p>So I add a new global in Twig like this :</p> <pre><code>$app['twig'] = $app-&gt;share($app-&gt;extend('twig', function($twig, $app) { $token = $app['security']-&gt;getToken(); $user = ($token === null) ? null : $token-&gt;getUser(); $twig-&gt;addGlobal('user', $user); return $twig; })); </code></pre> <p>It works but not outside the secured area: $token is null</p> <p>My question is simple : How can I access to the user outside of the secured area ?</p> <p>Thank you</p> <p><strong>EDIT:</strong> I tried to add a firewall with anonymous = true, like this :</p> <pre><code>$app-&gt;register(new Silex\Provider\SecurityServiceProvider(), array( 'security.firewalls' =&gt; array( 'user' =&gt; array( 'pattern' =&gt; '^/user/', 'form' =&gt; array( 'login_path' =&gt; '/connexion', 'check_path' =&gt; '/user/login_check', 'default_target_path' =&gt; 'homepage_user' ), 'logout' =&gt; array('logout_path' =&gt; '/user/deconnexion'), ... ), 'unsecured' =&gt; array( 'anonymous' =&gt; true ) ) )); </code></pre> <p>But it doesn't work, outside of the secured area, when the user is logged, the token is "anon." </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload