title: Symfony how to return all logged in Active Users
body: <p>I want to return all <strong>Logged in users</strong> of my application and render it in my Dashboard. The <code>user_id</code> and <code>user_name</code> should be retrieved from the session (I am using an external LDAP Library for authentication)</p>
  
  <p>I have created a field in the database called <code>lastActivity</code> which will contain the last login time and then I can query the database for <code>lastActivity</code>  display users logged in in the last 2 minutes. </p>
  
  <p><strong>ActivityListener.php</strong></p>
  
  <pre><code>     &lt;?php
  
  namespace Bnpp\SecurityBundle\EventListener;
  
  use Doctrine\ORM\EntityManager;
  //use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\SecurityContext;
  use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  use Symfony\Component\HttpKernel\HttpKernel;
  use Acme\SecurityBundle\Entity\User;
  use Doctrine\Bundle\DoctrineBundle\Registry;
  
  
  /**
   * Listener that updates the last activity of the authenticated user
   */
  
  class ActivityListener
  
      {
      protected $securityContext;
      protected $entityManager;
  
      public function __construct(SecurityContext $securityContext, EntityManager $entityManager)
      {
          $this-&gt;securityContext = $securityContext;
          $this-&gt;entityManager = $entityManager;
      }
  
  
  
      /**
       * Update the user "lastActivity" on each request
       * @param FilterControllerEvent $event
       */
  
  
      public function onCoreController(FilterControllerEvent $event)
      {
  
          // Check that the current request is a "MASTER_REQUEST"
          // Ignore any sub-request
          if ($event-&gt;getRequestType() !== HttpKernel::MASTER_REQUEST) {
              return;
          }
  
          // Check token authentication availability
          if ($this-&gt;securityContext-&gt;getToken()) {
              $user = $this-&gt;securityContext-&gt;getToken()-&gt;getUser();
  
  
              if ( ($user instanceof User) &amp;&amp; !($user-&gt;isActiveNow()) ) {
                  $user-&gt;setLastActivity(new \DateTime('now'));
                  $this-&gt;entityManager-&gt;flush($user);
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>Services.yml</p>
  
  <pre><code>services:
      activity_listener:
          class: Bnpp\SecurityBundle\EventListener\ActivityListener
          arguments: [@security.context, @doctrine.orm.entity_manager]
          tags:
           - { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
  </code></pre>
  
  <p>User Entity</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\SecurityBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  
  /**
   * User
   *
   * @ORM\Table(name="users")
   * @ORM\Entity(repositoryClass="Acme\SecurityBundle\Entity\UserRepository")
   */
  class User implements UserInterface
  {
  
  /**
       * @var \DateTime
       * @ORM\Column(name="LASTACTIVITY", type="datetime")
       */
      private $lastActivity;
  
  
      /**
       * @return bool whether the user is active or not
       */
  
      public function isActiveNow()
       {
  
      $delay = new\DateTime('2 minutes ago');
  
      return($this-&gt;getlastActivity()&gt;$delay);
  
         }
  
  /**
       * Set lastActivity
       *
       * @param\Datetime $lastActivity
       * @return User
       */
  
  
      public function setlastActivity($lastActivity)
      {
          $this-&gt;lastActivity = $lastActivity;
  
          return $this;
      }
  
  
      /**
       * Get lastActivity
       *
       * @return \DateTime
       */
      public function getlastActivity()
      {
          return $this-&gt;lastActivity;
      }
  
  
  
  
  }
  </code></pre>