<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User;
use App\Entity\PersonInTrouble;
use App\Entity\UdsCollectedInfo;
use App\Entity\CoesCollectedInfo;
use App\Constant\Enum\UserRoles;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private Security $security;
private RequestStack $requestStack;
private UserPasswordHasherInterface $passwordHasher;
private EntityManagerInterface $entityManager;
public function __construct(Security $security,
RequestStack $request_stack,
UserPasswordHasherInterface $password_hasher,
EntityManagerInterface $entity_manager)
{
$this->security = $security;
$this->requestStack = $request_stack;
$this->passwordHasher = $password_hasher;
$this->entityManager = $entity_manager;
}
public static function getSubscribedEvents() : array
{
return [
BeforeEntityPersistedEvent::class => [
['beforeCreateUser'],
['beforeCreateUdsCollectedInfo'],
['beforeCreateCoesCollectedInfo'],
],
BeforeEntityUpdatedEvent::class => [
['beforeUpdateUdsCollectedInfo'],
['beforeUpdateCoesCollectedInfo']
],
];
}
public function beforeCreateUser(BeforeEntityPersistedEvent $event) : void
{
if (!$this->security->isGranted(UserRoles::ROLE_ADMIN->name))
return;
$entity = $event->getEntityInstance();
if (!$entity instanceof User)
return;
$request = $this->requestStack->getCurrentRequest();
if (!$request->request->has('User'))
return;
$plaintextPassword = ($request->request->get('User'))['password']['first'];
$hashedPassword = $this->passwordHasher->hashPassword($entity, $plaintextPassword);
$entity->setPassword($hashedPassword);
}
public function beforeCreateUdsCollectedInfo(BeforeEntityPersistedEvent $event) : void
{
if (!($this->security->isGranted(UserRoles::ROLE_ADMIN->name) || $this->security->isGranted(UserRoles::ROLE_UDS->name)))
return;
$entity = $event->getEntityInstance();
if (!$entity instanceof UdsCollectedInfo)
return;
$folder = $entity->getFolder();
$personInTrouble = $folder->getPersonInTrouble();
if ((!$personInTrouble instanceof PersonInTrouble) || ($personInTrouble->getId() !== $entity->getPersonInTrouble()->getId()))
{
$folder->setPersonInTrouble($entity->getPersonInTrouble());
$this->entityManager->persist($folder);
}
}
public function beforeUpdateUdsCollectedInfo(BeforeEntityUpdatedEvent $event) : void
{
if (!($this->security->isGranted(UserRoles::ROLE_ADMIN->name) || $this->security->isGranted(UserRoles::ROLE_UDS->name)))
return;
$entity = $event->getEntityInstance();
if (!$entity instanceof UdsCollectedInfo)
return;
$folder = $entity->getFolder();
$personInTrouble = $folder->getPersonInTrouble();
if ((!$personInTrouble instanceof PersonInTrouble) || ($personInTrouble->getId() !== $entity->getPersonInTrouble()->getId()))
{
$folder->setPersonInTrouble($entity->getPersonInTrouble());
}
}
public function beforeCreateCoesCollectedInfo(BeforeEntityPersistedEvent $event) : void
{
if (!($this->security->isGranted(UserRoles::ROLE_ADMIN->name) || $this->security->isGranted(UserRoles::ROLE_COES->name)))
return;
$entity = $event->getEntityInstance();
if (!$entity instanceof CoesCollectedInfo)
return;
$folder = $entity->getFolder();
$folder->setPertinenzaTerritorialeUnitaDiStrada($entity->getPertinenzaTerritorialeUnitaDiStrada());
$this->entityManager->persist($folder);
}
public function beforeUpdateCoesCollectedInfo(BeforeEntityUpdatedEvent $event) : void
{
if (!($this->security->isGranted(UserRoles::ROLE_ADMIN->name) || $this->security->isGranted(UserRoles::ROLE_COES->name)))
return;
$entity = $event->getEntityInstance();
if (!$entity instanceof CoesCollectedInfo)
return;
$folder = $entity->getFolder();
$folder->setPertinenzaTerritorialeUnitaDiStrada($entity->getPertinenzaTerritorialeUnitaDiStrada());
}
}