Browse Source

ajout du service En ligne

François 6 years ago
parent
commit
3e26f12a69

+ 4 - 0
app/config/services.yml

@@ -33,3 +33,7 @@ services:
     # AppBundle\Service\ExampleService:
     #     arguments:
     #         $someArgument: 'some_value'
+
+    AppBundle\Service\ActivityListener:
+        tags:
+        - { name: 'kernel.event_listener', event: 'kernel.controller', method: onCoreController }

+ 34 - 0
src/AppBundle/Entity/User.php

@@ -72,6 +72,39 @@ class User implements UserInterface
      */
     private $roles = array();
 
+    /**
+     * @var \DateTime
+     *
+     * @ORM\Column(name="last_activity", type="datetime")
+     */
+    private $lastActivity;
+
+    /**
+     * @return \DateTime
+     */
+    public function getLastActivity()
+    {
+        return $this->lastActivity;
+    }
+
+    /**
+     * @param \DateTime $lastActivity
+     */
+    public function setLastActivity($lastActivity)
+    {
+        $this->lastActivity = $lastActivity;
+    }
+
+    /**
+     * @return boolean
+     */
+    public function isActiveNow()
+    {
+        $delay = new \DateTime('2 minutes ago');
+
+        return ( $this->getLastActivity() > $delay );
+    }
+
     /**
      * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Film", mappedBy="usersWantToView")
      * @var \Doctrine\Common\Collections\Collection
@@ -84,6 +117,7 @@ class User implements UserInterface
      */
     private $filmsVus;
 
+
     /**
      * Get id
      *

+ 8 - 0
src/AppBundle/Resources/views/security/liste.html.twig

@@ -9,6 +9,7 @@
     <table class="table table-bordered table-hover table-sm">
         <thead class="thead-dark">
         <tr>
+            <th>En ligne</th>
             <th>Username</th>
             <th>Prénom</th>
             <th>Nom</th>
@@ -20,6 +21,13 @@
         <tbody>
         {% for user in users %}
             <tr>
+                <td >
+                    {% if user.activeNow %}
+                        <span class="badge badge-success">En ligne</span>
+                    {% else %}
+                        <span class="badge badge-danger">Hors ligne</span>
+                    {% endif %}
+                </td>
                 <td>{{ user.username }}</td>
                 <td>{{ user.prenom }}</td>
                 <td>{{ user.nom }}</td>

+ 39 - 0
src/AppBundle/Service/ActivityListener.php

@@ -0,0 +1,39 @@
+<?php
+namespace AppBundle\Service;
+
+use AppBundle\Entity\User;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
+use Symfony\Component\HttpKernel\HttpKernel;
+use Symfony\Component\Security\Core\Security;
+
+class ActivityListener
+{
+    private $em;
+    private $security;
+
+    public function __construct(EntityManagerInterface $em, Security $security)
+    {
+        $this->em = $em;
+        $this->security = $security;
+    }
+
+    public function onCoreController(FilterControllerEvent $event)
+    {
+        // Check that the current request is a "MASTER_REQUEST"
+        // Ignore any sub-request
+        if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
+            return;
+        }
+
+        // Check token authentication availability
+        if ($this->security->getToken()) {
+            $user = $this->security->getToken()->getUser();
+
+            if ( ($user instanceof User) && !($user->isActiveNow()) ) {
+                $user->setLastActivity(new \DateTime());
+                $this->em->flush();
+            }
+        }
+    }
+}