Skip to content

Commit

Permalink
More use-case specific responder [SLE-192]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Nov 7, 2023
1 parent 03d3965 commit e47c3ea
Show file tree
Hide file tree
Showing 43 changed files with 412 additions and 415 deletions.
3 changes: 3 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Turn on the rewrite engine
RewriteEngine on
# If the URL path is empty, rewrite to the 'public/' directory
RewriteRule ^$ public/ [L]
# For any requested URL path, rewrite to the 'public/' directory followed by the requested path
RewriteRule (.*) public/$1 [L]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Application\Action\Authentication\Ajax;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Domain\Authentication\Exception\InvalidTokenException;
use App\Domain\Authentication\Exception\UserAlreadyVerifiedException;
use App\Domain\Authentication\Service\AccountUnlockTokenVerifier;
Expand All @@ -17,7 +17,7 @@ final class AccountUnlockProcessAction
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly Responder $responder,
private readonly RedirectHandler $redirectHandler,
private readonly SessionManagerInterface $sessionManager,
private readonly SessionInterface $session,
private readonly AccountUnlockTokenVerifier $accountUnlockTokenVerifier
Expand Down Expand Up @@ -52,10 +52,10 @@ public function __invoke(ServerRequest $request, Response $response): Response
)
);

return $this->responder->redirectToUrl($response, $queryParams['redirect']);
return $this->redirectHandler->redirectToUrl($response, $queryParams['redirect']);
}

return $this->responder->redirectToRouteName($response, 'home-page');
return $this->redirectHandler->redirectToRouteName($response, 'home-page');
} catch (InvalidTokenException $ite) {
$flash->add(
'error',
Expand All @@ -65,15 +65,15 @@ public function __invoke(ServerRequest $request, Response $response): Response
$newQueryParam = isset($queryParams['redirect']) ? ['redirect' => $queryParams['redirect']] : [];

// Redirect to login page with redirect query param if set
return $this->responder->redirectToRouteName($response, 'login-page', [], $newQueryParam);
return $this->redirectHandler->redirectToRouteName($response, 'login-page', [], $newQueryParam);
} catch (UserAlreadyVerifiedException $uave) {
$flash->add('info', $uave->getMessage());
$this->logger->info(
'Not locked user tried to unlock account. user_verification id: ' . $queryParams['id']
);
$newQueryParam = isset($queryParams['redirect']) ? ['redirect' => $queryParams['redirect']] : [];

return $this->responder->redirectToRouteName(
return $this->redirectHandler->redirectToRouteName(
$response,
'login-page',
[],
Expand Down
26 changes: 14 additions & 12 deletions src/Application/Action/Authentication/Ajax/LoginSubmitAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Application\Action\Authentication\Ajax;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Application\Responder\TemplateRenderer;
use App\Domain\Authentication\Exception\InvalidCredentialsException;
use App\Domain\Authentication\Exception\UnableToLoginStatusNotActiveException;
use App\Domain\Authentication\Service\LoginVerifier;
Expand All @@ -18,7 +19,8 @@
final class LoginSubmitAction
{
public function __construct(
private readonly Responder $responder,
private readonly RedirectHandler $redirectHandler,
private readonly TemplateRenderer $templateRenderer,
private readonly LoggerInterface $logger,
private readonly LoginVerifier $loginVerifier,
private readonly SessionManagerInterface $sessionManager,
Expand Down Expand Up @@ -57,17 +59,17 @@ public function __invoke(ServerRequest $request, Response $response): Response

// After register and login success, check if user should be redirected
if (isset($queryParams['redirect'])) {
return $this->responder->redirectToUrl(
return $this->redirectHandler->redirectToUrl(
$response,
$request->getQueryParams()['redirect'],
$themeQueryParams
);
}

return $this->responder->redirectToRouteName($response, 'home-page', [], $themeQueryParams);
return $this->redirectHandler->redirectToRouteName($response, 'home-page', [], $themeQueryParams);
} // When the response is not JSON but rendered, the validation exception has to be caught in action
catch (ValidationException $ve) {
return $this->responder->renderOnValidationError(
return $this->templateRenderer->renderOnValidationError(
$response,
'authentication/login.html.php',
$ve,
Expand All @@ -79,10 +81,10 @@ public function __invoke(ServerRequest $request, Response $response): Response
'InvalidCredentialsException thrown with message: "' . $e->getMessage() . '" user "' .
$e->getUserEmail() . '"'
);
$this->responder->addPhpViewAttribute('formError', true);
$this->responder->addPhpViewAttribute('formErrorMessage', __('Invalid credentials. Please try again.'));
$this->templateRenderer->addPhpViewAttribute('formError', true);
$this->templateRenderer->addPhpViewAttribute('formErrorMessage', __('Invalid credentials. Please try again.'));

return $this->responder->render(
return $this->templateRenderer->render(
$response->withStatus(401),
'authentication/login.html.php',
// Provide same query params passed to login page to be added to the login submit request
Expand All @@ -94,7 +96,7 @@ public function __invoke(ServerRequest $request, Response $response): Response
throw $securityException;
}

return $this->responder->respondWithFormThrottle(
return $this->templateRenderer->respondWithFormThrottle(
$response,
'authentication/login.html.php',
$securityException,
Expand All @@ -103,11 +105,11 @@ public function __invoke(ServerRequest $request, Response $response): Response
);
} catch (UnableToLoginStatusNotActiveException $unableToLoginException) {
// When user doesn't have status active
$this->responder->addPhpViewAttribute('formError', true);
$this->templateRenderer->addPhpViewAttribute('formError', true);
// Add form error message
$this->responder->addPhpViewAttribute('formErrorMessage', $unableToLoginException->getMessage());
$this->templateRenderer->addPhpViewAttribute('formErrorMessage', $unableToLoginException->getMessage());

return $this->responder->render(
return $this->templateRenderer->render(
$response->withStatus(401),
'authentication/login.html.php',
// Provide same query params passed to login page to be added to the login submit request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Application\Action\Authentication\Ajax;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Application\Responder\TemplateRenderer;
use App\Domain\Authentication\Exception\InvalidTokenException;
use App\Domain\Authentication\Service\PasswordResetterWithToken;
use App\Domain\Validation\ValidationException;
Expand All @@ -14,7 +15,8 @@
class NewPasswordResetSubmitAction
{
public function __construct(
private readonly Responder $responder,
private readonly TemplateRenderer $templateRenderer,
private readonly RedirectHandler $redirectHandler,
private readonly SessionInterface $session,
private readonly PasswordResetterWithToken $passwordResetterWithToken,
private readonly LoggerInterface $logger,
Expand Down Expand Up @@ -44,9 +46,9 @@ public function __invoke(ServerRequest $request, Response $response): Response
sprintf(__('Successfully changed password. <b>%s</b>'), __('Please log in.'))
);

return $this->responder->redirectToRouteName($response, 'login-page');
return $this->redirectHandler->redirectToRouteName($response, 'login-page');
} catch (InvalidTokenException $ite) {
$this->responder->addPhpViewAttribute(
$this->templateRenderer->addPhpViewAttribute(
'formErrorMessage',
__(
'<b>Invalid, used or expired link. <br> Please request a new link below and make
Expand All @@ -55,7 +57,7 @@ public function __invoke(ServerRequest $request, Response $response): Response
);
// Pre-fill email input field for more user comfort.
if ($ite->userData->email !== null) {
$this->responder->addPhpViewAttribute('preloadValues', ['email' => $ite->userData->email]);
$this->templateRenderer->addPhpViewAttribute('preloadValues', ['email' => $ite->userData->email]);
}

$this->logger->error(
Expand All @@ -64,15 +66,15 @@ public function __invoke(ServerRequest $request, Response $response): Response

// The login page is rendered but the url is reset-password. In login-main.js the url is replaced and
// the password forgotten form is shown instead of the login form.
return $this->responder->render($response, 'authentication/login.html.php');
return $this->templateRenderer->render($response, 'authentication/login.html.php');
} // Validation Exception has to be caught here and not middleware as we need to add token and id to php view
catch (ValidationException $validationException) {
$flash->add('error', $validationException->getMessage());
// Add token and id to php view attribute like PasswordResetAction does
$this->responder->addPhpViewAttribute('token', $parsedBody['token']);
$this->responder->addPhpViewAttribute('id', $parsedBody['id']);
$this->templateRenderer->addPhpViewAttribute('token', $parsedBody['token']);
$this->templateRenderer->addPhpViewAttribute('id', $parsedBody['id']);

return $this->responder->renderOnValidationError(
return $this->templateRenderer->renderOnValidationError(
$response,
'authentication/reset-password.html.php',
$validationException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Application\Action\Authentication\Ajax;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Application\Responder\TemplateRenderer;
use App\Domain\Authentication\Service\PasswordRecoveryEmailSender;
use App\Domain\Exception\DomainRecordNotFoundException;
use App\Domain\Security\Exception\SecurityException;
Expand All @@ -16,7 +17,8 @@
final class PasswordForgottenEmailSubmitAction
{
public function __construct(
private readonly Responder $responder,
private readonly TemplateRenderer $templateRenderer,
private readonly RedirectHandler $redirectHandler,
private readonly SessionInterface $session,
private readonly PasswordRecoveryEmailSender $passwordRecoveryEmailSender,
private readonly LoggerInterface $logger,
Expand Down Expand Up @@ -46,14 +48,14 @@ public function __invoke(ServerRequest $request, Response $response): Response
);
} catch (ValidationException $validationException) {
// Form error messages set in function below
return $this->responder->renderOnValidationError(
return $this->templateRenderer->renderOnValidationError(
$response,
'authentication/login.html.php',
$validationException,
$request->getQueryParams(),
);
} catch (SecurityException $securityException) {
return $this->responder->respondWithFormThrottle(
return $this->templateRenderer->respondWithFormThrottle(
$response,
'authentication/login.html.php',
$securityException,
Expand All @@ -63,7 +65,7 @@ public function __invoke(ServerRequest $request, Response $response): Response
} catch (TransportExceptionInterface $transportException) {
$flash->add('error', __('There was an error when sending the email.'));

return $this->responder->render(
return $this->templateRenderer->render(
$response,
'authentication/login.html.php',
$request->getQueryParams(),
Expand All @@ -77,6 +79,6 @@ public function __invoke(ServerRequest $request, Response $response): Response
)
);

return $this->responder->redirectToRouteName($response, 'login-page');
return $this->redirectHandler->redirectToRouteName($response, 'login-page');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Application\Action\Authentication\Ajax;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Domain\Authentication\Exception\InvalidTokenException;
use App\Domain\Authentication\Exception\UserAlreadyVerifiedException;
use App\Domain\Authentication\Service\RegisterTokenVerifier;
Expand All @@ -12,12 +12,14 @@
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpBadRequestException;
use Slim\Interfaces\RouteParserInterface;

final class RegisterVerifyProcessAction
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly Responder $responder,
private readonly RedirectHandler $redirectHandler,
private readonly RouteParserInterface $routeParser,
private readonly SessionManagerInterface $sessionManager,
private readonly SessionInterface $session,
private readonly RegisterTokenVerifier $registerTokenVerifier
Expand Down Expand Up @@ -51,41 +53,41 @@ public function __invoke(ServerRequest $request, Response $response): Response
$this->session->set('user_id', $userId);

if (isset($queryParams['redirect'])) {
return $this->responder->redirectToUrl($response, $queryParams['redirect']);
return $this->redirectHandler->redirectToUrl($response, $queryParams['redirect']);
}

return $this->responder->redirectToRouteName($response, 'home-page');
return $this->redirectHandler->redirectToRouteName($response, 'home-page');
} catch (InvalidTokenException $ite) {
$flash->add('error', __('Invalid or expired link. Please log in to receive a new link.'));
$this->logger->error('Invalid or expired token user_verification id: ' . $queryParams['id']);
$newQueryParam = isset($queryParams['redirect']) ? ['redirect' => $queryParams['redirect']] : [];

// Redirect to login page with redirect query param if set
return $this->responder->redirectToRouteName($response, 'login-page', [], $newQueryParam);
return $this->redirectHandler->redirectToRouteName($response, 'login-page', [], $newQueryParam);
} catch (UserAlreadyVerifiedException $uave) {
// Check if already logged in
if ($this->session->get('user_id') === null) {
// If not logged in, redirect to login page with correct further redirect query param
$flash->add('info', __('You are already verified. Please log in.'));
$newQueryParam = isset($queryParams['redirect']) ? ['redirect' => $queryParams['redirect']] : [];

return $this->responder->redirectToRouteName($response, 'login-page', [], $newQueryParam);
return $this->redirectHandler->redirectToRouteName($response, 'login-page', [], $newQueryParam);
}
// Already logged in
$flash->add(
'info',
sprintf(
__('You are already logged-in.<br>Would you like to %slogout%s?'),
'<a href="' . $this->responder->urlFor('logout') . '">',
'<a href="' . $this->routeParser->urlFor('logout') . '">',
'</a>'
)
);

if (isset($queryParams['redirect'])) {
return $this->responder->redirectToUrl($response, $queryParams['redirect']);
return $this->redirectHandler->redirectToUrl($response, $queryParams['redirect']);
}

return $this->responder->redirectToRouteName($response, 'home-page');
return $this->redirectHandler->redirectToRouteName($response, 'home-page');
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/Application/Action/Authentication/Page/LoginPageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace App\Application\Action\Authentication\Page;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use App\Application\Responder\TemplateRenderer;
use Odan\Session\SessionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\RouteParserInterface;

final class LoginPageAction
{
public function __construct(
private readonly Responder $responder,
private readonly RedirectHandler $redirectHandler,
private readonly RouteParserInterface $routeParser,
private readonly TemplateRenderer $templateRenderer,
private readonly SessionInterface $session,
) {
}
Expand All @@ -34,20 +38,20 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
'info',
sprintf(
__('You are already logged-in.<br>Would you like to %slogout%s?'),
'<a href="' . $this->responder->urlFor('logout') . '">',
'<a href="' . $this->routeParser->urlFor('logout') . '">',
'</a>'
)
);
// If redirect param set, redirect to this url
if (isset($queryParams['redirect'])) {
return $this->responder->redirectToUrl($response, $queryParams['redirect']);
return $this->redirectHandler->redirectToUrl($response, $queryParams['redirect']);
}

// Otherwise, go to home page
return $this->responder->redirectToRouteName($response, 'home-page');
return $this->redirectHandler->redirectToRouteName($response, 'home-page');
}

return $this->responder->render(
return $this->templateRenderer->render(
$response,
'authentication/login.html.php',
// Provide same query params passed to login page to be added to the login submit request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Application\Action\Authentication\Page;

use App\Application\Responder\Responder;
use App\Application\Responder\RedirectHandler;
use Odan\Session\SessionInterface;
use Odan\Session\SessionManagerInterface;
use Psr\Http\Message\ResponseInterface as Response;
Expand All @@ -13,7 +13,7 @@ final class LogoutPageAction
public function __construct(
private readonly SessionManagerInterface $sessionManager,
private readonly SessionInterface $session,
private readonly Responder $responder,
private readonly RedirectHandler $redirectHandler,
) {
}

Expand All @@ -26,6 +26,6 @@ public function __invoke(ServerRequest $request, Response $response): Response
// Add flash message to inform user of the success
$this->session->getFlash()->add('success', __('Logged out successfully.'));

return $this->responder->redirectToRouteName($response, 'login-page');
return $this->redirectHandler->redirectToRouteName($response, 'login-page');
}
}
Loading

0 comments on commit e47c3ea

Please sign in to comment.