From 774d7ab47b81d7c95f49e7c0769857000696feda Mon Sep 17 00:00:00 2001 From: hamou Date: Mon, 2 Dec 2024 15:31:49 +0100 Subject: [PATCH] Enrich arlas map services with arlas header --- package-lock.json | 4 +- package.json | 2 +- .../arlas-map/arlas-map.component.html | 1 + .../arlas-map/arlas-map.component.ts | 83 ++++++++++++++++++- src/package.json | 2 +- src/settings.yaml | 2 +- 6 files changed, 85 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 447c39f3..43ecce8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "arlas-wui", - "version": "26.1.0-rc.1", + "version": "26.1.1-tr", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "arlas-wui", - "version": "26.1.0-rc.1", + "version": "26.1.1-tr", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index 9e977b3c..fbc2bfb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arlas-wui", - "version": "26.1.0-rc.1", + "version": "26.1.1-tr", "author": "gisaia", "license": "Apache-2.0", "scripts": { diff --git a/src/app/components/arlas-map/arlas-map.component.html b/src/app/components/arlas-map/arlas-map.component.html index 61342704..eed58ba3 100644 --- a/src/app/components/arlas-map/arlas-map.component.html +++ b/src/app/components/arlas-map/arlas-map.component.html @@ -57,6 +57,7 @@ [margePanForLoad]="mapComponentConfig?.margePanForLoad" [margePanForTest]="mapComponentConfig?.margePanForTest" [initCenter]="mapComponentConfig?.initCenter" [drawData]="geojsondraw" + [transformRequest]="transformMapRequest" [initZoom]="mapComponentConfig?.initZoom" [minZoom]="mapComponentConfig?.minZoom" [maxZoom]="mapComponentConfig?.maxZoom" [displayScale]="mapComponentConfig?.displayScale" [displayCurrentCoordinates]="mapComponentConfig?.displayCurrentCoordinates" diff --git a/src/app/components/arlas-map/arlas-map.component.ts b/src/app/components/arlas-map/arlas-map.component.ts index 03441181..ecd040c3 100644 --- a/src/app/components/arlas-map/arlas-map.component.ts +++ b/src/app/components/arlas-map/arlas-map.component.ts @@ -35,8 +35,8 @@ import { import { ElementIdentifier, MapContributor } from 'arlas-web-contributors'; import { LegendData } from 'arlas-web-contributors/contributors/MapContributor'; import { - ArlasCollaborativesearchService, ArlasCollectionService, ArlasConfigService, ArlasMapService, - ArlasMapSettings, ArlasSettingsService, ArlasStartupService, getParamValue + ArlasCollaborativesearchService, ArlasCollectionService, ArlasConfigService, ArlasIamService, ArlasMapService, + ArlasMapSettings, ArlasSettingsService, ArlasStartupService, AuthentificationService, getParamValue } from 'arlas-wui-toolkit'; import * as mapboxgl from 'mapbox-gl'; import { BehaviorSubject, debounceTime, fromEvent, merge, mergeMap, Observable, of, Subject, takeUntil } from 'rxjs'; @@ -85,6 +85,9 @@ export class ArlasMapComponent implements OnInit { public mapRedrawSources; public mapLegendUpdater = new Subject>>(); public mapVisibilityUpdater; + + /** Map Url enricher */ + public transformMapRequest; /** Visibility status of layers on the map */ public layersVisibilityStatus: Map = new Map(); @@ -131,7 +134,9 @@ export class ArlasMapComponent implements OnInit { private snackbar: MatSnackBar, private iconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer, - private collectionService: ArlasCollectionService + private collectionService: ArlasCollectionService, + private authentService: AuthentificationService, + private arlasIamService: ArlasIamService ) { if (this.arlasStartupService.shouldRunApp && !this.arlasStartupService.emptyMode) { /** resize the map */ @@ -225,6 +230,9 @@ export class ArlasMapComponent implements OnInit { // eslint-disable-next-line max-len this.iconRegistry.addSvgIconLiteral('map_settings', this.domSanitizer.bypassSecurityTrustHtml('')); } + + this.updateMapTransformRequest(); + } public ngAfterViewInit() { @@ -246,6 +254,72 @@ export class ArlasMapComponent implements OnInit { } } + /** Updates the map url headers at each refresh.*/ + public updateMapTransformRequest() { + const authentMode = !!this.settingsService.getAuthentSettings() ? this.settingsService.getAuthentSettings().auth_mode : undefined; + const isAuthentActivated = !!this.settingsService.getAuthentSettings() && !!this.settingsService.getAuthentSettings().use_authent; + if (isAuthentActivated) { + if (authentMode === 'iam') { + this.arlasIamService.tokenRefreshed$.pipe(takeUntil(this._onDestroy$)).subscribe({ + next: (loginData) => { + if (!!loginData) { + const org = this.arlasIamService.getOrganisation(); + const iamHeader = { + Authorization: 'Bearer ' + loginData.access_token, + }; + // Set the org filter only if the organisation is defined + if (!!org) { + iamHeader['arlas-org-filter'] = org; + } + this.setMapTransformRequest(iamHeader); + } else { + this.setMapTransformRequest(); + } + } + }); + } else { + this.authentService.canActivateProtectedRoutes.pipe(takeUntil(this._onDestroy$)).subscribe(isConnected => { + if (isConnected) { + const headers = { + Authorization: 'Bearer ' + this.authentService.accessToken + }; + this.setMapTransformRequest(headers); + } else { + this.setMapTransformRequest(); + } + }); + } + } else { + this.setMapTransformRequest(); + } + } + + /** Enriches map url by an ARLAS header only if the map url is provided by ARLAS. */ + public setMapTransformRequest(headers?: any) { + this.transformMapRequest = (url, resourceType) => { + /** Wrapping with a try block because the URL() mdn docs says : 'Throws if the passed arguments don't define a valid URL.' */ + try { + const mapServiceUrl = new URL(url); + const appUrl = new URL(window.location.href); + const mapServiceOrigin = mapServiceUrl.origin; + const appOrigin = appUrl.origin; + console.log('enriching ...', appOrigin, mapServiceOrigin, headers) + /** We enrich map url by an ARLAS header only if the map url is provided by ARLAS. */ + if (appOrigin === mapServiceOrigin && !!headers) { + return ({ + url, + headers, + }); + } else { + return ({ url }); + } + } catch { + return ({ url }); + } + }; + } + + /** * Wait until the map component is loaded before fetching the data * @param isLoaded Whether the map has loaded @@ -253,6 +327,7 @@ export class ArlasMapComponent implements OnInit { public onMapLoaded(isLoaded: boolean): void { if (isLoaded && !this.arlasStartupService.emptyMode) { this.mapLoaded = true; + this.mapService.setMapComponent(this.mapglComponent); this.toolkitMapService.setMap(this.mapglComponent.map); this.visualizeService.setMap(this.mapglComponent.map); @@ -280,7 +355,7 @@ export class ArlasMapComponent implements OnInit { }); if (!!this.resultlistService.previewListContrib && this.resultlistService.previewListContrib.data.length > 0 && - this.mapComponentConfig.mapLayers.events.onHover.filter(l => this.mapglComponent.map.getLayer(l)).length > 0) { + this.mapComponentConfig.mapLayers.events.onHover.filter(l => this.mapglComponent.map.getLayer(l)).length > 0) { this.resultlistService.updateVisibleItems(); } } diff --git a/src/package.json b/src/package.json index ad5fee48..28b300a6 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "arlas-wui", - "version": "26.1.0-rc.1", + "version": "26.1.1-tr", "author": "gisaia", "license": "Apache-2.0" } diff --git a/src/settings.yaml b/src/settings.yaml index 1f27716e..77e9dddc 100644 --- a/src/settings.yaml +++ b/src/settings.yaml @@ -29,7 +29,7 @@ authentication: tab_name: "ARLAS Wui" persistence: - url: https://localhost/persist + url: https://cloud.arlas.io/arlas/persistence use_local_config: false histogram: max_buckets: 200