From c4761ddab889be542efe228ba2136f70b047de6f Mon Sep 17 00:00:00 2001 From: Guite Date: Tue, 6 Dec 2016 18:51:55 +0100 Subject: [PATCH] search also for user entities during lost user name and lost password processes, fixed #3274 --- .../Controller/AccountController.php | 46 +++++++++++++++---- .../Helper/LostPasswordVerificationHelper.php | 18 +++++--- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/system/ZAuthModule/Controller/AccountController.php b/src/system/ZAuthModule/Controller/AccountController.php index d7347e692a..dd9f4ebdc4 100644 --- a/src/system/ZAuthModule/Controller/AccountController.php +++ b/src/system/ZAuthModule/Controller/AccountController.php @@ -48,15 +48,28 @@ public function lostUserNameAction(Request $request) $form->handleRequest($request); if ($form->isSubmitted()) { $data = $form->getData(); - $mapping = $this->get('zikula_zauth_module.authentication_mapping_repository')->findBy(['email' => $data['email']]); + + $email = $data['email']; + $userName = ''; + + $mapping = $this->get('zikula_zauth_module.authentication_mapping_repository')->findBy(['email' => $email]); if (count($mapping) == 1) { + $userName = $mapping[0]->getUname(); + } elseif (count($mapping) < 1) { + $user = $this->get('zikula_users_module.user_repository')->findBy(['email' => $email]); + if (count($user) == 1) { + $userName = $user[0]->getUname(); + } + } + + if ($userName != '') { // send email - $sent = $this->get('zikula_zauth_module.helper.mail_helper')->sendNotification($mapping[0]->getEmail(), 'lostuname', [ - 'uname' => $mapping[0]->getUname(), + $sent = $this->get('zikula_zauth_module.helper.mail_helper')->sendNotification($email, 'lostuname', [ + 'uname' => $userName, 'requestedByAdmin' => false, ]); if ($sent) { - $this->addFlash('status', $this->__f('Done! The account information for %s has been sent via e-mail.', ['%s' => $data['email']])); + $this->addFlash('status', $this->__f('Done! The account information for %s has been sent via e-mail.', ['%s' => $email])); } else { $this->addFlash('error', $this->__('Unable to send email to the requested address. Please contact the system administrator for assistance.')); } @@ -90,20 +103,33 @@ public function lostPasswordAction(Request $request) $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $redirectToRoute = ''; - $map = ['uname' => $this->__('username'), 'email' => $this->__('email address')]; + $map = [ + 'uname' => $this->__('username'), + 'email' => $this->__('email address') + ]; $data = $form->getData(); $field = empty($data['uname']) ? 'email' : 'uname'; $inverse = $field == 'uname' ? 'email' : 'uname'; + + $user = null; + $mapping = $this->get('zikula_zauth_module.authentication_mapping_repository')->findBy([$field => $data[$field]]); if (count($mapping) == 1) { - $mapping = $mapping[0]; - $user = $this->get('zikula_users_module.user_repository')->find($mapping->getUid()); + $user = $this->get('zikula_users_module.user_repository')->find($mapping[0]->getUid()); + } elseif (count($mapping) < 1) { + $users = $this->get('zikula_users_module.user_repository')->findBy([$field => $data[$field]]); + if (count($users) == 1) { + $user = $users[0]; + } + } + + if (null !== $user) { switch ($user->getActivated()) { case UsersConstant::ACTIVATED_ACTIVE: $changePasswordExpireDays = $this->getVar(ZAuthConstant::MODVAR_EXPIRE_DAYS_CHANGE_PASSWORD, ZAuthConstant::DEFAULT_EXPIRE_DAYS_CHANGE_PASSWORD); - $lostPasswordId = $this->get('zikula_zauth_module.helper.lost_password_verification_helper')->createLostPasswordId($mapping); - $sent = $this->get('zikula_zauth_module.helper.mail_helper')->sendNotification($mapping->getEmail(), 'lostpassword', [ - 'uname' => $mapping->getUname(), + $lostPasswordId = $this->get('zikula_zauth_module.helper.lost_password_verification_helper')->createLostPasswordId($user); + $sent = $this->get('zikula_zauth_module.helper.mail_helper')->sendNotification($user->getEmail(), 'lostpassword', [ + 'uname' => $user->getUname(), 'validDays' => $changePasswordExpireDays, 'lostPasswordId' => $lostPasswordId, 'requestedByAdmin' => false, diff --git a/src/system/ZAuthModule/Helper/LostPasswordVerificationHelper.php b/src/system/ZAuthModule/Helper/LostPasswordVerificationHelper.php index f43f8ba1c8..67598c9827 100644 --- a/src/system/ZAuthModule/Helper/LostPasswordVerificationHelper.php +++ b/src/system/ZAuthModule/Helper/LostPasswordVerificationHelper.php @@ -11,7 +11,9 @@ namespace Zikula\ZAuthModule\Helper; +use Zikula\Core\Doctrine\EntityAccess; use Zikula\ExtensionsModule\Api\VariableApi; +use Zikula\UsersModule\Entity\UserEntity; use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity; use Zikula\ZAuthModule\Entity\RepositoryInterface\UserVerificationRepositoryInterface; use Zikula\ZAuthModule\Entity\UserVerificationEntity; @@ -55,17 +57,21 @@ public function __construct(UserVerificationRepositoryInterface $userVerificatio * Creates an identifier for the lost password link. * This link carries the user's id, name and email address as well as the actual confirmation code. * - * @param AuthenticationMappingEntity $mapping + * @param EntityAccess $record instance of UserEntity or AuthenticationMappingEntity * @return string The created identifier */ - public function createLostPasswordId(AuthenticationMappingEntity $mapping) + public function createLostPasswordId(EntityAccess $record) { - $confirmationCode = $this->userVerificationRepository->setVerificationCode($mapping->getUid()); + if (!($record instanceof UserEntity) && !($record instanceof AuthenticationMappingEntity)) { + throw new Exception('Record must be an instance of UserEntity or AuthenticationMappingEntity.'); + } + + $confirmationCode = $this->userVerificationRepository->setVerificationCode($record->getUid()); $params = [ - $mapping->getUid(), - $mapping->getUname(), - $mapping->getEmail(), + $record->getUid(), + $record->getUname(), + $record->getEmail(), $confirmationCode ];