From 75760f5381850a0ab6988eff3e851c6ed9747edb Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 18 Dec 2024 16:46:22 +0100 Subject: [PATCH] First sketch logic on endpoint data --- lib/Service/EndpointService.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/Service/EndpointService.php b/lib/Service/EndpointService.php index a4a7efb..a8a41ae 100644 --- a/lib/Service/EndpointService.php +++ b/lib/Service/EndpointService.php @@ -4,6 +4,7 @@ use Adbar\Dot; use Exception; +use JWadhams\JsonLogic; use OCA\OpenConnector\Db\SourceMapper; use OCA\OpenConnector\Service\AuthenticationService; use OCA\OpenConnector\Service\MappingService; @@ -62,6 +63,12 @@ public function __construct( */ public function handleRequest(Endpoint $endpoint, IRequest $request, string $path): JSONResponse { + $errors = $this->checkConditions($endpoint, $request); + + if($errors !== []) { + return new JSONResponse(['error' => 'The following parameters are not correctly set', 'fields' => $errors], 400); + } + try { // Check if endpoint connects to a schema if ($endpoint->getTargetType() === 'register/schema') { @@ -299,6 +306,21 @@ private function getHeaders(array $server, bool $proxyHeaders = false): array ); } + private function checkConditions(Endpoint $endpoint, IRequest $request): array + { + $conditions = $endpoint->getConditions(); + $data['parameters'] = $request->getParams(); + $data['headers'] = $this->getHeaders($request->server, true); + + $result = JsonLogic::apply(logic: $conditions, data: $data); + + if($result === true || $result === []) { + return []; + } + + return $result; + } + /** * Handles requests for source-based endpoints *