src/EventSubscriber/User/UserSubscriber.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\User;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use App\Settings\SettingsManager;
  17. use App\Entity\ProfileType;
  18. use App\Entity\Core\Language;
  19. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  20. use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
  21. use App\Events\User\UserCreatedEvent;
  22. class UserSubscriber implements EventSubscriberInterface
  23. {
  24.     private $em;
  25.     private $security;
  26.     private $translator;
  27.     private $settingsManager;
  28.     private $guardAuthenticatorHandler;
  29.     public function __construct(
  30.         EntityManagerInterface $em,
  31.         Security $security,
  32.         TranslatorInterface $translator,
  33.         SettingsManager $settingsManager,
  34.         GuardAuthenticatorHandler $guardAuthenticatorHandler
  35.     ) {
  36.         $this->em $em;
  37.         $this->security $security;
  38.         $this->translator $translator;
  39.         $this->settingsManager $settingsManager;
  40.         $this->guardAuthenticatorHandler $guardAuthenticatorHandler;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             KernelEvents::REQUEST => 'onKernelRequest',
  46.             UserCreatedEvent::NAME => 'onUserCreatedEvent',
  47.         ];
  48.     }
  49.     public function onUserCreatedEvent(UserCreatedEvent $event): void
  50.     {
  51.         if (!$event->getUser()) {
  52.             return;
  53.         }
  54.         $configs $this->settingsManager->get([
  55.             SettingsManager::USER_RADIUS,
  56.         ], true);
  57.         $user->setRadius((int) $configs[SettingsManager::USER_RADIUS]);
  58.         $profileTypes $this->em->getRepository(ProfileType::class)->findAll();
  59.         if ($profileTypes) {
  60.             $data = [];
  61.             foreach ($profileTypes as $profileType) {
  62.                 $data[] = $profileType->getId();
  63.             }
  64.             $user->setProfileTypes($data);
  65.         }
  66.         $this->em->flush();
  67.     }
  68.     public function onKernelRequest(RequestEvent $event): void
  69.     {
  70.         if (!$event->isMasterRequest()) {
  71.             return;
  72.         }
  73.         $user $this->security->getUser();
  74.         $language null;
  75.         if ($user && $user->getLanguage()) {
  76.             $language $user->getLanguage();
  77.         } else {
  78.             $preferedLanguage $event->getRequest()->getPreferredLanguage();
  79.             if ($preferedLanguage) {
  80.                 $language $this->em->getRepository(Language::class)->findOneBy([
  81.                     'isoCode' => $preferedLanguage,
  82.                 ]);
  83.             }
  84.         }
  85.         if (!$language) {
  86.             $language $this->em->getRepository(Language::class)->find(
  87.                 (int) $this->settingsManager->get(SettingsManager::CORE_DEFAULT_LANGUAGEtrue)
  88.             );
  89.         }
  90.         $request $event->getRequest();
  91.         $request->setLocale($language->getIsoCode());
  92.         $this->translator->setLocale($language->getIsoCode());
  93.     
  94.         if (!$user) {
  95.             return ;
  96.         }
  97.         $user->setActivity(new \DateTime());
  98.         $this->em->flush();
  99.         if (!in_array('ROLE_SUPER_ADMIN'$user->getRoles()) && !in_array('ROLE_ADMIN'$user->getRoles())) {
  100.             $actions = [
  101.                 SettingsManager::ADMIN_VIEW_PRIVATE_FIELDS,
  102.                 SettingsManager::ADMIN_TOP_UP_BALANCE,
  103.                 SettingsManager::ADMIN_EDIT_USER,
  104.                 SettingsManager::ADMIN_EDIT_PROFILE,
  105.                 SettingsManager::ADMIN_PAYOUTS,
  106.             ];
  107.             $roles = ['ROLE_USER'];
  108.             foreach ($this->settingsManager->get($actionstrue) as $role => $groups) {
  109.                 $groups json_decode($groupstrue);
  110.                 if ($groups && count($user->getGroups($groups))) {
  111.                     $roles[] = 'ROLE_MODERATOR';
  112.                 }
  113.             }
  114.             if (count(array_intersect($roles$user->getRoles())) != count($roles) ||
  115.                 count(array_intersect($roles$user->getRoles())) != count($user->getRoles())) {
  116.                 $user->setRoles($roles);
  117.                 $this->em->flush();
  118.                 $this->guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken($user'secure'$user->getRoles()), $request'secure');
  119.             }
  120.         }
  121.     }
  122. }