- 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:
Dave Mc Nicoll committed 2026-10-22 15:03:18 +00:00
1 parent 9571c6a462
commit 4b86ac9e82
42 files changed
+1331 -148

No files matched your search

+40 -26
View File
@@ -21,35 +21,31 @@ trait LeanApiTrait
protected ContainerInterface $container;
#[Ignore]
public function renderMarkdown(string $filepath, array $entities = [], array $forms = [], int $code = 200, array $headers = []) : ResponseInterface
public function renderMarkdown(string $filepath, array $entities = [], array $forms = [], int $code = 200, array $headers = []): ResponseInterface
{
if ( ! class_exists(CommonMarkConverter::class)) {
if (!class_exists(CommonMarkConverter::class)) {
throw new \BadFunctionCallException("League\CommonMark seems to be missing, please install dependency before trying to render Markdown content");
}
$markdown = ( new CommonMarkConverter() )->convert(file_get_contents($filepath))->getContent();
$markdown = (new CommonMarkConverter())->convert(file_get_contents($filepath))->getContent();
if (str_contains($markdown, '{route:descriptor}'))
{
if (str_contains($markdown, '{route:descriptor}')) {
$markdown = str_replace('{route:descriptor}', $this->renderRawView('lean-api/route_descriptor', [
'routes' => (new RouteDescriptor($this, $this->picea->compiler->getExtensionFromToken('url'), $this->container))->getRoutes()
]), $markdown);
}
if (str_contains($markdown, '{request:debugger}'))
{
if (str_contains($markdown, '{request:debugger}')) {
$markdown = str_replace('{request:debugger}', $this->renderRawView('lean-api/request_debugger', []), $markdown);
}
if (str_contains($markdown, '{entity:descriptor}'))
{
if (str_contains($markdown, '{entity:descriptor}')) {
$markdown = str_replace('{entity:descriptor}', $this->renderRawView('lean-api/entity_descriptor', [
'entities' => (new EntityDescriptor())->getEntities(... $entities)
]), $markdown);
}
if (str_contains($markdown, '{form:descriptor}'))
{
if (str_contains($markdown, '{form:descriptor}')) {
$markdown = str_replace('{form:descriptor}', $this->renderRawView('lean-api/form_descriptor', [
'forms' => (new FormDescriptor())->getForms(... $forms)
]), $markdown);
@@ -59,7 +55,7 @@ trait LeanApiTrait
}
#[Ignore]
protected function output(\JsonSerializable|array $data, int $count) : ResponseInterface
protected function output(\JsonSerializable|array $data, int $count): ResponseInterface
{
return $this->renderJson([
'data' => $data,
@@ -68,18 +64,18 @@ trait LeanApiTrait
}
#[Ignore]
protected function outputSearchResult(ApiSearchRequestInterface $apiSearchRequest) : ResponseInterface
protected function outputSearchResult(ApiSearchRequestInterface $apiSearchRequest): ResponseInterface
{
return $this->output($apiSearchRequest->getResult(), $apiSearchRequest->getSearch()->count);
}
#[Ignore]
protected function searchEntityFromRequest(ServerRequestInterface $request, string $entityType, ? string $resultKey = null) : ServerRequestInterface
protected function searchEntityFromRequest(ServerRequestInterface $request, string $entityType, ?string $resultKey = null): ServerRequestInterface
{
$search = $entityType::searchRequest()->fromRequest($request);
$entity = $entityType::repository()->filterServerRequest($search)->loadOne() ?? false;
$entity = $entityType::repository()->filterServerRequest($search, false)->loadOne() ?? false;
if (! $entity ) {
if (!$entity) {
throw new \InvalidArgumentException(sprintf("L'entré pour l'entité demandé (%s) est introuvable avec le ou les arguments fournis '%s'", $entityType, json_encode($request->getAttributes(), \JSON_PARTIAL_OUTPUT_ON_ERROR)));
}
@@ -87,11 +83,17 @@ trait LeanApiTrait
$request = $request->withAttribute("lean.searchRequest", $this->searchEntityResult());
}
if ($request->getAttribute('lean-api.security-action:context')) {
$request->getAttribute('lean-api.security-action:context')->attachEntity($entity);
}
$request->getAttribute("lean.searchRequest")[$resultKey ?: $entityType] = new class($search, $entity) implements ApiSearchRequestInterface {
public function __construct(
public readonly SearchRequestInterface $search,
public readonly EntityInterface $entity,
) {}
public readonly EntityInterface $entity,
)
{
}
public function getSearch(): SearchRequestInterface
{
@@ -108,12 +110,12 @@ trait LeanApiTrait
}
#[Ignore]
protected function searchEntitiesFromRequest(ServerRequestInterface $request, string $entityType, ? string $resultKey = null) : ServerRequestInterface
protected function searchEntitiesFromRequest(ServerRequestInterface $request, string $entityType, ?string $resultKey = null, bool $count = true): ServerRequestInterface
{
$search = $entityType::searchRequest()->fromRequest($request);
$entity = $entityType::repository()->filterServerRequest($search)->loadAll();
$entity = $entityType::repository()->filterServerRequest($search, $count)->loadAll();
if (! $entity ) {
if (!$entity) {
throw new \InvalidArgumentException(sprintf("L'entré pour l'entité demandé (%s) est introuvable avec le ou les arguments fournis '%s'", $entityType, json_encode($request->getAttributes())));
}
@@ -124,8 +126,10 @@ trait LeanApiTrait
$request->getAttribute("lean.searchRequest")[$resultKey ?: $entityType] = new class($search, $entity) implements ApiSearchRequestInterface {
public function __construct(
public readonly SearchRequestInterface $search,
public readonly EntityCollection $collection,
) {}
public readonly EntityCollection $collection,
)
{
}
public function getSearch(): SearchRequestInterface
{
@@ -142,8 +146,9 @@ trait LeanApiTrait
}
#[Ignore]
protected function searchEntityResult() :\ArrayObject {
return new class()extends \ArrayObject implements ApiSearchRequestInterface {
protected function searchEntityResult(): \ArrayObject
{
return new class() extends \ArrayObject implements ApiSearchRequestInterface {
public function getSearch(): SearchRequestInterface
{
@@ -154,7 +159,8 @@ trait LeanApiTrait
return $this[array_key_last($this->getArrayCopy())]->getSearch();
}
public function getResult() : EntityInterface|EntityCollection {
public function getResult(): EntityInterface|EntityCollection
{
if ($this->count() !== 1) {
throw new \LogicException("Search results contains more than one results. Impossible to continue without precising which one you needs.");
}
@@ -163,4 +169,12 @@ trait LeanApiTrait
}
};
}
#[Ignore]
protected function attachActionEntity(ServerRequestInterface $request, EntityInterface $entity): void
{
if ($request->getAttribute('lean-api.security-action:attribute')) {
$request->getAttribute('lean-api.security-action:context')->attachEntity($entity);
}
}
}