vendor/datenwerk/ginger-bundle/EventSubscriber/AdminLogEventSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace DW\GingerBundle\EventSubscriber;
  3. use DW\GingerBundle\DWGingerEvents;
  4. use DW\GingerBundle\Entity\AdminLog;
  5. use DW\GingerBundle\Event\AdminLogEvent;
  6. use DW\GingerBundle\Doctrine\AdminLogManager;
  7. use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. class AdminLogEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
  18.      */
  19.     private $tokenStorage;
  20.     
  21.     /**
  22.      * @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
  23.      */
  24.     private $authorizationChecker;
  25.     
  26.     private ?TokenInterface $token null;
  27.     
  28.     public function __construct(TokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authorizationChecker null)
  29.     {
  30.         $this->tokenStorage $tokenStorage;
  31.         $this->authorizationChecker $authorizationChecker;
  32.     }
  33.     
  34.     /**
  35.      * Add auth-related information to admin log.
  36.      */
  37.     public function addAuthInfo(AdminLogEvent $event)
  38.     {
  39.         if (null === $this->tokenStorage || null === $this->authorizationChecker) {
  40.             return;
  41.         }
  42.         $this->token $this->tokenStorage->getToken();
  43.         if (null !== $this->token && $this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)) {
  44.             $log $event->getAdminLog();
  45.             $this->setChannel($log);
  46.             $this->setUserName($log);
  47.         }
  48.     }
  49.     
  50.     /**
  51.      * Set channel.
  52.      *
  53.      * TODO: Also check one user role in case of UsernamePasswordToken ???
  54.      *
  55.      * @param \DW\GingerBundle\Entity\AdminLog $log
  56.      */
  57.     private function setChannel(AdminLog $log)
  58.     {
  59.         if ($this->token instanceof OAuthToken) {
  60.             $log->setChannel(AdminLogManager::CHANNEL_API_CLIENT);
  61.         } else if ($this->token instanceof UsernamePasswordToken) {
  62.             $log->setChannel(AdminLogManager::CHANNEL_ADMIN_BACKEND);
  63.         }
  64.     }
  65.     
  66.     /**
  67.      * If not username is present, set it from current token.
  68.      *
  69.      * @param \DW\GingerBundle\Entity\AdminLog $log
  70.      */
  71.     private function setUserName(AdminLog $log)
  72.     {
  73.         if(empty($log->getUsername())) {
  74.             $log->setUsername($this->token->getUserIdentifier());
  75.         }
  76.     }
  77.     
  78.     /**
  79.      * @return array|void
  80.      */
  81.     public static function getSubscribedEvents()
  82.     {
  83.         return [
  84.           DWGingerEvents::ADMIN_LOG_PRE_PERSIST => 'addAuthInfo'
  85.         ];
  86.     }
  87. }