diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-07-29 10:42:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-29 10:42:36 +0200 |
commit | ec7e837d6a1753cd6bbed69badd07d22bad0dffc (patch) | |
tree | 71e68de384ee6b0efdde1ef1eb306d90f8212621 /core/Controller | |
parent | 173d95c9041be53d5a0ec980df18223ddc58737b (diff) | |
parent | b6dd2ebd3909017b9a5dbe0e145d6c5041a9043c (diff) | |
download | nextcloud-server-ec7e837d6a1753cd6bbed69badd07d22bad0dffc.tar.gz nextcloud-server-ec7e837d6a1753cd6bbed69badd07d22bad0dffc.zip |
Merge pull request #16563 from nextcloud/enh/lostcontroller/better_exceptions
Use proper exception in lostController
Diffstat (limited to 'core/Controller')
-rw-r--r-- | core/Controller/LostController.php | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index 5ac0557e5d6..7947440785b 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -32,6 +32,7 @@ namespace OC\Core\Controller; use OC\Authentication\TwoFactorAuth\Manager; +use OC\Core\Exception\ResetPasswordException; use OC\HintException; use \OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; @@ -248,11 +249,11 @@ class LostController extends Controller { // FIXME: use HTTP error codes try { $this->sendEmail($user); - } catch (\Exception $e) { + } catch (ResetPasswordException $e) { // Ignore the error since we do not want to leak this info - $this->logger->logException($e, [ - 'level' => ILogger::WARN - ]); + $this->logger->warning('Could not send password reset email: ' . $e->getMessage()); + } catch (\Exception $e) { + $this->logger->logException($e); } $response = new JSONResponse($this->success()); @@ -312,16 +313,15 @@ class LostController extends Controller { /** * @param string $input - * @throws \Exception + * @throws ResetPasswordException + * @throws \OCP\PreConditionNotMetException */ protected function sendEmail($input) { $user = $this->findUserByIdOrMail($input); $email = $user->getEMailAddress(); if (empty($email)) { - throw new \Exception( - $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') - ); + throw new ResetPasswordException('Could not send reset e-mail since there is no email for username ' . $input); } // Generate the token. It is stored encrypted in the database with the @@ -367,26 +367,21 @@ class LostController extends Controller { $message->useTemplate($emailTemplate); $this->mailer->send($message); } catch (\Exception $e) { - throw new \Exception($this->l10n->t( - 'Couldn\'t send reset email. Please contact your administrator.' - )); + // Log the exception and continue + $this->logger->logException($e); } } /** * @param string $input * @return IUser - * @throws \InvalidArgumentException + * @throws ResetPasswordException */ protected function findUserByIdOrMail($input) { - $userNotFound = new \InvalidArgumentException( - $this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.') - ); - $user = $this->userManager->get($input); if ($user instanceof IUser) { if (!$user->isEnabled()) { - throw $userNotFound; + throw new ResetPasswordException('User is disabled'); } return $user; @@ -400,6 +395,6 @@ class LostController extends Controller { return reset($users); } - throw $userNotFound; + throw new ResetPasswordException('Could not find user'); } } |