Commit 02728278 authored by Tino Goratsch's avatar Tino Goratsch

made the bootstrapping class a little bit less dependent on other modules

parent 2fc2844c
......@@ -7,6 +7,15 @@
namespace ACP3\Core\ACL\Exception;
class AccessForbiddenException extends \Exception
use ACP3\Core\Controller\Exception\ForwardControllerActionAwareExceptionInterface;
class AccessForbiddenException extends \Exception implements ForwardControllerActionAwareExceptionInterface
{
/**
* {@inheritdoc}
*/
public function getServiceId(): string
{
return 'errors.controller.frontend.index.access_forbidden';
}
}
......@@ -8,6 +8,7 @@
namespace ACP3\Core\Application;
use ACP3\Core\Application\Exception\MaintenanceModeActiveException;
use ACP3\Core\Controller\Exception\ForwardControllerActionAwareExceptionInterface;
use ACP3\Core\DependencyInjection\ServiceContainerBuilder;
use ACP3\Core\Environment\ApplicationMode;
use Patchwork\Utf8;
......@@ -87,6 +88,7 @@ class Bootstrap extends AbstractBootstrap
*/
public function outputPage()
{
/** @var \ACP3\Core\Application\ControllerActionDispatcher $controllerActionDispatcher */
$controllerActionDispatcher = $this->container->get('core.application.controller_action_dispatcher');
try {
......@@ -98,19 +100,8 @@ class Bootstrap extends AbstractBootstrap
$controllerActionDispatcher = $this->container->get('core.application.controller_action_dispatcher');
$response = $controllerActionDispatcher->dispatch();
} catch (\ACP3\Core\Controller\Exception\ResultNotExistsException $e) {
$response = $controllerActionDispatcher->dispatch('errors.controller.frontend.index.not_found');
} catch (\ACP3\Core\Authentication\Exception\UnauthorizedAccessException $e) {
/** @var \ACP3\Core\Http\RedirectResponse $redirect */
$redirect = $this->container->get('core.http.redirect_response');
/** @var \ACP3\Core\Http\Request $request */
$request = $this->container->get('core.http.request');
$redirectUri = \base64_encode($request->getPathInfo());
$response = $redirect->temporary('users/index/login/redirect_' . $redirectUri);
} catch (\ACP3\Core\ACL\Exception\AccessForbiddenException $e) {
$response = $controllerActionDispatcher->dispatch('errors.controller.frontend.index.access_forbidden');
} catch (\ACP3\Core\Controller\Exception\ControllerActionNotFoundException $e) {
$response = $controllerActionDispatcher->dispatch('errors.controller.frontend.index.not_found');
} catch (ForwardControllerActionAwareExceptionInterface $e) {
$response = $controllerActionDispatcher->dispatch($e->getServiceId(), $e->routeArguments());
} catch (MaintenanceModeActiveException $e) {
$response = new Response($e->getMessage(), $e->getCode());
} catch (\Exception $e) {
......
......@@ -56,8 +56,8 @@ class ControllerActionDispatcher
}
/**
* @param string $serviceId
* @param array $arguments
* @param string|null $serviceId
* @param array $arguments
*
* @return Response|string
*
......@@ -65,12 +65,12 @@ class ControllerActionDispatcher
* @throws \ACP3\Core\Controller\Exception\ResultNotExistsException
* @throws \ReflectionException
*/
public function dispatch(string $serviceId = '', array $arguments = [])
public function dispatch(?string $serviceId = null, array $arguments = [])
{
if (empty($serviceId)) {
$serviceId = $this->buildControllerServiceId();
} else {
$this->modifyRequest($serviceId);
$this->modifyRequest($serviceId, $arguments);
}
if ($this->container->has($serviceId)) {
......@@ -109,11 +109,16 @@ class ControllerActionDispatcher
. '.' . $this->request->getAction();
}
protected function modifyRequest(string $serviceId)
protected function modifyRequest(string $serviceId, array $arguments)
{
list($module, , , $controller, $action) = explode('.', $serviceId);
$this->request->setPathInfo($module . '/' . $controller . '/' . $action);
$params = '';
foreach ($arguments as $key => $value) {
$params .= "/{$key}_{$value}";
}
$this->request->setPathInfo($module . '/' . $controller . '/' . $action . $params);
$this->request->processQuery();
}
......
......@@ -7,6 +7,41 @@
namespace ACP3\Core\Authentication\Exception;
class UnauthorizedAccessException extends \Exception
use ACP3\Core\Controller\Exception\ForwardControllerActionAwareExceptionInterface;
class UnauthorizedAccessException extends \Exception implements ForwardControllerActionAwareExceptionInterface
{
/**
* @var array
*/
private $routeArguments;
public function __construct(
array $routeArguments = [],
string $message = '',
int $code = 0,
\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->routeArguments = $routeArguments;
}
/**
* Returns the serviceId of the controller action to forward to.
*
* @return string
*/
public function getServiceId(): string
{
return 'users.controller.frontend.index.login';
}
/**
* @return array
*/
public function routeArguments(): array
{
return $this->routeArguments;
}
}
......@@ -7,6 +7,21 @@
namespace ACP3\Core\Controller\Exception;
class ControllerActionNotFoundException extends \Exception
class ControllerActionNotFoundException extends \Exception implements ForwardControllerActionAwareExceptionInterface
{
/**
* {@inheritdoc}
*/
public function getServiceId(): string
{
return 'errors.controller.frontend.index.not_found';
}
/**
* {@inheritdoc}
*/
public function routeArguments(): array
{
return [];
}
}
<?php
/**
* Copyright (c) by the ACP3 Developers.
* See the LICENSE file at the top-level module directory for licensing details.
*/
namespace ACP3\Core\Controller\Exception;
interface ForwardControllerActionAwareExceptionInterface extends \Throwable
{
/**
* Returns the serviceId of the controller action to forward to.
*
* @return string
*/
public function getServiceId(): string;
/**
* @return array
*/
public function routeArguments(): array;
}
......@@ -7,6 +7,21 @@
namespace ACP3\Core\Controller\Exception;
class ResultNotExistsException extends \Exception
class ResultNotExistsException extends \Exception implements ForwardControllerActionAwareExceptionInterface
{
/**
* {@inheritdoc}
*/
public function getServiceId(): string
{
return 'errors.controller.frontend.index.not_found';
}
/**
* {@inheritdoc}
*/
public function routeArguments(): array
{
return [];
}
}
......@@ -17,7 +17,7 @@ abstract class AbstractAction extends AbstractFrontendAction
parent::preDispatch();
if ($this->user->isAuthenticated() === false) {
throw new UnauthorizedAccessException();
throw new UnauthorizedAccessException(['redirect' => \base64_encode($this->request->getPathInfo())]);
}
}
}
......@@ -11,6 +11,7 @@ use ACP3\Core\Application\Event\ControllerActionBeforeDispatchEvent;
use ACP3\Core\Authentication\Exception\UnauthorizedAccessException;
use ACP3\Core\Controller\AreaEnum;
use ACP3\Core\Helpers\RedirectMessages;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Modules\ACP3\Users\Model\UserModel;
......@@ -28,15 +29,21 @@ class IsUserAuthenticatedListener
* @var Translator
*/
private $translator;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
private $request;
/**
* IsUserAuthenticatedOnControllerActionBeforeDispatchListener constructor.
*
* @param RedirectMessages $redirectMessages
* @param Translator $translator
* @param UserModel $user
* @param \ACP3\Core\Http\RequestInterface $request
* @param RedirectMessages $redirectMessages
* @param Translator $translator
* @param UserModel $user
*/
public function __construct(
RequestInterface $request,
RedirectMessages $redirectMessages,
Translator $translator,
UserModel $user
......@@ -44,6 +51,7 @@ class IsUserAuthenticatedListener
$this->user = $user;
$this->redirectMessages = $redirectMessages;
$this->translator = $translator;
$this->request = $request;
}
/**
......@@ -59,7 +67,7 @@ class IsUserAuthenticatedListener
$this->translator->t('users', 'authentication_required')
);
throw new UnauthorizedAccessException();
throw new UnauthorizedAccessException(['redirect' => \base64_encode($this->request->getPathInfo())]);
}
}
}
......@@ -9,6 +9,7 @@ services:
users.event.is_user_authenticated_listener:
class: ACP3\Modules\ACP3\Users\Event\Listener\IsUserAuthenticatedListener
arguments:
- '@core.http.request'
- '@core.helpers.redirect'
- '@core.i18n.translator'
- '@users.model.user_model'
......
......@@ -65,19 +65,17 @@ class Bootstrap extends Core\Application\AbstractBootstrap
*/
public function outputPage()
{
/** @var \ACP3\Core\Http\RedirectResponse $redirect */
$redirect = $this->container->get('core.http.redirect_response');
/** @var \ACP3\Core\Application\ControllerActionDispatcher $controllerActionDispatcher */
$controllerActionDispatcher = $this->container->get('core.application.controller_action_dispatcher');
try {
/** @var \ACP3\Core\Application\ControllerActionDispatcher $controllerActionDispatcher */
$controllerActionDispatcher = $this->container->get('core.application.controller_action_dispatcher');
$response = $controllerActionDispatcher->dispatch();
} catch (Core\Controller\Exception\ControllerActionNotFoundException $e) {
$response = $redirect->temporary('errors/index/not_found');
$response = $controllerActionDispatcher->dispatch('errors.controller.install.index.not_found');
} catch (\Exception $e) {
$this->logger->critical($e);
$response = $redirect->temporary('errors/index/server_error');
$response = $controllerActionDispatcher->dispatch('errors.controller.install.index.server_error');
}
return $response;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment