Skip to content

Commit

Permalink
Make LostController use IInitialState and LoggerInterface
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jun 10, 2022
1 parent 6283d14 commit abe5ff3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
30 changes: 15 additions & 15 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@
*/
namespace OC\Core\Controller;

use Exception;
use OC\Authentication\TwoFactorAuth\Manager;
use OC\Core\Events\BeforePasswordResetEvent;
use OC\Core\Events\PasswordResetEvent;
use OC\Core\Exception\ResetPasswordException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Defaults;
use OCP\Encryption\IEncryptionModule;
use OCP\Encryption\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\HintException;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -80,9 +81,8 @@ class LostController extends Controller {
protected IMailer $mailer;
private LoggerInterface $logger;
private Manager $twoFactorManager;
private IInitialStateService $initialStateService;
private IInitialState $initialState;
private IVerificationToken $verificationToken;

private IEventDispatcher $eventDispatcher;

public function __construct(
Expand All @@ -93,12 +93,12 @@ public function __construct(
Defaults $defaults,
IL10N $l10n,
IConfig $config,
$defaultMailAddress,
string $defaultMailAddress,
IManager $encryptionManager,
IMailer $mailer,
LoggerInterface $logger,
Manager $twoFactorManager,
IInitialStateService $initialStateService,
IInitialState $initialState,
IVerificationToken $verificationToken,
IEventDispatcher $eventDispatcher
) {
Expand All @@ -113,7 +113,7 @@ public function __construct(
$this->mailer = $mailer;
$this->logger = $logger;
$this->twoFactorManager = $twoFactorManager;
$this->initialStateService = $initialStateService;
$this->initialState = $initialState;
$this->verificationToken = $verificationToken;
$this->eventDispatcher = $eventDispatcher;
}
Expand All @@ -127,7 +127,7 @@ public function __construct(
public function resetform(string $token, string $userId): TemplateResponse {
try {
$this->checkPasswordResetToken($token, $userId);
} catch (\Exception $e) {
} catch (Exception $e) {
if ($this->config->getSystemValue('lost_password_link', '') !== 'disabled'
|| ($e instanceof InvalidTokenException
&& !in_array($e->getCode(), [InvalidTokenException::TOKEN_NOT_FOUND, InvalidTokenException::USER_UNKNOWN]))
Expand All @@ -145,8 +145,8 @@ public function resetform(string $token, string $userId): TemplateResponse {
TemplateResponse::RENDER_AS_GUEST
);
}
$this->initialStateService->provideInitialState('core', 'resetPasswordUser', $userId);
$this->initialStateService->provideInitialState('core', 'resetPasswordTarget',
$this->initialState->provideInitialState('resetPasswordUser', $userId);
$this->initialState->provideInitialState('resetPasswordTarget',
$this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', ['userId' => $userId, 'token' => $token])
);

Expand All @@ -159,7 +159,7 @@ public function resetform(string $token, string $userId): TemplateResponse {
}

/**
* @throws \Exception
* @throws Exception
*/
protected function checkPasswordResetToken(string $token, string $userId): void {
try {
Expand All @@ -169,7 +169,7 @@ protected function checkPasswordResetToken(string $token, string $userId): void
$error = $e->getCode() === InvalidTokenException::TOKEN_EXPIRED
? $this->l10n->t('Could not reset password because the token is expired')
: $this->l10n->t('Could not reset password because the token is invalid');
throw new \Exception($error, (int)$e->getCode(), $e);
throw new Exception($error, (int)$e->getCode(), $e);
}
}

Expand Down Expand Up @@ -203,7 +203,7 @@ public function email(string $user): JSONResponse {
} catch (ResetPasswordException $e) {
// Ignore the error since we do not want to leak this info
$this->logger->warning('Could not send password reset email: ' . $e->getMessage());
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public function setPassword(string $token, string $userId, string $password, boo
\OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', ['uid' => $userId, 'password' => $password]);

if (!$user->setPassword($password)) {
throw new \Exception();
throw new Exception();
}

$this->eventDispatcher->dispatchTyped(new PasswordResetEvent($user, $password));
Expand All @@ -248,7 +248,7 @@ public function setPassword(string $token, string $userId, string $password, boo
@\OC::$server->getUserSession()->unsetMagicInCookie();
} catch (HintException $e) {
return $this->error($e->getHint());
} catch (\Exception $e) {
} catch (Exception $e) {
return $this->error($e->getMessage());
}

Expand Down Expand Up @@ -301,7 +301,7 @@ protected function sendEmail(string $input): void {
$message->setFrom([$this->from => $this->defaults->getName()]);
$message->useTemplate($emailTemplate);
$this->mailer->send($message);
} catch (\Exception $e) {
} catch (Exception $e) {
// Log the exception and continue
$this->logger->error($e->getMessage(), ['app' => 'core', 'exception' => $e]);
}
Expand Down
24 changes: 18 additions & 6 deletions tests/Core/Controller/LostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
use OC\Mail\Message;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Defaults;
use OCP\Encryption\IEncryptionModule;
use OCP\Encryption\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -72,12 +72,12 @@ class LostControllerTest extends TestCase {
private $encryptionManager;
/** @var IRequest|MockObject */
private $request;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
/** @var LoggerInterface|MockObject */
private $logger;
/** @var Manager|MockObject */
private $twofactorManager;
/** @var IInitialStateService|MockObject */
private $initialStateService;
/** @var IInitialState|MockObject */
private $initialState;
/** @var IVerificationToken|MockObject */
private $verificationToken;
/** @var IEventDispatcher|MockObject */
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function setUp(): void {
->willReturn(true);
$this->logger = $this->createMock(LoggerInterface::class);
$this->twofactorManager = $this->createMock(Manager::class);
$this->initialStateService = $this->createMock(IInitialStateService::class);
$this->initialState = $this->createMock(IInitialState::class);
$this->verificationToken = $this->createMock(IVerificationToken::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->lostController = new LostController(
Expand All @@ -142,7 +142,7 @@ protected function setUp(): void {
$this->mailer,
$this->logger,
$this->twofactorManager,
$this->initialStateService,
$this->initialState,
$this->verificationToken,
$this->eventDispatcher
);
Expand Down Expand Up @@ -176,6 +176,18 @@ public function testResetFormValidToken() {
$this->verificationToken->expects($this->once())
->method('check')
->with('MySecretToken', $this->existingUser, 'lostpassword', 'test@example.com');
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with('core.lost.setPassword', ['userId' => 'ValidTokenUser', 'token' => 'MySecretToken'])
->willReturn('https://example.tld/index.php/lostpassword/set/sometoken/someuser');
$this->initialState
->expects($this->exactly(2))
->method('provideInitialState')
->withConsecutive(
['resetPasswordUser', 'ValidTokenUser'],
['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser']
);

$response = $this->lostController->resetform('MySecretToken', 'ValidTokenUser');
$expectedResponse = new TemplateResponse('core',
Expand Down

0 comments on commit abe5ff3

Please sign in to comment.