title: Symfony 2.8 Twig extension based on current logged user
body: <p>What I want to do is show logged user only content that he has access to.</p>
  
  <p>First thing I was doing  <strong>access_control</strong> in <strong>security.yml</strong> &amp; redirect but i must do at least 30 deferent accounts;/ </p>
  
  <p>Next i create twig extension that will connect to DB and get current logged user specific settings - 
  access to panels. Is this good way? </p>
  
  <p>The problem is  </p>
  
  <pre><code>$user = $this-&gt;getUser()-&gt;getId();
  $currentUser = $this-&gt;em-&gt;getRepository('AppBundle:User')-&gt;find($user);
  </code></pre>
  
  <p>It will not work, Blank page appears in dev env</p>
  
  <p>But when i put 1</p>
  
  <pre><code>$currentUser = $this-&gt;em-&gt;getRepository('AppBundle:User')-&gt;find(1);
  </code></pre>
  
  <p>&amp; 1 is user id everything is ok. </p>
  
  <p><strong>services.yml</strong></p>
  
  <pre><code>    app.twig.users_extension:
      class: AppBundle\Twig\Extension\AccesExtension
      arguments: ["@doctrine.orm.entity_manager","@security.token_storage"]
      tags:
          - { name: twig.extension }
  </code></pre>
  
  <p><strong>Twig Extension</strong></p>
  
  <pre><code>class AccesExtension extends \Twig_Extension
  {
  
  protected $em;
  protected $tokenStorage;
  
  
  public function __construct(EntityManager $em, TokenStorage $tokenStorage)
  {
      $this-&gt;em = $em;
      $this-&gt;tokenStorage = $tokenStorage;
  }
  
  public function getUser()
  {
      return $this-&gt;tokenStorage-&gt;getToken()-&gt;getUser();
  }
  
  public function getGlobals()
  {
      $user = $this-&gt;getUser()-&gt;getId();
      $currentUser = $this-&gt;em-&gt;getRepository('AppBundle:User')-&gt;find($user);
      return array (
          "acces" =&gt; $currentUser,
      );
  }
  
  public function getName()
  {
      return "AppBundle:AccesExtension ";
  }
  </code></pre>
  
  <p>}</p>