diff --git a/composer.json b/composer.json index 634ee44..5c73572 100644 --- a/composer.json +++ b/composer.json @@ -27,9 +27,9 @@ "mcnd/tell": "dev-master", "mcnd/dump": "dev-master", "mcnd/event": "dev-master", - "mcnd/ulmus-user": "dev-master", "mcnd/thebugs": "dev-master", "mcnd/taxus": "dev-master", + "mcnd/notes": "dev-master", "psr/simple-cache": "*" }, "repositories": [ @@ -49,10 +49,6 @@ "type": "vcs", "url": "https://git.mcnd.ca/mcndave/ulmus.git" }, - { - "type": "vcs", - "url": "https://git.mcnd.ca/mcndave/ulmus-user.git" - }, { "type": "vcs", "url": "https://git.mcnd.ca/mcndave/notes.git" diff --git a/meta/definitions/event.php b/meta/definitions/event.php index 36bd99d..84c081f 100644 --- a/meta/definitions/event.php +++ b/meta/definitions/event.php @@ -25,11 +25,16 @@ return [ Event\EventDefinition::class => function($c) { return new Event\EventDefinition([ - new class() implements RoutingCompileRoutes { + + new class($c->has(\Picea\Extension\UrlExtension::class) ? $c->get(\Picea\Extension\UrlExtension::class) : null) implements RoutingCompileRoutes { + public function __construct( + protected ? \Picea\Extension\UrlExtension $extension, + ) {} + public function execute(Routing $routing, Route $attribute) : void { if (null !== ($name = $attribute->name ?? null)) { - $routing->extension->registerRoute($name, $attribute->getRoute(), $attribute->class, $attribute->classMethod, $attribute->methods ?? (array)$attribute->method); + $this->extension->registerRoute($name, $attribute->getRoute(), $attribute->class, $attribute->classMethod, $attribute->methods ?? (array)$attribute->method); } } }, @@ -45,7 +50,9 @@ return [ # Checking if user needs to be logged if ( $container->has(SecurityHandler::class) ){ - if ( $redirect = $container->get(SecurityHandler::class)->verify($class, $method) ) { + $securityHandler = $container->get(SecurityHandler::class); + + if ( $redirect = $securityHandler->verify($class, $method) ) { if ( empty($object->user) || ! $object->user->logged ) { $routing->session->set('redirectedFrom', (string) $request->getUri()); $routing->response = $redirect; @@ -54,7 +61,7 @@ return [ } } - if ( $container->has(Taxus::class) ) { + if ( $securityHandler->isLocked($class, $method) && $container->has(Taxus::class) ) { if ( $forbidden = $container->get(SecurityHandler::class)->taxus($class, $method, $object->user ?? null) ) { $routing->response = $forbidden; diff --git a/meta/definitions/security.php b/meta/definitions/security.php deleted file mode 100644 index 1cc4c09..0000000 --- a/meta/definitions/security.php +++ /dev/null @@ -1,22 +0,0 @@ - function ($c) { - return ( new Taxus( $c->get(PermissionGrantInterface::class) ) )->add( - [ new Privilege("dev", "Is a developper of this application."), "is_dev" ], - [ new Privilege("admin", "Can manage mostly everything from this application."), "is_admin" ], - [ new Privilege("user", "Is an authenticated user."), "is_user" ], - [ new Privilege("anonymous", "Is an anonymous (unauthenticated) user."), "is_anonymous" ], - ); - }, - - PermissionGrantInterface::class => create(DefaultPermissionGrant::class)->constructor(get(ServerRequestInterface::class), get(Session::class)), -]; diff --git a/skeleton/.env b/skeleton/.env index b9cc2c8..7d73d77 100644 --- a/skeleton/.env +++ b/skeleton/.env @@ -23,6 +23,12 @@ DEFAULT_TIME = "fr.UTF-8" DEFAULT_TIME_FALLBACK = "french.UTF-8" # Database +#SQLITE_PATH = "var/home.sqlite3" +#SQLITE_ADAPTER = "SQLite" +#SQLITE_PRAGMA_BEGIN = "foreign_keys=ON,synchronous=NORMAL" +#SQLITE_PRAGMA_DEBUG_BEGIN = "journal_mode=WAL" +#SQLITE_PRAGMA_CLOSE = "analysis_limit=500,optimize" + DATABASE_PORT = "" DATABASE_HOST = "" DATABASE_NAME = "" diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index ea0b2be..adfc2a8 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -13,8 +13,8 @@ use Psr\Http\Message\{ ServerRequestInterface, ResponseInterface }; use function file_get_contents; -#[Security(locked: true)] -#[Route(method: [ "GET", "POST", "DELETE" ])] +#[Security(locked: true, realm: "Protected Area")] +#[Route(method: [ "GET", "POST" ])] trait ControllerTrait { public ? \Notes\Breadcrumb\Breadcrumb $breadcrumb; diff --git a/src/Factory/HttpFactory.php b/src/Factory/HttpFactory.php index 6f32edc..33324db 100644 --- a/src/Factory/HttpFactory.php +++ b/src/Factory/HttpFactory.php @@ -4,10 +4,16 @@ namespace Lean\Factory; use Laminas\Diactoros\Response\{EmptyResponse, HtmlResponse, JsonResponse, RedirectResponse, TextResponse}; use Lean\Response\{ DownloadResponse, ImageResponse, FileDownloadResponse, PdfResponse }; +use Laminas\Diactoros\Response; use Psr\Http\Message\ResponseInterface; class HttpFactory { + public static function createResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface + { + return new Response($url, $code, $headers); + } + public static function createRedirectResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface { return new RedirectResponse($url, $code, $headers); diff --git a/src/Routing.php b/src/Routing.php index 8095452..eb07ca6 100644 --- a/src/Routing.php +++ b/src/Routing.php @@ -2,9 +2,6 @@ namespace Lean; -use Notes\Annotation; -use Taxus\Taxus; - use League\Route\RouteGroup, League\Route\Router; @@ -14,54 +11,33 @@ use Psr\Http\Message\ServerRequestInterface, use Notes\Route\RouteFetcher; -use Notes\Security\SecurityHandler; - -use Notes\Tell\LanguageHandler; - -use Picea\Picea, - Picea\Extension\UrlExtension; - -use Storage\Cookie, - Storage\Session; - use Mcnd\Event\EventManager; -use function DI\autowire, DI\create; - class Routing { - public Annotation $selectedRoute; - public ResponseInterface $response; public function __construct( - public Session $session, - public Cookie $cookie, - public UrlExtension $extension, public Router $router, public RouteFetcher $fetcher, - public SecurityHandler $security, - public Taxus $taxus, public EventManager $eventManager, ) { } public function registerRoute(ContainerInterface $container, string $urlBase) { $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) { - foreach($this->fetcher->compile() as $annotation) { - $this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $annotation); + foreach($this->fetcher->compile() as $attribute) { + $this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $attribute); - /* @deprecated annotation->method will become standard when using native attributes */ - foreach((array) ( $annotation->method ?? $annotation->methods ) as $method) { - # Mapping every URLs from annotations in searched folders (Api, Controller, etc...) - $route->map(strtoupper($method), $annotation->getRoute(), function (ServerRequestInterface $request, array $arguments) use ( - $container, $route, $annotation - ) : ResponseInterface - { - $class = $annotation->class; - $method = $annotation->classMethod; + # Mapping every URLs from attributes in searched folders (Api, Controller, etc...) + foreach((array) $attribute->method as $method) { + $route->map(strtoupper($method), $attribute->getRoute(), function (ServerRequestInterface $request, array $arguments) use ( + $container, $route, $attribute + ) : ResponseInterface { + $class = $attribute->class; + $method = $attribute->classMethod; $object = $container->get($class); - $this->eventManager->execute(Event\RoutingMapRoutes::class, $this, $container, $request, $annotation); + $this->eventManager->execute(Event\RoutingMapRoutes::class, $this, $container, $request, $attribute); $container->set(ServerRequestInterface::class, $request);