From a3035c8900771fb97460af0a5e46f89f1941284f Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 30 Dec 2024 21:08:20 +0000 Subject: [PATCH] Update the user session management into discrete interfaces. --- app/composer.lock | 24 ++++++++--------- app/src/Bootstrap.php | 6 +++++ .../Api/Database/DatabaseHandlerInterface.php | 3 +++ .../Api/User/UserSessionManagerInterface.php | 11 ++++++++ .../Database/DatabaseHandler.php | 4 ++- .../User/UserSessionManager.php | 26 +++++++++++++++++++ app/src/Middlewares.php | 9 +++++-- 7 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 app/src/Infrastructure/Api/User/UserSessionManagerInterface.php create mode 100644 app/src/Infrastructure/User/UserSessionManager.php diff --git a/app/composer.lock b/app/composer.lock index d495d26..7cb1ca0 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -4169,16 +4169,16 @@ }, { "name": "twig/twig", - "version": "v3.17.1", + "version": "v3.18.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "677ef8da6497a03048192aeeb5aa3018e379ac71" + "reference": "acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/677ef8da6497a03048192aeeb5aa3018e379ac71", - "reference": "677ef8da6497a03048192aeeb5aa3018e379ac71", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50", + "reference": "acffa88cc2b40dbe42eaf3a5025d6c0d4600cc50", "shasum": "" }, "require": { @@ -4233,7 +4233,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.17.1" + "source": "https://github.com/twigphp/Twig/tree/v3.18.0" }, "funding": [ { @@ -4245,7 +4245,7 @@ "type": "tidelift" } ], - "time": "2024-12-12T09:58:10+00:00" + "time": "2024-12-29T10:51:50+00:00" }, { "name": "vlucas/phpdotenv", @@ -4510,16 +4510,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.3.1", + "version": "v5.4.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" + "reference": "447a020a1f875a434d62f2a401f53b82a396e494" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", - "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/447a020a1f875a434d62f2a401f53b82a396e494", + "reference": "447a020a1f875a434d62f2a401f53b82a396e494", "shasum": "" }, "require": { @@ -4562,9 +4562,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.4.0" }, - "time": "2024-10-08T18:51:32+00:00" + "time": "2024-12-30T11:07:19+00:00" }, { "name": "phar-io/manifest", diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 3b816d7..b0bc778 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -42,6 +42,8 @@ use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Slovocast\Infrastructure\Database\DatabaseHandler; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\BasicUserAuthorization; +use Slovocast\Infrastructrue\Api\User\UserSessionManagerInterface; +use Slovocast\Infrastructure\User\UserSessionManager; /** @@ -139,6 +141,10 @@ class Bootstrap ); }, + UserSessionManagerInterface::class => function (ContainerInterface $container) { + return new UserSessionManager($container->get(SessionInterface::class)); + }, + /** * Application DI */ diff --git a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php index 99afc82..ffcba7a 100644 --- a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php +++ b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php @@ -7,6 +7,9 @@ use ReturnTypeWillChange; interface DatabaseHandlerInterface { /** + * Typically, this method would return the PDO object that represents the + * active connection to a database. + * * @return mixed The return must be an active connection to a database. */ #[ReturnTypeWillChange] diff --git a/app/src/Infrastructure/Api/User/UserSessionManagerInterface.php b/app/src/Infrastructure/Api/User/UserSessionManagerInterface.php new file mode 100644 index 0000000..c55d232 --- /dev/null +++ b/app/src/Infrastructure/Api/User/UserSessionManagerInterface.php @@ -0,0 +1,11 @@ +database};host={$this->host};port={$this->port}"; + return "mysql:dbname={$this->database};" + . "host={$this->host};" + . "port={$this->port}"; } public function getHost(): string diff --git a/app/src/Infrastructure/User/UserSessionManager.php b/app/src/Infrastructure/User/UserSessionManager.php new file mode 100644 index 0000000..95a2c95 --- /dev/null +++ b/app/src/Infrastructure/User/UserSessionManager.php @@ -0,0 +1,26 @@ +session->set('authenticated', true); + $this->session->set('user', $user->toArray()); + } + + public function clearUserAuthenticatedState(): void + { + $this->session->delete('authenticated'); + $this->session->delete('user'); + } +} diff --git a/app/src/Middlewares.php b/app/src/Middlewares.php index f11eea0..9281a29 100644 --- a/app/src/Middlewares.php +++ b/app/src/Middlewares.php @@ -16,6 +16,9 @@ use Twig\Error\LoaderError; class Middlewares { /** + * Set up all the application middleware. This is where the application + * should determine the environment the code is running in as well. + * * @param App $app The Slim application * @return void * @throws NotFoundExceptionInterface @@ -43,8 +46,10 @@ class Middlewares ]); // Add the global variables - $twig->getEnvironment()->addGlobal('site_name', $config->get('site.name')); - $twig->getEnvironment()->addGlobal('site_description', $config->get('site.description')); + $twig->getEnvironment() + ->addGlobal('site_name', $config->get('site.name')); + $twig->getEnvironment() + ->addGlobal('site_description', $config->get('site.description')); /** @var SessionInterface $session */ $session = $container->get(SessionInterface::class);