diff --git a/meta/definitions/authorize.php b/meta/definitions/authorize.php index 33b3f5f..1b49249 100644 --- a/meta/definitions/authorize.php +++ b/meta/definitions/authorize.php @@ -2,16 +2,16 @@ use Lean\Factory\HttpFactoryInterface; -use Ulmus\User\{Authorize\HeaderAuthentication, +use Ulmus\User\{ + Authorize\CookieAuthentication, + Authorize\HeaderAuthentication, Entity, - Lib\Authenticate, Lib\Authorize, Middleware\AuthenticationMiddleware, Middleware\HeaderAuthenticationMiddleware, + Middleware\CookieAuthenticationMiddleware, Middleware\PostRequestAuthenticationMiddleware, - Middleware\LocalMacAddressMiddleware, - Authorize\MacAddressAuthentication, Authorize\PostRequestAuthentication}; use Picea\Picea; @@ -32,14 +32,14 @@ return [ HeaderAuthenticationMiddleware::class => create(HeaderAuthenticationMiddleware::class)->constructor(get(HeaderAuthentication::class)), + CookieAuthentication::class => autowire(CookieAuthentication::class), + + CookieAuthenticationMiddleware::class => create(CookieAuthenticationMiddleware::class)->constructor(get(CookieAuthentication::class), get(Cookie::class)), + PostRequestAuthentication::class => create(PostRequestAuthentication::class)->constructor("email", "email", "password"), PostRequestAuthenticationMiddleware::class => create(PostRequestAuthenticationMiddleware::class)->constructor(get(PostRequestAuthentication::class)), - MacAddressAuthentication::class => create(MacAddressAuthentication::class)->constructor(get(Authenticate::class), "mac"), - - LocalMacAddressMiddleware::class => create(LocalMacAddressMiddleware::class)->constructor(get(Entity\UserInterface::class), get('authentication.error'), get(MacAddressAuthentication::class)), - 'authentication.method' => null, 'authorize.error' => function(ContainerInterface $c) { diff --git a/meta/definitions/http.php b/meta/definitions/http.php index b2b3ce3..c8369ea 100644 --- a/meta/definitions/http.php +++ b/meta/definitions/http.php @@ -6,6 +6,9 @@ use Psr\Http\Message\ServerRequestInterface; use Laminas\{ Diactoros\ServerRequestFactory, HttpHandlerRunner\Emitter\EmitterInterface, HttpHandlerRunner\Emitter\SapiEmitter }; use Lean\Factory\{ HttpFactory, HttpFactoryInterface }; use Picea\Picea; +use Storage\Cookie, + Storage\Session, + Storage\SessionMiddleware; use function DI\autowire, DI\create, DI\get; @@ -24,6 +27,22 @@ return [ HttpFactoryInterface::class => create(HttpFactory::class), + Cookie::class => create(Cookie::class)->constructor([ + 'secure' => true, + 'path' => getenv('URL_BASE') ?: '/', + 'httponly' => true, + 'samesite'=> "Lax", + ], getenv("LEAN_RANDOM")), + + Session::class => create(Session::class)->constructor(get(Cookie::class), [ 'path' => getenv('URL_BASE') ?: '/', ]), + + SessionMiddleware::class => create(SessionMiddleware::class)->constructor(get(Cookie::class), [ + 'name' => "lean_sess_" . substr(md5(getenv("LEAN_RANDOM")), 0, 12), + 'path' => getenv('URL_BASE') ?: '/', + 'samesite' => getenv("LEAN_COOKIE_SAMESITE") ?? "Lax", + 'secure' => (bool) getenv('LEAN_COOKIE_SECURE') ?? false + ]), + 'error.401' => function(ContainerInterface $c, Picea $picea) { return $c->get(HttpFactoryInterface::class)::createHtmlResponse($picea->renderHtml("lean/error/401", []), 401); }, diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index 5d0140f..c783383 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -71,6 +71,7 @@ return [ $router->addPatternMatcher('email', '[^@ \t\r\n/]+@[^@ \t\r\n/]+\.[^@ \t\r\n/]+'); $router->addPatternMatcher('ids', '\b\d[\d,]*\b'); + $routing = $container->get(Lean\Routing::class); $routing->registerRoute($container, getenv('URL_BASE')); diff --git a/meta/definitions/software.php b/meta/definitions/software.php index b3ef6b2..48c2278 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -6,9 +6,6 @@ use TheBugs\JavascriptMiddleware; use Lean\Lean; -use Storage\Cookie, - Storage\Session, - Storage\SessionMiddleware; use function DI\autowire, DI\create, DI\get, DI\add; @@ -78,12 +75,6 @@ return [ JavascriptMiddleware::class => create(JavascriptMiddleware::class), - Cookie::class => create(Cookie::class)->constructor([ 'secure' => true, 'path' => getenv('URL_BASE') ?: '/', ], getenv("LEAN_RANDOM")), - - Session::class => create(Session::class)->constructor(get(Cookie::class), [ 'path' => getenv('URL_BASE') ?: '/', ]), - - SessionMiddleware::class => create(SessionMiddleware::class)->constructor(get(Cookie::class), [ 'name' => "lean_sess_" . substr(md5(getenv("LEAN_RANDOM")), 0, 12), 'path' => getenv('URL_BASE') ?: '/' ]), - 'git.commit' => function($c) { if ( getenv("DEBUG") ) { return uniqid("debug_"); diff --git a/src/ApplicationStrategy.php b/src/ApplicationStrategy.php index 2897e70..f297672 100644 --- a/src/ApplicationStrategy.php +++ b/src/ApplicationStrategy.php @@ -37,5 +37,36 @@ class ApplicationStrategy extends Strategy\ApplicationStrategy { public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception): MiddlewareInterface { return $this->getContainer()->has(MethodNotAllowedInterface::class) ? $this->getContainer()->get(MethodNotAllowedInterface::class) : parent::getMethodNotAllowedDecorator($exception); + } + /*======= + public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception): MiddlewareInterface + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + if (php_sapi_name() !== 'cli' and ! defined('STDIN')) { + return $this->throw404($request); + } + + return $handler->handle($request);; + } + + public function throw404(ServerRequestInterface $request) : ResponseInterface + { + if ( getenv('DEBUG') && $this->di->has(\Picea\Asset\Asset::class) ) { + $params = $request->getServerParams(); + + $scpName = basename(explode('?', $params['REQUEST_URI'] ?? "", 2)[0]); + list(, $ext) = array_pad(explode('.', $scpName), 2, null); + + if ($ext && in_array($ext, ApplicationStrategy::ASSET_TRIGGER_UPDATE)) { + $this->di->get(\Picea\Asset\Asset::class)->launchInstall(); + } + } + + return $this->di->get('error.404'); + } + }; + } + +>>>>>>> af8b686 (- WIP on ulmus-user mechanics updates)*/ } \ No newline at end of file diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 31dcbc0..ff632e0 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -9,6 +9,7 @@ use Notes\Route\Attribute\Object\Route; use Notes\Security\Attribute\Security; use Picea, Picea\Ui\Method\FormContext; use Picea\Ui\Method\FormContextInterface; +use Picea\Ui\Method\FormInterface; use TheBugs\Email\MailerInterface; use Storage\{ Cookie, Session }; use League\CommonMark\CommonMarkConverter; @@ -210,6 +211,16 @@ trait ControllerTrait { return $this->picea->compiler->getExtensionFromToken('route')->buildRouteUrl($name, $parameters); } + #[Ignore] + public function form(FormInterface $form, ? FormContextInterface $formContext = null) : Picea\Ui\Method\FormHandler + { + if (! $this->picea ) { + throw new \Exception("A picea object must be provided on class initialization to use this method."); + } + + return $this->picea->compiler->getExtensionInstanceOf(Picea\Ui\Method\Form::class)->formClass($form, $formContext); + } + #[Ignore] public function json($data, int $flags = 0) : string { diff --git a/src/Kernel.php b/src/Kernel.php index 4c926d9..27754e4 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -24,7 +24,7 @@ class Kernel public array $paths = []; - public int $errorReportingDev = -1; + public int $errorReportingDev = -1 & ~E_USER_DEPRECATED & ~E_DEPRECATED ; public int $errorReportingProd = E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED /*& ~E_NOTICE*/; @@ -34,10 +34,12 @@ class Kernel public function __construct( public string $projectPath - ) - { + ) { # Still not sure about adding a folder check each call just to add a basic project setup error ... # File::assertFolderExists($this->projectPath); + + define('IS_CLI', php_sapi_name() === 'cli' OR defined('STDIN')); + $this->setEnvironment(); $this->initializeEngine(); $this->setupContainer(); @@ -78,8 +80,10 @@ class Kernel $path = getenv("PROJECT_PATH") . DIRECTORY_SEPARATOR . getenv($envkey); if (getenv('DEBUG') && ! file_exists($path)) { - if (false === mkdir($path, 0755, true)) { - throw new \Exception(sprintf("Folder '%s' could not be created", $path)); + if (false === @mkdir($path, 0755, true)) { + if (! IS_CLI) { + throw new \Exception(sprintf("Folder '%s' could not be created", $path)); + } } } diff --git a/src/Routing.php b/src/Routing.php index c359c40..adc6a50 100644 --- a/src/Routing.php +++ b/src/Routing.php @@ -44,6 +44,11 @@ class Routing { $container->set(ServerRequestInterface::class, $request); + # Decoding arguments received from the router + foreach($arguments as &$arg) { + $arg = urldecode($arg); + } + return $this->response ?? $object->$method($request, $arguments); }); }