From 2d4181b9d494d795a1acac73826a1ee7a67fc3c6 Mon Sep 17 00:00:00 2001 From: aritz Date: Tue, 20 Jan 2026 15:59:42 +0100 Subject: [PATCH 1/5] Mailer exceptions --- .../Admin/InvitationsController.php | 24 +++++++++++++---- .../Settings/ConfirmationController.php | 27 ++++++++++++++----- .../Admin/FlashMessagesListener.php | 24 +++++++++++++++++ src/EventListener/EmailInvitationListener.php | 7 +++-- .../SendResetPasswordEmailListener.php | 15 ++++++----- .../UserRegistrationListener.php | 17 +++++++----- src/SfsUserEvents.php | 2 ++ translations/sfs_user.en.yaml | 6 +++++ translations/sfs_user.es.yaml | 4 +++ 9 files changed, 100 insertions(+), 26 deletions(-) diff --git a/src/Controller/Admin/InvitationsController.php b/src/Controller/Admin/InvitationsController.php index 305ba85..9a89f75 100644 --- a/src/Controller/Admin/InvitationsController.php +++ b/src/Controller/Admin/InvitationsController.php @@ -2,9 +2,12 @@ namespace Softspring\UserBundle\Controller\Admin; +use http\Client\Request; use Softspring\Component\Events\DispatchGetResponseTrait; +use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; use Softspring\UserBundle\Manager\UserInvitationManagerInterface; +use Softspring\UserBundle\SfsUserEvents; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Response; @@ -34,14 +37,25 @@ public function pendingCountWidget(): Response ]); } - public function resendEmail(mixed $invitation): Response + public function resendEmail(mixed $invitation, Request $request): Response { - $invitation = $this->invitationsManager->findInvitationBy(['id' => $invitation]); - - if (!$invitation->getAcceptedAt() && $this->userMailer) { - $this->userMailer->sendInvitationEmail($invitation); + try { + $invitation = $this->invitationsManager->findInvitationBy(['id' => $invitation]); + $user = $invitation->getUser(); + + if (!$invitation->getAcceptedAt() && $this->userMailer) { + $this->userMailer->sendInvitationEmail($invitation); + if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_INVITATIONS_RESEND_SUCCESS, new GetResponseUserEvent($user, $request))) { + return $response; + } + } + } catch (\Exception $e) { + if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_INVITATIONS_RESEND_ERROR, new GetResponseUserEvent($user, $request))) { + return $response; + } } + return $this->redirectToRoute('sfs_user_admin_invitations_details', ['invitation' => $invitation]); } } diff --git a/src/Controller/Settings/ConfirmationController.php b/src/Controller/Settings/ConfirmationController.php index 62f215c..a71a816 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -3,14 +3,17 @@ namespace Softspring\UserBundle\Controller\Settings; use Softspring\Component\Events\DispatchGetResponseTrait; +use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; use Softspring\UserBundle\Manager\UserManagerInterface; use Softspring\UserBundle\Model\ConfirmableInterface; use Softspring\UserBundle\Model\UserInterface; +use Softspring\UserBundle\SfsUserEvents; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; class ConfirmationController extends AbstractController { @@ -21,21 +24,33 @@ class ConfirmationController extends AbstractController protected ?UserMailerInterface $userMailer; protected EventDispatcherInterface $eventDispatcher; + protected ?FlashBagInterface $flashBag; - public function __construct(UserManagerInterface $userManager, ?UserMailerInterface $userMailer, EventDispatcherInterface $eventDispatcher) + public function __construct(UserManagerInterface $userManager, ?UserMailerInterface $userMailer, EventDispatcherInterface $eventDispatcher, ?FlashBagInterface $flashBag) { $this->userManager = $userManager; $this->userMailer = $userMailer; $this->eventDispatcher = $eventDispatcher; + $this->flashBag = $flashBag; } public function resendConfirmation(Request $request): Response { - /** @var UserInterface $user */ - $user = $this->getUser(); - - if ($user instanceof ConfirmableInterface && !$user->isConfirmed()) { - $this->userMailer->sendRegisterConfirmationEmail($user); + try { + /** @var UserInterface $user */ + $user = $this->getUser(); + + if ($user instanceof ConfirmableInterface && !$user->isConfirmed()) { + $this->userMailer->sendRegisterConfirmationEmail($user); + if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_SUCCESS, new GetResponseUserEvent($user, $request))) { + return $response; + } + } + + } catch (\Exception $e) { + if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_ERROR, new GetResponseUserEvent($user, $request))) { + return $response; + } } return $this->redirect($request->server->get('HTTP_REFERER') ?? $this->generateUrl('sfs_user_preferences')); diff --git a/src/EventListener/Admin/FlashMessagesListener.php b/src/EventListener/Admin/FlashMessagesListener.php index 3ab93dc..1a7abed 100644 --- a/src/EventListener/Admin/FlashMessagesListener.php +++ b/src/EventListener/Admin/FlashMessagesListener.php @@ -27,6 +27,8 @@ public static function getSubscribedEvents(): array SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_SUCCESS => 'onResendConfirmationSuccess', SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_ERROR => 'onResendConfirmationError', SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_ALREADY_CONFIRMED => 'onResendConfirmationAlready', + SfsUserEvents::ADMIN_INVITATIONS_RESEND_SUCCESS => 'onResendInvitationsSuccess', + SfsUserEvents::ADMIN_INVITATIONS_RESEND_ERROR => 'onResendInvitationsError', ]; } @@ -63,6 +65,28 @@ public function onResendConfirmationAlready(GetResponseUserEvent $event): void ], 'sfs_user', $locale); } + public function onResendInvitationsSuccess(GetResponseUserEvent $event) + { + $user = $event->getUser(); + $locale = $event->getRequest()->getLocale(); + + $this->addFlash('success', 'admin_users.resend_invitaion.messages.success', [ + '%username%' => $user->getDisplayName(), + '%email%' => $user instanceof UserWithEmailInterface ? $user->getEmail() : $user->getDisplayName(), + ], 'sfs_user', $locale); + } + + public function onResendInvitationsError(GetResponseUserEvent $event) + { + $user = $event->getUser(); + $locale = $event->getRequest()->getLocale(); + + $this->addFlash('error', 'admin_users.resend_invitaion.messages.error', [ + '%username%' => $user->getDisplayName(), + '%email%' => $user instanceof UserWithEmailInterface ? $user->getEmail() : $user->getDisplayName(), + ], 'sfs_user', $locale); + } + protected function addFlash(string $type, string $trans, array $transParams = [], string $domain = 'sfs_user', string $locale = 'en'): void { if (!$this->flashBag) { diff --git a/src/EventListener/EmailInvitationListener.php b/src/EventListener/EmailInvitationListener.php index 8d2a5ec..323d304 100644 --- a/src/EventListener/EmailInvitationListener.php +++ b/src/EventListener/EmailInvitationListener.php @@ -25,8 +25,11 @@ public static function getSubscribedEvents(): array public function onInvitation(UserInvitationEvent $event): void { - $invitation = $event->getInvitation(); + try { + $invitation = $event->getInvitation(); - $this->mailer->sendInvitationEmail($invitation); + $this->mailer->sendInvitationEmail($invitation); + } catch (\Exception $e) { + } } } diff --git a/src/EventListener/SendResetPasswordEmailListener.php b/src/EventListener/SendResetPasswordEmailListener.php index 5ca282b..629f10b 100644 --- a/src/EventListener/SendResetPasswordEmailListener.php +++ b/src/EventListener/SendResetPasswordEmailListener.php @@ -57,13 +57,16 @@ public function onResetRequestCheckToken(GetResponseFormEvent $event): void public function sendResetEmail(GetResponseFormEvent $event): void { - /** @var UserInterface $user */ - $user = $this->userManager->findUserBy(['email' => $event->getForm()->get('email')->getData()]); + try { + /** @var UserInterface $user */ + $user = $this->userManager->findUserBy(['email' => $event->getForm()->get('email')->getData()]); - if (!$user instanceof PasswordRequestInterface) { - return; - } + if (!$user instanceof PasswordRequestInterface) { + return; + } - $this->mailer->sendResettingEmail($user); + $this->mailer->sendResettingEmail($user); + } catch (\Exception $e) { + } } } diff --git a/src/EventListener/UserRegistrationListener.php b/src/EventListener/UserRegistrationListener.php index 4ef497d..4c33c8c 100644 --- a/src/EventListener/UserRegistrationListener.php +++ b/src/EventListener/UserRegistrationListener.php @@ -34,14 +34,17 @@ public static function getSubscribedEvents(): array public function onRegisterSendConfirmationEmail(GetResponseUserEvent $event): void { - $user = $event->getUser(); - if (!$user instanceof ConfirmableInterface) { - return; - } + try { + $user = $event->getUser(); + if (!$user instanceof ConfirmableInterface) { + return; + } - $user->setConfirmationToken($this->tokenGenerator->generateToken()); - $this->userManager->saveEntity($user); + $user->setConfirmationToken($this->tokenGenerator->generateToken()); + $this->userManager->saveEntity($user); - $this->mailer->sendRegisterConfirmationEmail($user); + $this->mailer->sendRegisterConfirmationEmail($user); + } catch (\Exception $e) { + } } } diff --git a/src/SfsUserEvents.php b/src/SfsUserEvents.php index f9c2812..2c6c77f 100644 --- a/src/SfsUserEvents.php +++ b/src/SfsUserEvents.php @@ -258,6 +258,8 @@ class SfsUserEvents public const ADMIN_INVITATIONS_CREATE_SUCCESS = 'sfs_user.admin.invitations.create_success'; public const ADMIN_INVITATIONS_CREATE_FORM_INVALID = 'sfs_user.admin.invitations.create_form_invalid'; public const ADMIN_INVITATIONS_CREATE_VIEW = 'sfs_user.admin.invitations.create_view'; + public const ADMIN_INVITATIONS_RESEND_SUCCESS = 'sfs_user.admin.invitations.resend_success'; + public const ADMIN_INVITATIONS_RESEND_ERROR = 'sfs_user.admin.invitations.resend__error'; /** @Event("Softspring\Component\Events\GetResponseRequestEvent") */ public const ADMIN_ACCESS_HISTORY_LIST_INITIALIZE = 'sfs_user.admin.access_history.list_initialize'; diff --git a/translations/sfs_user.en.yaml b/translations/sfs_user.en.yaml index 6854ca2..f053c37 100644 --- a/translations/sfs_user.en.yaml +++ b/translations/sfs_user.en.yaml @@ -205,6 +205,12 @@ admin_users: error: "An error has occurred sending the confirmation email, try it again. If the problem persist contact to support." already_confirmed: "The email has not been send, user %username% is already confirmed" + resend_invitation: + messages: + success: "The invitation email has been sent to %email%" + error: "An error has occurred sending the invitation email, try it again. If the problem persist contact to support." + + admin_administrators: list: meta.title: "Administrators" diff --git a/translations/sfs_user.es.yaml b/translations/sfs_user.es.yaml index 1760e0b..6a8529e 100644 --- a/translations/sfs_user.es.yaml +++ b/translations/sfs_user.es.yaml @@ -204,6 +204,10 @@ admin_users: success: "El email de confirmación se ha enviado a %email%" error: "Se ha producido un error enviando el correo de confirmación, intentalo de nuevo. Si el problema persiste contacta con el soporte." already_confirmed: "No se ha enviado el email de confirmación, el usuario %username% ya está confirmado" + resend_invitation: + messages: + success: "El email de invitación se ha enviado a %email%" + error: "Se ha producido un error enviando el correo de invitación, intentalo de nuevo. Si el problema persiste contacta con el soporte." admin_administrators: list: From 53e18a8fc79c45cbf0169cf1bf35804146c6fbef Mon Sep 17 00:00:00 2001 From: aritz Date: Tue, 20 Jan 2026 16:01:06 +0100 Subject: [PATCH 2/5] Mailer exceptions --- src/Controller/Settings/ConfirmationController.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Controller/Settings/ConfirmationController.php b/src/Controller/Settings/ConfirmationController.php index a71a816..95d54d9 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -24,14 +24,12 @@ class ConfirmationController extends AbstractController protected ?UserMailerInterface $userMailer; protected EventDispatcherInterface $eventDispatcher; - protected ?FlashBagInterface $flashBag; - public function __construct(UserManagerInterface $userManager, ?UserMailerInterface $userMailer, EventDispatcherInterface $eventDispatcher, ?FlashBagInterface $flashBag) + public function __construct(UserManagerInterface $userManager, ?UserMailerInterface $userMailer, EventDispatcherInterface $eventDispatcher) { $this->userManager = $userManager; $this->userMailer = $userMailer; $this->eventDispatcher = $eventDispatcher; - $this->flashBag = $flashBag; } public function resendConfirmation(Request $request): Response From 7cfbe72f2f1e9f86a1e1ca9018449e309e040128 Mon Sep 17 00:00:00 2001 From: aritz Date: Tue, 20 Jan 2026 16:06:12 +0100 Subject: [PATCH 3/5] Mailer exceptions --- src/Controller/Admin/InvitationsController.php | 2 +- src/Controller/Settings/ConfirmationController.php | 5 ++--- src/EventListener/Admin/FlashMessagesListener.php | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Controller/Admin/InvitationsController.php b/src/Controller/Admin/InvitationsController.php index 9a89f75..c40e97f 100644 --- a/src/Controller/Admin/InvitationsController.php +++ b/src/Controller/Admin/InvitationsController.php @@ -2,7 +2,7 @@ namespace Softspring\UserBundle\Controller\Admin; -use http\Client\Request; +use Symfony\Component\HttpFoundation\Request; use Softspring\Component\Events\DispatchGetResponseTrait; use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; diff --git a/src/Controller/Settings/ConfirmationController.php b/src/Controller/Settings/ConfirmationController.php index 95d54d9..d6c71d3 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -34,10 +34,9 @@ public function __construct(UserManagerInterface $userManager, ?UserMailerInterf public function resendConfirmation(Request $request): Response { + /** @var UserInterface $user */ + $user = $this->getUser(); try { - /** @var UserInterface $user */ - $user = $this->getUser(); - if ($user instanceof ConfirmableInterface && !$user->isConfirmed()) { $this->userMailer->sendRegisterConfirmationEmail($user); if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_SUCCESS, new GetResponseUserEvent($user, $request))) { diff --git a/src/EventListener/Admin/FlashMessagesListener.php b/src/EventListener/Admin/FlashMessagesListener.php index 1a7abed..e9968b8 100644 --- a/src/EventListener/Admin/FlashMessagesListener.php +++ b/src/EventListener/Admin/FlashMessagesListener.php @@ -65,7 +65,7 @@ public function onResendConfirmationAlready(GetResponseUserEvent $event): void ], 'sfs_user', $locale); } - public function onResendInvitationsSuccess(GetResponseUserEvent $event) + public function onResendInvitationsSuccess(GetResponseUserEvent $event): void { $user = $event->getUser(); $locale = $event->getRequest()->getLocale(); @@ -76,7 +76,7 @@ public function onResendInvitationsSuccess(GetResponseUserEvent $event) ], 'sfs_user', $locale); } - public function onResendInvitationsError(GetResponseUserEvent $event) + public function onResendInvitationsError(GetResponseUserEvent $event): void { $user = $event->getUser(); $locale = $event->getRequest()->getLocale(); From 03838a1e830dbd9c5f00b06f698a0e9a7f30f434 Mon Sep 17 00:00:00 2001 From: aritz Date: Tue, 20 Jan 2026 16:12:55 +0100 Subject: [PATCH 4/5] Mailer exceptions --- src/Controller/Admin/InvitationsController.php | 6 +++--- src/Controller/Settings/ConfirmationController.php | 4 ++-- src/EventListener/EmailInvitationListener.php | 3 ++- src/EventListener/SendResetPasswordEmailListener.php | 3 ++- src/EventListener/UserRegistrationListener.php | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Controller/Admin/InvitationsController.php b/src/Controller/Admin/InvitationsController.php index c40e97f..f25a0d0 100644 --- a/src/Controller/Admin/InvitationsController.php +++ b/src/Controller/Admin/InvitationsController.php @@ -2,7 +2,7 @@ namespace Softspring\UserBundle\Controller\Admin; -use Symfony\Component\HttpFoundation\Request; +use Exception; use Softspring\Component\Events\DispatchGetResponseTrait; use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; @@ -10,6 +10,7 @@ use Softspring\UserBundle\SfsUserEvents; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class InvitationsController extends AbstractController @@ -49,13 +50,12 @@ public function resendEmail(mixed $invitation, Request $request): Response return $response; } } - } catch (\Exception $e) { + } catch (Exception $e) { if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_INVITATIONS_RESEND_ERROR, new GetResponseUserEvent($user, $request))) { return $response; } } - return $this->redirectToRoute('sfs_user_admin_invitations_details', ['invitation' => $invitation]); } } diff --git a/src/Controller/Settings/ConfirmationController.php b/src/Controller/Settings/ConfirmationController.php index d6c71d3..e07234e 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -2,6 +2,7 @@ namespace Softspring\UserBundle\Controller\Settings; +use Exception; use Softspring\Component\Events\DispatchGetResponseTrait; use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; @@ -43,8 +44,7 @@ public function resendConfirmation(Request $request): Response return $response; } } - - } catch (\Exception $e) { + } catch (Exception $e) { if ($response = $this->dispatchGetResponse(SfsUserEvents::ADMIN_USERS_RESEND_CONFIRMATION_ERROR, new GetResponseUserEvent($user, $request))) { return $response; } diff --git a/src/EventListener/EmailInvitationListener.php b/src/EventListener/EmailInvitationListener.php index 323d304..3a14d59 100644 --- a/src/EventListener/EmailInvitationListener.php +++ b/src/EventListener/EmailInvitationListener.php @@ -2,6 +2,7 @@ namespace Softspring\UserBundle\EventListener; +use Exception; use Softspring\UserBundle\Event\UserInvitationEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; use Softspring\UserBundle\SfsUserEvents; @@ -29,7 +30,7 @@ public function onInvitation(UserInvitationEvent $event): void $invitation = $event->getInvitation(); $this->mailer->sendInvitationEmail($invitation); - } catch (\Exception $e) { + } catch (Exception $e) { } } } diff --git a/src/EventListener/SendResetPasswordEmailListener.php b/src/EventListener/SendResetPasswordEmailListener.php index 629f10b..6000a7e 100644 --- a/src/EventListener/SendResetPasswordEmailListener.php +++ b/src/EventListener/SendResetPasswordEmailListener.php @@ -3,6 +3,7 @@ namespace Softspring\UserBundle\EventListener; use DateTime; +use Exception; use Softspring\Component\Events\GetResponseFormEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; use Softspring\UserBundle\Manager\UserManagerInterface; @@ -66,7 +67,7 @@ public function sendResetEmail(GetResponseFormEvent $event): void } $this->mailer->sendResettingEmail($user); - } catch (\Exception $e) { + } catch (Exception $e) { } } } diff --git a/src/EventListener/UserRegistrationListener.php b/src/EventListener/UserRegistrationListener.php index 4c33c8c..eb54d8c 100644 --- a/src/EventListener/UserRegistrationListener.php +++ b/src/EventListener/UserRegistrationListener.php @@ -2,6 +2,7 @@ namespace Softspring\UserBundle\EventListener; +use Exception; use Softspring\UserBundle\Event\GetResponseUserEvent; use Softspring\UserBundle\Mailer\UserMailerInterface; use Softspring\UserBundle\Manager\UserManagerInterface; @@ -44,7 +45,7 @@ public function onRegisterSendConfirmationEmail(GetResponseUserEvent $event): vo $this->userManager->saveEntity($user); $this->mailer->sendRegisterConfirmationEmail($user); - } catch (\Exception $e) { + } catch (Exception $e) { } } } From 69440f0cb17cdea4bbeff22b5375ab28b7038c2f Mon Sep 17 00:00:00 2001 From: aritz Date: Tue, 20 Jan 2026 16:14:16 +0100 Subject: [PATCH 5/5] Mailer exceptions --- src/Controller/Settings/ConfirmationController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controller/Settings/ConfirmationController.php b/src/Controller/Settings/ConfirmationController.php index e07234e..fb7ec7d 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -14,7 +14,6 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; class ConfirmationController extends AbstractController {