diff --git a/assets/entrepot/api/geonetwork.ts b/assets/entrepot/api/geonetwork.ts index 280229d2..fbf47d36 100644 --- a/assets/entrepot/api/geonetwork.ts +++ b/assets/entrepot/api/geonetwork.ts @@ -3,7 +3,7 @@ import SymfonyRouting from "../../modules/Routing"; import { jsonFetch } from "../../modules/jsonFetch"; const getMetadataInfos = (fileIdentifier: string, otherOptions: RequestInit = {}) => { - const url = SymfonyRouting.generate("cartesgouvfr_geonetwork_metadata_get_infos", { fileIdentifier }); + const url = SymfonyRouting.generate("cartesgouvfr_geonetwork_metadata_get", { fileIdentifier }); return jsonFetch(url, { ...otherOptions, }); diff --git a/assets/entrepot/pages/AccessesRequest.tsx b/assets/entrepot/pages/AccessesRequest.tsx index 0f30662e..b1755a57 100644 --- a/assets/entrepot/pages/AccessesRequest.tsx +++ b/assets/entrepot/pages/AccessesRequest.tsx @@ -9,7 +9,8 @@ import { useQuery } from "@tanstack/react-query"; import { ComponentProps, FC, ReactNode, useMemo, useState } from "react"; import { useForm } from "react-hook-form"; import * as yup from "yup"; -import { GeonetworkMetadataResponse } from "../../@types/app"; + +import type { GeonetworkMetadataResponse } from "../../@types/app"; import AppLayout from "../../components/Layout/AppLayout"; import LoadingText from "../../components/Utils/LoadingText"; import Wait from "../../components/Utils/Wait"; @@ -29,7 +30,7 @@ const AccessesRequest: FC = ({ fileIdentifier }) => { const { t } = useTranslation({ AccessesRequest }); const { t: tCommon } = useTranslation("Common"); - const { user } = useAuthStore(); + const user = useAuthStore((state) => state.user); const [isSending, setIsSending] = useState(false); const [sendError, setSendError] = useState(undefined); diff --git a/assets/router/RouterRenderer.tsx b/assets/router/RouterRenderer.tsx index 8e3cdec7..5fd209aa 100644 --- a/assets/router/RouterRenderer.tsx +++ b/assets/router/RouterRenderer.tsx @@ -57,6 +57,8 @@ const PyramidVectorTmsServiceForm = lazy(() => import("../entrepot/pages/service const ServiceView = lazy(() => import("../entrepot/pages/service/view/ServiceView")); +const AccessesRequest = lazy(() => import("../entrepot/pages/AccessesRequest")); + const EspaceCoCommunityList = lazy(() => import("../espaceco/pages/communities/Communities")); const RouterRenderer: FC = () => { @@ -119,8 +121,8 @@ const RouterRenderer: FC = () => { return ; case "user_key_edit": return ; - // case "accesses_request": - // return ; + case "accesses_request": + return ; case "datastore_manage_storage": return ; case "datastore_manage_permissions": diff --git a/assets/router/router.ts b/assets/router/router.ts index b0063091..57280d63 100644 --- a/assets/router/router.ts +++ b/assets/router/router.ts @@ -65,13 +65,12 @@ const routeDefs = { (p) => `${appRoot}/communaute/${p.communityId}/membres` ), - // NOTE : désactivé car la variable d'environnement `geonetwork_url` a été supprimée, parce qu'en production la route `geonetwork/srv/api/records/$fileIdentifier/formatters/xml` n'est pas disponible - // accesses_request: defineRoute( - // { - // fileIdentifier: param.path.string, - // }, - // (p) => `${appRoot}/demande-acces/${p.fileIdentifier}` - // ), + accesses_request: defineRoute( + { + fileIdentifier: param.path.string, + }, + (p) => `${appRoot}/demande-acces/${p.fileIdentifier}` + ), datastore_create_request_confirm: defineRoute(`${appRoot}/demande-acces/demande-envoyee`), diff --git a/config/parameters.yaml b/config/parameters.yaml index 6ed7f0c1..01c92f8c 100644 --- a/config/parameters.yaml +++ b/config/parameters.yaml @@ -43,4 +43,3 @@ parameters: api_espaceco_url: "%env(resolve:API_ESPACE_COLLABORATIF_URL)%" catalogue_url: "%env(resolve:CATALOGUE_URL)%" - # geonetwork_url: "%env(resolve:GEONETWORK_URL)%" diff --git a/src/Controller/ContactController.php b/src/Controller/ContactController.php index d241c993..15d56311 100644 --- a/src/Controller/ContactController.php +++ b/src/Controller/ContactController.php @@ -16,13 +16,15 @@ #[Route( name: 'cartesgouvfr_contact_', + options: ['expose' => true], + condition: 'request.isXmlHttpRequest()', )] class ContactController extends AbstractController { public function __construct( private UserApiService $userApiService, private MailerService $mailerService, - private LoggerInterface $mailerLogger + private LoggerInterface $mailerLogger, ) { } @@ -215,9 +217,7 @@ public function joinCommunity(Request $request): JsonResponse #[Route( '/accesses_request', name: 'accesses_request', - options: ['expose' => true], methods: ['POST'], - condition: 'request.isXmlHttpRequest()' )] public function accessesRequest(Request $request): JsonResponse { @@ -237,12 +237,12 @@ public function accessesRequest(Request $request): JsonResponse 'layers' => $data['layers'], ]; if (isset($data['myself'])) { - $mailParams['myself'] = true; + $mailParams['myself'] = true; } if (isset($data['beneficiaries'])) { $mailParams['beneficiaries'] = $data['beneficiaries']; } - + $context = array_merge(['userEmail' => $userEmail], $mailParams); $this->mailerLogger->info('User ({userEmail}) : Demande d\'accès à des services de diffusion de données dont l\'accès est restreint', $context); diff --git a/src/Controller/Entrepot/GeonetworkController.php b/src/Controller/Entrepot/GeonetworkController.php index ba8d3e20..9ea2916c 100644 --- a/src/Controller/Entrepot/GeonetworkController.php +++ b/src/Controller/Entrepot/GeonetworkController.php @@ -8,11 +8,8 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; -/** - * // NOTE : ne pas utiliser car la variable d'environnement `geonetwork_url` a été supprimée, parce qu'en production la route `geonetwork/srv/api/records/$fileIdentifier/formatters/xml` n'est pas disponible. - */ #[Route( - '/geonetwork/metadata', + '/api/geonetwork/metadata', name: 'cartesgouvfr_geonetwork_metadata_', options: ['expose' => true], condition: 'request.isXmlHttpRequest()' @@ -25,19 +22,25 @@ public function __construct( ) { } - #[Route('/get_infos/{fileIdentifier}', name: 'get_infos', methods: ['GET'])] - public function getInfos(string $fileIdentifier): JsonResponse + #[Route('/{fileIdentifier}', name: 'get', methods: ['GET'])] + public function get(string $fileIdentifier): JsonResponse { + // https://data.geopf.fr/csw $xml = $this->geonetworkApiService->getMetadataXml($fileIdentifier); + + // supprime la balise csw:GetRecordByIdResponse + $xml = explode("\n", $xml); + $xml = array_filter($xml, fn ($l) => !str_contains($l, 'csw:GetRecordByIdResponse')); + $xml = implode("\n", $xml); + $cswMetadata = $this->cswMetadataHelper->fromXml($xml); - $privateLayers = []; - foreach ($cswMetadata->layers as $layer) { + $privateLayers = array_filter($cswMetadata->layers, function ($layer) { $parts = parse_url($layer->endpointUrl); - if (preg_match('/private/', $parts['path'])) { - $privateLayers[] = $layer; - } - } + + return preg_match('/private/', $parts['path']); + }); + $privateLayers = array_values($privateLayers); return new JsonResponse([ 'contact_email' => $cswMetadata->contactEmail, diff --git a/src/Services/GeonetworkApiService.php b/src/Services/GeonetworkApiService.php index caa8e30e..7375bcff 100644 --- a/src/Services/GeonetworkApiService.php +++ b/src/Services/GeonetworkApiService.php @@ -4,6 +4,7 @@ use App\Exception\CartesApiException; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\HttpClient\HttpOptions; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; @@ -18,18 +19,37 @@ public function __construct( ParameterBagInterface $parameterBag, HttpClientInterface $httpClient, ) { - $this->geonetworkClient = $httpClient->withOptions([ - // 'base_uri' => $parameterBag->get('geonetwork_url').'/', - 'proxy' => $parameterBag->get('http_proxy'), - 'verify_peer' => false, - 'verify_host' => false, - ]); + $parsedUrl = parse_url($parameterBag->get('api_entrepot_url')); + $baseUrl = sprintf('%s://%s', $parsedUrl['scheme'], $parsedUrl['host']); + + $this->geonetworkClient = $httpClient->withOptions( + (new HttpOptions()) + ->setBaseUri($baseUrl) + ->setProxy($parameterBag->get('http_proxy')) + ->verifyHost(false) + ->verifyPeer(false) + ->toArray() + ); } public function getMetadataXml(string $fileIdentifier): string { - $url = "geonetwork/srv/api/records/$fileIdentifier/formatters/xml"; - $response = $this->geonetworkClient->request('GET', $url); + $query = [ + 'REQUEST' => 'GetRecordById', + 'SERVICE' => 'CSW', + 'VERSION' => '2.0.2', + 'OUTPUTSCHEMA' => 'http://www.isotc211.org/2005/gmd', + 'elementSetName' => 'full', + 'ID' => $fileIdentifier, + ]; + + // $url = "geonetwork/srv/api/records/$fileIdentifier/formatters/xml"; + $url = '/csw'; + if (str_starts_with($fileIdentifier, 'sandbox')) { + $url = '/sandbox/csw'; + } + + $response = $this->geonetworkClient->request('GET', $url, ['query' => $query]); return $this->handleResponse($response); } @@ -37,7 +57,7 @@ public function getMetadataXml(string $fileIdentifier): string protected function handleResponse(ResponseInterface $response): mixed { $statusCode = $response->getStatusCode(); - if (200 == $statusCode) { // requête réussie + if (200 === $statusCode) { // requête réussie return $response->getContent(); }