Skip to content

Commit

Permalink
Added custom event to fix email confirmation system (#156)
Browse files Browse the repository at this point in the history
* Added custom event to fix email confirmation system

* Update UserFormEvent.php

Use `FormEvent` as base class

* Update composer.json

Updated user bundle dependency

* Fix CS

* Make event class final

Co-authored-by: Christian Gripp <core23@users.noreply.github.com>
  • Loading branch information
fkrauthan and core23 authored Jan 17, 2021
1 parent b8ba963 commit 78d8003
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": "^7.3 || ^8.0",
"egulias/email-validator": "^2.1.10",
"nucleos/user-bundle": "^1.6",
"nucleos/user-bundle": "^1.7",
"symfony/config": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/event-dispatcher": "^4.4 || ^5.0",
Expand Down
3 changes: 2 additions & 1 deletion src/Action/RegistrationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Nucleos\ProfileBundle\Action;

use Nucleos\ProfileBundle\Event\GetResponseRegistrationEvent;
use Nucleos\ProfileBundle\Event\UserFormEvent;
use Nucleos\ProfileBundle\Form\Model\Registration;
use Nucleos\ProfileBundle\Form\Type\RegistrationFormType;
use Nucleos\ProfileBundle\NucleosProfileEvents;
Expand Down Expand Up @@ -119,7 +120,7 @@ private function updateUser(Request $request, Registration $formModel, FormInter
{
$user = $formModel->toUser($this->userManager);

$event = new FormEvent($form, $request);
$event = new UserFormEvent($user, $form, $request);
$this->eventDispatcher->dispatch($event, NucleosProfileEvents::REGISTRATION_SUCCESS);

$this->userManager->updateUser($user);
Expand Down
37 changes: 37 additions & 0 deletions src/Event/UserFormEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the NucleosProfileBundle package.
*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\ProfileBundle\Event;

use Nucleos\UserBundle\Event\FormEvent ;
use Nucleos\UserBundle\Model\UserInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class UserFormEvent extends FormEvent
{
/**
* @var UserInterface
*/
private $user;

public function __construct(UserInterface $user, FormInterface $form, Request $request)
{
parent::__construct($form, $request);
$this->user = $user;
}

public function getUser(): UserInterface
{
return $this->user;
}
}
12 changes: 3 additions & 9 deletions src/EventListener/EmailConfirmationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

namespace Nucleos\ProfileBundle\EventListener;

use Nucleos\ProfileBundle\Event\UserFormEvent;
use Nucleos\ProfileBundle\Mailer\MailerInterface;
use Nucleos\ProfileBundle\NucleosProfileEvents;
use Nucleos\UserBundle\Event\FormEvent;
use Nucleos\UserBundle\Model\UserInterface;
use Nucleos\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -65,14 +64,9 @@ public static function getSubscribedEvents(): array
];
}

public function onRegistrationSuccess(FormEvent $event): void
public function onRegistrationSuccess(UserFormEvent $event): void
{
$user = $event->getForm()->getData();

if (!$user instanceof UserInterface) {
return;
}

$user = $event->getUser();
$user->setEnabled(false);

if (null === $user->getConfirmationToken()) {
Expand Down
2 changes: 1 addition & 1 deletion src/NucleosProfileEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class NucleosProfileEvents
*
* This event allows you to set the response instead of using the default one.
*
* @Event("Nucleos\UserBundle\Event\FormEvent")
* @Event("Nucleos\ProfileBundle\Event\UserFormEvent")
*/
public const REGISTRATION_SUCCESS = 'nucleos_profile.registration.success';

Expand Down

0 comments on commit 78d8003

Please sign in to comment.