refactor: Fix initialization errors.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-06-13 08:02:42 +02:00
parent fbe0e37c17
commit ef73720d2b
5 changed files with 55 additions and 69 deletions

View File

@@ -29,6 +29,7 @@ use Exception;
use Klein\Klein;
use SP\Core\Application;
use SP\Core\Bootstrap\BootstrapBase;
use SP\Core\Bootstrap\BootstrapWeb;
use SP\Core\Context\ContextBase;
use SP\Core\Context\SessionContext;
use SP\Core\Crypt\CryptSessionHandler;
@@ -57,6 +58,7 @@ use SP\Domain\User\Services\UserProfileService;
use SP\Domain\User\UserProfileServiceInterface;
use SP\Http\Address;
use SP\Http\RequestInterface;
use SP\Http\Uri;
use SP\Infrastructure\Database\DatabaseUtil;
use SP\Infrastructure\File\FileException;
use SP\Plugin\PluginManager;
@@ -83,7 +85,14 @@ final class Init extends HttpModuleBase
/**
* List of controllers that don't need to update the user's session activity
*/
private const NO_SESSION_ACTIVITY = ['items', 'login'];
private const NO_SESSION_ACTIVITY = ['items', 'login'];
public const ROUTE_INSTALL = 'install/index';
public const ROUTE_ERROR_DATABASE_CONNECTION = 'error/databaseConnection';
public const ROUTE_ERROR_MAINTENANCE = 'error/maintenanceError';
public const ROUTE_ERROR_DATABASE = 'error/databaseError';
public const ROUTE_UPGRADE = 'upgrade/index';
private CSRF $csrf;
private ThemeInterface $theme;
private Language $language;
@@ -180,33 +189,27 @@ final class Init extends HttpModuleBase
if (!$this->checkInstalled()) {
logger('Not installed', 'ERROR');
$this->router->response()
->redirect('index.php?r=install/index')
->send();
$this->router->response()->redirect(self::getUriFor(self::ROUTE_INSTALL))->send();
return;
throw new InitializationException('Not installed');
}
// Checks if the database is set up
if (!$this->databaseUtil->checkDatabaseConnection()) {
logger('Database connection error', 'ERROR');
$this->router->response()
->redirect('index.php?r=error/databaseConnection')
->send();
$this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_DATABASE_CONNECTION))->send();
return;
throw new InitializationException('Database connection error');
}
// Checks if maintenance mode is turned on
if ($this->checkMaintenanceMode()) {
logger('Maintenance mode', 'INFO');
$this->router->response()
->redirect('index.php?r=error/maintenanceError')
->send();
$this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_MAINTENANCE))->send();
return;
throw new InitializationException('Maintenance mode');
}
// Checks if upgrade is needed
@@ -215,22 +218,18 @@ final class Init extends HttpModuleBase
$this->config->generateUpgradeKey();
$this->router->response()
->redirect('index.php?r=upgrade/index')
->send();
$this->router->response()->redirect(self::getUriFor(self::ROUTE_UPGRADE))->send();
return;
throw new InitializationException('Upgrade needed');
}
// Checks if the database is set up
if (!$this->databaseUtil->checkDatabaseTables($this->configData->getDbName())) {
logger('Database checking error', 'ERROR');
$this->router->response()
->redirect('index.php?r=error/databaseError')
->send();
$this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_DATABASE))->send();
return;
throw new InitializationException('Database checking error');
}
if (!in_array($controller, self::NO_SESSION_ACTIVITY)) {
@@ -260,8 +259,6 @@ final class Init extends HttpModuleBase
// Initialize CSRF
$this->csrf->initialize();
return;
}
// Do not keep the PHP's session opened
@@ -285,9 +282,7 @@ final class Init extends HttpModuleBase
try {
$this->context->initialize();
} catch (Exception $e) {
$this->router
->response()
->header('HTTP/1.1', '500 Internal Server Error');
$this->router->response()->header('HTTP/1.1', '500 Internal Server Error');
throw $e;
}
@@ -332,9 +327,7 @@ final class Init extends HttpModuleBase
&& time() > ($lastActivity + $this->getSessionLifeTime())
) {
if ($this->router->request()->cookies()->get(session_name()) !== null) {
$this->router
->response()
->cookie(session_name(), '', time() - 42000);
$this->router->response()->cookie(session_name(), '', time() - 42000);
}
SessionContext::restart();
@@ -345,20 +338,18 @@ final class Init extends HttpModuleBase
if ($sidStartTime === 0) {
// Try to set PHP's session lifetime
@ini_set('session.gc_maxlifetime', $this->getSessionLifeTime());
} else {
if (!$inMaintenance
&& time() > ($sidStartTime + SessionContext::MAX_SID_TIME)
&& $this->context->isLoggedIn()
) {
try {
CryptSession::reKey($this->context);
} catch (CryptoException $e) {
logger($e->getMessage());
} elseif (!$inMaintenance
&& time() > ($sidStartTime + SessionContext::MAX_SID_TIME)
&& $this->context->isLoggedIn()
) {
try {
CryptSession::reKey($this->context);
} catch (CryptoException $e) {
logger($e->getMessage());
SessionContext::restart();
SessionContext::restart();
return;
}
return;
}
}
@@ -377,8 +368,7 @@ final class Init extends HttpModuleBase
try {
if ($this->isIndex || $timeout === null) {
$userTimeout = $this->getSessionTimeoutForUser($timeout)
?: $this->configData->getSessionTimeout();
$userTimeout = $this->getSessionTimeoutForUser($timeout) ?: $this->configData->getSessionTimeout();
logger('Session timeout: '.$userTimeout);
@@ -417,4 +407,9 @@ final class Init extends HttpModuleBase
return $default;
}
private static function getUriFor(string $route): string
{
return (new Uri(BootstrapWeb::$WEBROOT))->addParam('r', $route)->getUri();
}
}

View File

@@ -37,27 +37,27 @@ defined('APP_ROOT') || die();
*/
class Acl implements ActionsInterface
{
protected static ?Actions $action;
private ContextInterface $context;
private EventDispatcher $eventDispatcher;
protected static ?Actions $actions = null;
private ContextInterface $context;
private EventDispatcher $eventDispatcher;
/**
* Acl constructor.
*
* @param ContextInterface $context
* @param EventDispatcher $eventDispatcher
* @param Actions|null $action
* @param ContextInterface $context
* @param EventDispatcher $eventDispatcher
* @param Actions|null $actions
*/
public function __construct(
ContextInterface $context,
EventDispatcher $eventDispatcher,
Actions $action = null
Actions $actions = null
)
{
$this->context = $context;
$this->eventDispatcher = $eventDispatcher;
self::$action = $action;
self::$actions = $actions;
}
/**
@@ -66,7 +66,7 @@ class Acl implements ActionsInterface
public static function getActionRoute(string $actionId): string
{
try {
return self::$action !== null ? self::$action->getActionById($actionId)->getRoute() : '';
return self::$actions !== null ? self::$actions->getActionById($actionId)->getRoute() : '';
} catch (ActionNotFoundException $e) {
processException($e);
}
@@ -86,7 +86,7 @@ class Acl implements ActionsInterface
public static function getActionInfo(int $actionId, bool $translate = true): string
{
try {
$text = self::$action->getActionById($actionId)->getText();
$text = self::$actions->getActionById($actionId)->getText();
return $translate ? __($text) : $text;
} catch (ActionNotFoundException $e) {
@@ -275,7 +275,7 @@ class Acl implements ActionsInterface
}
try {
$actionName = self::$action->getActionById($action)->getName();
$actionName = self::$actions->getActionById($action)->getName();
} catch (ActionNotFoundException $e) {
$actionName = __u('N/A');
}

View File

@@ -105,11 +105,7 @@ abstract class BootstrapBase
});
// Manage requests for options
$this->router->respond(
'OPTIONS',
null,
$this->manageCorsRequest()
);
$this->router->respond('OPTIONS', null, $this->manageCorsRequest());
}
private function manageCorsRequest(): Closure

View File

@@ -96,7 +96,7 @@ final class BootstrapWeb extends BootstrapBase
$route = Filter::getString($request->param('r', 'index/index'));
if (!preg_match_all(
'#(?P<controller>[a-zA-Z]+)(?:/(?P<action>[a-zA-Z]+))?(?P<params>(/[a-zA-Z\d.]+)+)?#',
'#(?P<controller>[a-zA-Z]+)(?:/(?P<actions>[a-zA-Z]+))?(?P<params>(/[a-zA-Z\d.]+)+)?#',
$route,
$matches
)) {
@@ -134,10 +134,7 @@ final class BootstrapWeb extends BootstrapBase
$this->initializeCommon();
// TODO: remove??
if (APP_MODULE === 'web') {
$this->module->initialize($controllerName);
}
$this->module->initialize($controllerName);
logger(
sprintf(

View File

@@ -48,10 +48,8 @@ abstract class ModuleBase
* @param \SP\Core\Application $application
* @param \SP\Core\ProvidersHelper $providersHelper
*/
public function __construct(
Application $application,
ProvidersHelper $providersHelper
) {
public function __construct(Application $application, ProvidersHelper $providersHelper)
{
$this->config = $application->getConfig();
$this->configData = $this->config->getConfigData();
$this->context = $application->getContext();
@@ -59,7 +57,7 @@ abstract class ModuleBase
$this->providersHelper = $providersHelper;
}
abstract public function initialize(string $controller);
abstract public function initialize(string $controller): void;
/**
* Initializes event handlers