src/EventSubscriber/UserSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\User;
  5. use App\Repository\UserRepository;
  6. use App\User\UserActivation;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\String\ByteString;
  16. final class UserSubscriber implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private UserActivation $userActivation,
  20.         private Security $security,
  21.         private LoggerInterface $logger,
  22.         private EntityManagerInterface $entityManager,
  23.         private UserPasswordHasherInterface $passwordHasher)
  24.     {
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => [
  30.                 ['postWriteUser'EventPriorities::POST_WRITE],
  31.                 ['preWriteUser'EventPriorities::PRE_WRITE]
  32.             ],
  33.         ];
  34.     }
  35.     public function preWriteUser(ViewEvent $event): void
  36.     {
  37.         $user $event->getControllerResult();
  38.         $method $event->getRequest()->getMethod();
  39.         if (!($user instanceof User)) {
  40.             return;
  41.         }
  42.         $password null;
  43.         if (Request::METHOD_PUT === $method) {
  44.             $unitOfWork $this->entityManager->getUnitOfWork();
  45.             $originalEntityData $unitOfWork->getOriginalEntityData($user);
  46.             // If the user went from disabled to enabled
  47.             if (!empty($originalEntityData) &&
  48.                 isset($originalEntityData['isEnabled']) &&
  49.                 $originalEntityData['isEnabled'] === false &&
  50.                 $user->isIsEnabled() === true) {
  51.                 // set unlocked to null and reset attempts
  52.                 $user->setLockedAt(null);
  53.                 $user->setAttempts(0);
  54.             }
  55.         }
  56.         if (Request::METHOD_POST == $method){
  57.             $password ByteString::fromRandom(10)->toString();
  58.             $user->setPlainPassword($password);
  59.             // $password = "testtest";
  60.             $user->setIsEnabled(true);
  61. //            $user->addRole(User::ROLE_USER);
  62.         }
  63.         else if ($user->getPlainPassword()) {
  64.             $password $user->getPlainPassword();
  65.         }
  66.         if ($password) {
  67.             $hashedPassword $this->passwordHasher->hashPassword(
  68.                 $user,
  69.                 $password
  70.             );
  71.             $user->setPassword($hashedPassword);
  72.         }
  73.     }
  74.     public function postWriteUser(ViewEvent $event): void
  75.     {
  76.         $user $event->getControllerResult();
  77.         $method $event->getRequest()->getMethod();
  78.         if (!($user instanceof User)|| (Request::METHOD_POST !== $method)) {
  79.             return;
  80.         }
  81.         $this->userActivation->sendActivationEmail($user);
  82.     }
  83. }