src/LiteDesk/AuditBundle/Request/CurrentUserListener.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * (c) 2011 SimpleThings GmbH
  4.  *
  5.  * @package SimpleThings\EntityAudit
  6.  * @author Benjamin Eberlei <eberlei@simplethings.de>
  7.  * @link http://www.simplethings.de
  8.  *
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  *
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.  * Lesser General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with this library; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22.  */
  23. namespace LiteDesk\AuditBundle\Request;
  24. use Symfony\Component\HttpKernel\HttpKernelInterface;
  25. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  26. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  27. use Symfony\Component\Security\Core\SecurityContext;
  28. use LiteDesk\AuditBundle\AuditConfiguration;
  29. /**
  30.  * Inject the SecurityContext username into the AuditConfiguration as current username.
  31.  */
  32. class CurrentUserListener
  33. {
  34.     /**
  35.      * @var AuditConfiguration
  36.      */
  37.     private $auditConfiguration;
  38.     /**
  39.      * @var TokenStorage
  40.      */
  41.     private $tokenStorage;
  42.     
  43.     public function __construct(AuditConfiguration $configTokenStorage $tokenStorage null)
  44.     {
  45.         $this->auditConfiguration $config;
  46.         $this->tokenStorage $tokenStorage;
  47.     }
  48.     
  49.     /**
  50.      * Handles access authorization.
  51.      *
  52.      * @param GetResponseEvent $event An Event instance
  53.      */
  54.     public function handle(GetResponseEvent $event)
  55.     {
  56.         if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  57.             return;
  58.         }
  59.         if ($this->tokenStorage) {
  60.             $token $this->tokenStorage->getToken();
  61.             if ($token && $token->isAuthenticated()) {
  62.                 $this->auditConfiguration->setCurrentUsername($token->getUsername());
  63.             }
  64.         }
  65.     }
  66. }