. */ namespace SP\Modules\Web\Controllers\UserPassReset; use Exception; use JsonException; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Services\UserPassRecover; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\Traits\JsonTrait; /** * Class SaveRequestController */ final class SaveRequestController extends UserPassResetSaveBase { use JsonTrait; /** * @return bool * @throws JsonException */ public function saveRequestAction(): bool { try { $this->checkTracking(); $login = $this->request->analyzeString('login'); $email = $this->request->analyzeEmail('email'); $userData = $this->userService->getByLogin($login); if ($userData->getEmail() !== $email) { throw new SPException(__u('Wrong data'), SPException::WARNING); } if ($userData->isDisabled() || $userData->isLdap()) { throw new SPException( __u('Unable to reset the password'), SPException::WARNING, __u('Please contact to the administrator') ); } $hash = $this->userPassRecoverService->requestForUserId($userData->getId()); $this->eventDispatcher->notify( 'request.user.passReset', new Event( $this, EventMessage::factory() ->addDescription(__u('Password Recovery')) ->addDetail(__u('Requested for'), sprintf('%s (%s)', $login, $email)) ) ); $this->mailService->send( __('Password Change'), $email, UserPassRecover::getMailMessage($hash) ); return $this->returnJsonResponse( JsonMessage::JSON_SUCCESS, __u('Request sent'), [__u('You will receive an email to complete the request shortly.')] ); } catch (Exception $e) { processException($e); $this->addTracking(); $this->eventDispatcher->notify('exception', new Event($e)); return $this->returnJsonResponseException($e); } } }