<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Settings\SettingsManager;
use App\Entity\ProfileType;
use App\Entity\Core\Language;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
use App\Events\User\UserCreatedEvent;
class UserSubscriber implements EventSubscriberInterface
{
private $em;
private $security;
private $translator;
private $settingsManager;
private $guardAuthenticatorHandler;
public function __construct(
EntityManagerInterface $em,
Security $security,
TranslatorInterface $translator,
SettingsManager $settingsManager,
GuardAuthenticatorHandler $guardAuthenticatorHandler
) {
$this->em = $em;
$this->security = $security;
$this->translator = $translator;
$this->settingsManager = $settingsManager;
$this->guardAuthenticatorHandler = $guardAuthenticatorHandler;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
UserCreatedEvent::NAME => 'onUserCreatedEvent',
];
}
public function onUserCreatedEvent(UserCreatedEvent $event): void
{
if (!$event->getUser()) {
return;
}
$configs = $this->settingsManager->get([
SettingsManager::USER_RADIUS,
], true);
$user->setRadius((int) $configs[SettingsManager::USER_RADIUS]);
$profileTypes = $this->em->getRepository(ProfileType::class)->findAll();
if ($profileTypes) {
$data = [];
foreach ($profileTypes as $profileType) {
$data[] = $profileType->getId();
}
$user->setProfileTypes($data);
}
$this->em->flush();
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}
$user = $this->security->getUser();
$language = null;
if ($user && $user->getLanguage()) {
$language = $user->getLanguage();
} else {
$preferedLanguage = $event->getRequest()->getPreferredLanguage();
if ($preferedLanguage) {
$language = $this->em->getRepository(Language::class)->findOneBy([
'isoCode' => $preferedLanguage,
]);
}
}
if (!$language) {
$language = $this->em->getRepository(Language::class)->find(
(int) $this->settingsManager->get(SettingsManager::CORE_DEFAULT_LANGUAGE, true)
);
}
$request = $event->getRequest();
$request->setLocale($language->getIsoCode());
$this->translator->setLocale($language->getIsoCode());
if (!$user) {
return ;
}
$user->setActivity(new \DateTime());
$this->em->flush();
if (!in_array('ROLE_SUPER_ADMIN', $user->getRoles()) && !in_array('ROLE_ADMIN', $user->getRoles())) {
$actions = [
SettingsManager::ADMIN_VIEW_PRIVATE_FIELDS,
SettingsManager::ADMIN_TOP_UP_BALANCE,
SettingsManager::ADMIN_EDIT_USER,
SettingsManager::ADMIN_EDIT_PROFILE,
SettingsManager::ADMIN_PAYOUTS,
];
$roles = ['ROLE_USER'];
foreach ($this->settingsManager->get($actions, true) as $role => $groups) {
$groups = json_decode($groups, true);
if ($groups && count($user->getGroups($groups))) {
$roles[] = 'ROLE_MODERATOR';
}
}
if (count(array_intersect($roles, $user->getRoles())) != count($roles) ||
count(array_intersect($roles, $user->getRoles())) != count($user->getRoles())) {
$user->setRoles($roles);
$this->em->flush();
$this->guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken($user, 'secure', $user->getRoles()), $request, 'secure');
}
}
}
}