- Added a new CSRF token verification Middleware.
- Added a new ActionLog middleware to catch every modifying request and save the changes made and the user who did. - Added a new RequestLog to catch every request made whenever required.
This commit is contained in:
1 parent
9571c6a462
commit
4b86ac9e82
42 files changed
+1331
-148
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Api\Middleware\Security;
|
||||
|
||||
use Lean\Api\Controller\Security\ActionLog;
|
||||
use Lean\Api\Factory\LeanApiFormFactoryInterface;
|
||||
use Lean\Api\Form;
|
||||
use DI\Attribute\Inject;
|
||||
use Lean\Event\RoutingMapRoutes;
|
||||
use Lean\Routing;
|
||||
use Mcnd\Event\EventManager;
|
||||
use Notes\ObjectReflection;
|
||||
use Notes\ObjectResolver;
|
||||
use Notes\Route\Attribute\Method\Route;
|
||||
use Picea\Ui\Method\FormContextInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class ActionLogMiddleware implements MiddlewareInterface {
|
||||
|
||||
#[Inject]
|
||||
protected readonly LeanApiFormFactoryInterface $leanApiFormFactory;
|
||||
|
||||
#[Inject]
|
||||
protected readonly ActionLog $actionLogController;
|
||||
|
||||
#[Inject]
|
||||
protected readonly EventManager $eventManager;
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
|
||||
{
|
||||
$eventHandler = $this->createEventHandler($request);
|
||||
|
||||
$this->eventManager->eventRegister($eventHandler);
|
||||
|
||||
$handler = $handler->handle($request);
|
||||
|
||||
try {
|
||||
if ($handler->getStatusCode() < 300) {
|
||||
if (isset($eventHandler->request)) {
|
||||
if ($eventHandler->request->getAttribute('lean-api.security-action:attribute')) {
|
||||
$this->actionLogController->add($eventHandler->request, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(\Throwable $ex) {
|
||||
if (getenv('DEBUG')) {
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
protected function createEventHandler(ServerRequestInterface $request) : object
|
||||
{
|
||||
return new class($this->leanApiFormFactory->securityActionLogSaveContext($request)) implements RoutingMapRoutes {
|
||||
|
||||
public readonly ServerRequestInterface $request;
|
||||
|
||||
public function __construct(
|
||||
public readonly Form\Security\ActionLogContext $context,
|
||||
) {}
|
||||
|
||||
public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface &$request, Route $attribute): void
|
||||
{
|
||||
if (in_array($request->getMethod(), [ 'PATCH', 'POST', 'PUT', 'DELETE' ])) {
|
||||
$objectReflection = ObjectReflection::fromClass($attribute->class, null)->reflectClass();
|
||||
$actionLog = $objectReflection->methods[$attribute->classMethod]->getAttribute(\Lean\Api\Attribute\Security\ActionLog::class);
|
||||
|
||||
if ($actionLog) {
|
||||
$this->context->event = $attribute->name;
|
||||
$this->context->method = $request->getMethod();
|
||||
|
||||
$request = $this->request = $request
|
||||
->withAttribute('lean-api.security-action:attribute', $actionLog->object)
|
||||
->withAttribute('lean-api.security-action:context', $this->context);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user