diff --git a/src/app/components/arlas-map/arlas-map.component.html b/src/app/components/arlas-map/arlas-map.component.html index 9a73079b..510701fe 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 794912e8..9ece2792 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'; @@ -82,6 +82,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(); @@ -128,7 +131,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 */ @@ -222,6 +227,9 @@ export class ArlasMapComponent implements OnInit { // eslint-disable-next-line max-len this.iconRegistry.addSvgIconLiteral('map_settings', this.domSanitizer.bypassSecurityTrustHtml('')); } + + this.updateMapTransformRequest(); + } public ngAfterViewInit() { @@ -243,6 +251,71 @@ 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; + /** 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 @@ -250,6 +323,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); @@ -277,7 +351,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(); } }