Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): bust cache when user changed #543

Merged
merged 3 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/auth/src/lib/shared/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Md5 } from 'ts-md5';

import { ConfigService } from '@igo2/core';
import { TokenService } from './token.service';
Expand Down Expand Up @@ -42,9 +42,27 @@ export class AuthInterceptor implements HttpInterceptor {
}

const authHeader = `Bearer ${token}`;
const authReq = req.clone({
let authReq = req.clone({
headers: req.headers.set('Authorization', authHeader)
});

const tokenDecoded: any = this.tokenService.decode();
if (
authReq.params.get('_i') === 'true' &&
tokenDecoded &&
tokenDecoded.user &&
tokenDecoded.user.sourceId
) {
const hashUser = Md5.hashStr(tokenDecoded.user.sourceId) as string;
authReq = authReq.clone({
params: authReq.params.set('_i', hashUser)
});
} else if (authReq.params.get('_i') === 'true') {
authReq = authReq.clone({
params: authReq.params.delete('_i')
});
}

return next.handle(authReq);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/auth/src/lib/shared/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Router } from '@angular/router';

import { Observable, BehaviorSubject, of } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import { globalCacheBusterNotifier } from 'ngx-cacheable';

import { ConfigService, LanguageService, MessageService } from '@igo2/core';
import { Base64 } from '@igo2/utils';
Expand All @@ -28,6 +29,9 @@ export class AuthService {
@Optional() private router: Router
) {
this.authenticate$.next(this.authenticated);
this.authenticate$.subscribe(() => {
globalCacheBusterNotifier.next();
});
}

login(username: string, password: string): Observable<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class ContextService {
return this.http
.get<DetailedContext>(url)
.pipe(catchError(res => {
return this.handleError(res, id)
return this.handleError(res, id);
}));
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export class ContextService {

return this.http.post<ContextPermission[]>(url, JSON.stringify(association))
.pipe(catchError(res => {
return [this.handleError(res, undefined, true)]
return [this.handleError(res, undefined, true)];
}));
}

Expand Down
44 changes: 30 additions & 14 deletions packages/geo/src/lib/datasource/shared/capabilities.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ import {
ArcGISRestDataSourceOptions,
TileArcGISRestDataSourceOptions
} from './datasources';
import { LegendOptions, ItemStyleOptions } from '../../layer/shared/layers/layer.interface';
import { TimeFilterType, TimeFilterStyle } from '../../filter/shared/time-filter.enum';
import {
LegendOptions,
ItemStyleOptions
} from '../../layer/shared/layers/layer.interface';
import {
TimeFilterType,
TimeFilterStyle
} from '../../filter/shared/time-filter.enum';

@Injectable({
providedIn: 'root'
Expand All @@ -42,7 +48,9 @@ export class CapabilitiesService {

return this.getCapabilities('wms', url, version).pipe(
map((capabilities: any) => {
return capabilities ? this.parseWMSOptions(baseOptions, capabilities) : undefined;
return capabilities
? this.parseWMSOptions(baseOptions, capabilities)
: undefined;
})
);
}
Expand All @@ -55,7 +63,9 @@ export class CapabilitiesService {

const options = this.getCapabilities('wmts', url, version).pipe(
map((capabilities: any) => {
return capabilities ? this.parseWMTSOptions(baseOptions, capabilities) : undefined;
return capabilities
? this.parseWMTSOptions(baseOptions, capabilities)
: undefined;
})
);

Expand Down Expand Up @@ -117,7 +127,9 @@ export class CapabilitiesService {
);
}

@Cacheable()
@Cacheable({
maxCacheCount: 20
})
getCapabilities(
service: 'wms' | 'wmts',
baseUrl: string,
Expand All @@ -127,7 +139,8 @@ export class CapabilitiesService {
fromObject: {
request: 'GetCapabilities',
service,
version: version || '1.3.0'
version: version || '1.3.0',
_i: 'true'
}
});

Expand Down Expand Up @@ -173,8 +186,7 @@ export class CapabilitiesService {
title: layer.Title,
maxResolution:
getResolutionFromScale(layer.MaxScaleDenominator) || Infinity,
minResolution:
getResolutionFromScale(layer.MinScaleDenominator) || 0,
minResolution: getResolutionFromScale(layer.MinScaleDenominator) || 0,
metadata: {
url: metadata ? metadata.OnlineResource : undefined,
extern: metadata ? true : undefined,
Expand Down Expand Up @@ -350,18 +362,22 @@ export class CapabilitiesService {
}

getStyle(Style): LegendOptions {

const styleOptions: ItemStyleOptions[] = Style
.map((style) => {
const styleOptions: ItemStyleOptions[] = Style.map(style => {
return {
name: style.Name,
title: style.Title
};
})
// Handle repeat the style "default" in output (MapServer or OpenLayer)
.filter((item, index, self) => self.findIndex((i: ItemStyleOptions) => i.name === item.name) === index);
// Handle repeat the style "default" in output (MapServer or OpenLayer)
.filter(
(item, index, self) =>
self.findIndex((i: ItemStyleOptions) => i.name === item.name) ===
index
);

const legendOptions: LegendOptions = { stylesAvailable: styleOptions } as LegendOptions;
const legendOptions: LegendOptions = {
stylesAvailable: styleOptions
} as LegendOptions;

return legendOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { BehaviorSubject, Subscription } from 'rxjs';

import { EntityRecord, EntityStore, ToolComponent } from '@igo2/common';
import { AuthService } from '@igo2/auth';

import {
IgoMap,
Expand Down Expand Up @@ -51,6 +52,11 @@ export class CatalogBrowserToolComponent implements OnInit, OnDestroy {
*/
private catalog$$: Subscription;

/**
* Subscription for authentication
*/
private authenticate$$: Subscription;

/**
* Whether a group can be toggled when it's collapsed
*/
Expand All @@ -67,7 +73,8 @@ export class CatalogBrowserToolComponent implements OnInit, OnDestroy {
constructor(
private catalogService: CatalogService,
private catalogState: CatalogState,
private mapState: MapState
private mapState: MapState,
private authService: AuthService
) {}

/**
Expand All @@ -83,16 +90,20 @@ export class CatalogBrowserToolComponent implements OnInit, OnDestroy {
if (record && record.entity) {
const catalog = record.entity;
this.catalog = catalog;
this.loadCatalogItems(catalog);
}
});

this.authenticate$$ = this.authService.authenticate$.subscribe(() => {
this.loadCatalogItems(this.catalog);
});
}

/**
* @internal
*/
ngOnDestroy() {
this.catalog$$.unsubscribe();
this.authenticate$$.unsubscribe();
}

/**
Expand Down
14 changes: 13 additions & 1 deletion packages/integration/src/lib/catalog/catalog.state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';

import { AuthService } from '@igo2/auth';
import { EntityStore } from '@igo2/common';
import { Catalog, CatalogItem } from '@igo2/geo';

Expand All @@ -22,8 +23,12 @@ export class CatalogState {
*/
private catalogItemsStores = new Map<string, EntityStore<CatalogItem>>();

constructor() {
constructor(authService: AuthService) {
this._catalogStore = new EntityStore([]);

authService.authenticate$.subscribe(() => {
this.clearCatalogItemsStores();
});
}

/**
Expand All @@ -43,4 +48,11 @@ export class CatalogState {
setCatalogItemsStore(catalog: Catalog, store: EntityStore<CatalogItem>) {
this.catalogItemsStores.set(catalog.id as string, store);
}

/**
* Clear all catalog items stores
*/
clearCatalogItemsStores() {
this.catalogItemsStores.clear();
}
}
2 changes: 1 addition & 1 deletion proxy.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"changeOrigin": true
},
"/apis/": {
"target": "https://geoegl.msp.gouv.qc.ca",
"target": "https://testgeoegl.msp.gouv.qc.ca",
"secure": false,
"changeOrigin": true
},
Expand Down