- WIP on Lean's event definition

This commit is contained in:
Dave Mc Nicoll committed 2025-05-19 17:29:52 +00:00
1 parent 42f33d9f13
commit 156defddc9
3 files changed
+15 -7

No files matched your search

+12 -5
View File
@@ -1,8 +1,6 @@
<?php
use Picea\Picea;
use function DI\autowire, DI\create, DI\get;
use Lean\{ Lean, Routing, Event\RoutingCompileRoutes, Event\RoutingMapRoutes };
use Mcnd\Event;
use Taxus\Taxus;
@@ -11,6 +9,9 @@ use Notes\Route\Attribute\Method\Route;
use Notes\Security\SecurityHandler;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\{ ResponseInterface, ServerRequestInterface };
use Ulmus\User\Entity\UserInterface;
use function DI\autowire, DI\create, DI\get;
return [
Event\EventManager::class => autowire(Event\EventManager::class),
@@ -25,6 +26,7 @@ return [
Event\EventDefinition::class => function($c) {
$extension = $c->has(\Picea\Extension\UrlExtension::class) ? $c->get(\Picea\Extension\UrlExtension::class) : null;
$user = $c->get(UserInterface::class);
return new Event\EventDefinition([
new class($extension) implements RoutingCompileRoutes {
@@ -40,7 +42,12 @@ return [
}
},
new class() implements RoutingMapRoutes {
new class($user) implements RoutingMapRoutes {
public function __construct(
protected UserInterface $user
) {}
public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route $attribute) : void
{
$class = $attribute->class;
@@ -54,7 +61,7 @@ return [
$securityHandler = $container->get(SecurityHandler::class);
if ( $redirect = $securityHandler->verify($class, $method) ) {
if ( empty($object->user) || ! $object->user->logged ) {
if (! $this->user->loggedIn() ) {
if ($container->has(Session::class)) {
$container->get(Session::class)->set('redirectedFrom', (string)$request->getUri());
}
@@ -65,7 +72,7 @@ return [
}
if ( $securityHandler->isLocked($class, $method) && $container->has(Taxus::class)) {
if ( $forbidden = $securityHandler->taxus($class, $method, $object->user ?? null) ) {
if ( $forbidden = $securityHandler->taxus($class, $method, $this->user) ) {
$routing->response = $forbidden;
return;