- WIP on ulmus-user mechanics updates

This commit is contained in:
Dave Mc Nicoll 2025-06-13 19:14:30 +00:00
parent 69382f912b
commit af8b686e3b
9 changed files with 72 additions and 27 deletions

View File

@ -1,10 +1,12 @@
<?php <?php
use Lean\Factory\HttpFactory; use Lean\Factory\HttpFactoryInterface;
use Ulmus\User\{Entity, use Ulmus\User\{Authorize\HeaderAuthentication,
Entity,
Lib\Authenticate, Lib\Authenticate,
Lib\Authorize, Lib\Authorize,
Middleware\AuthenticationMiddleware,
Middleware\HeaderAuthenticationMiddleware, Middleware\HeaderAuthenticationMiddleware,
Middleware\PostRequestAuthenticationMiddleware, Middleware\PostRequestAuthenticationMiddleware,
Authorize\PostRequestAuthentication}; Authorize\PostRequestAuthentication};
@ -13,24 +15,29 @@ use Picea\Picea;
use Storage\{ Cookie, Session }; use Storage\{ Cookie, Session };
use Psr\Container\ContainerInterface;
use function DI\{get, autowire, create}; use function DI\{get, autowire, create};
return [ return [
Authenticate::class => create(Authenticate::class)->constructor(get(Entity\UserInterface::class), get(Session::class), get(Cookie::class), get('authentication.method')),
Authorize::class => create(Authorize::class)->constructor(get(Entity\UserInterface::class), get(Session::class), get(Cookie::class), get('authentication.method')), Authorize::class => create(Authorize::class)->constructor(get(Entity\UserInterface::class), get(Session::class), get(Cookie::class), get('authentication.method')),
HeaderAuthenticationMiddleware::class => create(HeaderAuthenticationMiddleware::class)->constructor(get(Authorize::class), get(Entity\UserInterface::class), get('authorize.error')), Authenticate::class => create(Authenticate::class)->constructor(get(Entity\UserInterface::class), get(Session::class), get(Cookie::class), get('authentication.method')),
PostRequestAuthentication::class => create(PostRequestAuthentication::class)->constructor(get(Authenticate::class), "email", "email", "password"), AuthenticationMiddleware::class => create(AuthenticationMiddleware::class)->constructor(get(Authorize::class), get('authorize.error')),
PostRequestAuthenticationMiddleware::class => create(PostRequestAuthenticationMiddleware::class)->constructor(get(Entity\UserInterface::class), get('authentication.error'), get(PostRequestAuthentication::class)), HeaderAuthentication::class => autowire(HeaderAuthentication::class),
HeaderAuthenticationMiddleware::class => create(HeaderAuthenticationMiddleware::class)->constructor(get(HeaderAuthentication::class)),
PostRequestAuthentication::class => create(PostRequestAuthentication::class)->constructor("email", "email", "password"),
PostRequestAuthenticationMiddleware::class => create(PostRequestAuthenticationMiddleware::class)->constructor(get(PostRequestAuthentication::class)),
'authentication.method' => null, 'authentication.method' => null,
'authorize.error' => function($c) { 'authorize.error' => function(ContainerInterface $c) {
return function(array $errorData) { return function(array $errorData) use ($c) {
return HttpFactory::createJsonResponse($errorData + [ return $c->get(HttpFactoryInterface::class)::createJsonResponse($errorData + [
'api.error' => "Authorization failed", 'api.error' => "Authorization failed",
'api.datetime' => (new \DateTime)->format(\DateTime::ISO8601), 'api.datetime' => (new \DateTime)->format(\DateTime::ISO8601),
], 403); ], 403);
@ -38,8 +45,8 @@ return [
}, },
'authentication.error' => function($c, Picea $picea) { 'authentication.error' => function($c, Picea $picea) {
return function($message) use ($picea) { return function($message) use ($c, $picea) {
return HttpFactory::createHtmlResponse($picea->renderHtml('lean/error/500', [ return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml('lean/error/500', [
'title' => "Authentication failed", 'title' => "Authentication failed",
'subtitle' => "", 'subtitle' => "",
'message' => $message, 'message' => $message,

View File

@ -1,10 +1,12 @@
<?php <?php
use function DI\autowire, DI\create, DI\get; use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Laminas\{ Diactoros\ServerRequestFactory, HttpHandlerRunner\Emitter\EmitterInterface, HttpHandlerRunner\Emitter\SapiEmitter }; use Laminas\{ Diactoros\ServerRequestFactory, HttpHandlerRunner\Emitter\EmitterInterface, HttpHandlerRunner\Emitter\SapiEmitter };
use Lean\Factory\{ HttpFactory, HttpFactoryInterface };
use Picea\Picea;
use function DI\autowire, DI\create, DI\get;
return [ return [
ServerRequestInterface::class => function ($c) { ServerRequestInterface::class => function ($c) {
@ -14,4 +16,18 @@ return [
}, },
EmitterInterface::class => create(SapiEmitter::class), EmitterInterface::class => create(SapiEmitter::class),
HttpFactoryInterface::class => create(HttpFactory::class),
'error.401' => function(ContainerInterface $c, Picea $picea) {
return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml("lean/error/401", []), 401);
},
'error.404' => function(ContainerInterface $c, Picea $picea) {
return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml("lean/error/404", []), 404);
},
'error.500' => function(ContainerInterface $c, Picea $picea) {
return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml("lean/error/500", []), 500);
}
]; ];

0
skeleton/lean Executable file → Normal file
View File

View File

@ -4,6 +4,7 @@ use function DI\autowire, DI\create, DI\get;
use %NAMESPACE%\Entity; use %NAMESPACE%\Entity;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Ulmus\Entity\Field\Datetime, use Ulmus\Entity\Field\Datetime,
@ -21,16 +22,18 @@ use TheBugs\Email\{ EmailConfiguration, MailerInterface, SwiftMailer };
use Taxus\{ Taxus, PermissionGrantInterface }; use Taxus\{ Taxus, PermissionGrantInterface };
use Lean\Factory\HttpFactoryInterface;
return [ return [
\Ulmus\User\Entity\UserInterface::class => autowire(Entity\User::class), \Ulmus\User\Entity\UserInterface::class => autowire(Entity\User::class),
SecurityHandler::class => create(SecurityHandler::class)->constructor(function() { SecurityHandler::class => create(SecurityHandler::class)->constructor(function(ContainerInterface $c) {
return Lean\Factory\HttpFactory::createRedirectResponse(getenv("URL_BASE")."/login"); return $c->get(HttpFactoryInterface::class)::createRedirectResponse(getenv("URL_BASE")."/login");
}, get('authentication.unauthorize'), get(Taxus::class)), }, get('authentication.unauthorize'), get(Taxus::class)),
'authentication.unauthorize' => function($c, Picea $picea) { 'authentication.unauthorize' => function(ContainerInterface $c, Picea $picea) {
return function($message) use ($picea) { return function($message) use ($c, $picea) {
return Lean\Factory\HttpFactory::createHtmlResponse($picea->renderHtml('lean/error/401', [ return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml('lean/error/401', [
'title' => "", 'title' => "",
'subtitle' => "", 'subtitle' => "",
'message' => $message, 'message' => $message,

View File

@ -4,7 +4,7 @@ use Picea\Picea;
use Negundo\Client\{ NegundoMiddleware, SoftwareConfig }; use Negundo\Client\{ NegundoMiddleware, SoftwareConfig };
use Lean\Factory\HttpFactory; use Lean\Factory\HttpFactoryInterface;
use Psr\Http\Server\MiddlewareInterface, use Psr\Http\Server\MiddlewareInterface,
Psr\Http\Message\ServerRequestInterface, Psr\Http\Message\ServerRequestInterface,
@ -31,11 +31,11 @@ return [
"errorHandler" => create(NegundoMiddleware::class)->constructor(get(SoftwareConfig::class), null, get('app.errorhandler.html')), "errorHandler" => create(NegundoMiddleware::class)->constructor(get(SoftwareConfig::class), null, get('app.errorhandler.html')),
'app.errorhandler.html' => function($c, Picea $picea) { 'app.errorhandler.html' => function(\Psr\Container\ContainerInterface $c, Picea $picea) {
return function(\Throwable $exception) use ($picea) { return function(\Throwable $exception) use ($c, $picea) {
error_log($exception->getMessage()); error_log($exception->getMessage());
return HttpFactory::createHtmlResponse($picea->renderHtml('lean/error/500', [ return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml('lean/error/500', [
'title' => "Une erreur s'est produite lors de l'exécution du script.", 'title' => "Une erreur s'est produite lors de l'exécution du script.",
'subtitle' => "Êtes-vous connecté avec le bon compte ?", 'subtitle' => "Êtes-vous connecté avec le bon compte ?",
'message' => $exception->getMessage(), 'message' => $exception->getMessage(),

View File

@ -58,7 +58,7 @@ class ApplicationStrategy extends Strategy\ApplicationStrategy {
} }
} }
return new \Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); return $this->di->get('error.404');
} }
}; };
} }

View File

@ -7,7 +7,7 @@ use Lean\Response\{ DownloadResponse, ImageResponse, FileDownloadResponse, PdfRe
use Laminas\Diactoros\Response; use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
class HttpFactory class HttpFactory implements HttpFactoryInterface
{ {
public static function createResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface public static function createResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface
{ {

View File

@ -0,0 +1,19 @@
<?php
namespace Lean\Factory;
use Psr\Http\Message\ResponseInterface;
interface HttpFactoryInterface
{
public static function createResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface;
public static function createRedirectResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface;
public static function createHtmlResponse(string $html, int $code = 200, array $headers = []) : ResponseInterface;
public static function createTextResponse(string $text, int $code = 200, array $headers = []) : ResponseInterface;
public static function createJsonResponse(mixed $data, int $code = 200, array $headers = []) : ResponseInterface;
public static function createEmptyResponse(int $code = 204, array $headers = []) : ResponseInterface;
public static function createPdfResponse(\Stringable|string $binary, int $code = 200, array $headers = []) : ResponseInterface;
public static function createDownloadableResponse(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface;
public static function createImageResponse(string $data, int $code = 200, array $headers = []) : ResponseInterface;
public static function createFileDownloadResponse(string $path, int $code = 200, array $headers = []) : ResponseInterface;
}

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 40 KiB