diff --git a/src/Controller/Admin/InvitationsController.php b/src/Controller/Admin/InvitationsController.php index 305ba85..f25a0d0 100644 --- a/src/Controller/Admin/InvitationsController.php +++ b/src/Controller/Admin/InvitationsController.php @@ -2,11 +2,15 @@ namespace Softspring\UserBundle\Controller\Admin; +use Exception; 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\Request; use Symfony\Component\HttpFoundation\Response; class InvitationsController extends AbstractController @@ -34,12 +38,22 @@ 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..fb7ec7d 100644 --- a/src/Controller/Settings/ConfirmationController.php +++ b/src/Controller/Settings/ConfirmationController.php @@ -2,11 +2,14 @@ namespace Softspring\UserBundle\Controller\Settings; +use Exception; 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; @@ -33,9 +36,17 @@ public function resendConfirmation(Request $request): Response { /** @var UserInterface $user */ $user = $this->getUser(); - - if ($user instanceof ConfirmableInterface && !$user->isConfirmed()) { - $this->userMailer->sendRegisterConfirmationEmail($user); + try { + 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..e9968b8 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): void + { + $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): void + { + $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..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; @@ -25,8 +26,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..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; @@ -57,13 +58,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..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; @@ -34,14 +35,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: