Merge branch 'master' of code-repo.d4science.org:MaDgIK/openaire-library

This commit is contained in:
Alex Martzios 2021-11-08 17:25:14 +02:00
commit 3632510e38
4 changed files with 104 additions and 29 deletions

View File

@ -4,15 +4,17 @@ import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators'; import { tap } from 'rxjs/operators';
import { TransferState, makeStateKey, StateKey } from '@angular/platform-browser'; import { TransferState, makeStateKey, StateKey } from '@angular/platform-browser';
import { isPlatformServer } from '@angular/common'; import { isPlatformServer } from '@angular/common';
import {properties} from "../../environments/environment";
@Injectable() @Injectable()
export class HttpInterceptorService implements HttpInterceptor { export class HttpInterceptorService implements HttpInterceptor {
private static HTTP_WHITELIST = [ properties.monitorServiceAPIURL + '/stakeholder' ];
constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) {} constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) {}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//console.info("intercept transferstate"); if (request.method !== 'GET' || this.isService(request, HttpInterceptorService.HTTP_WHITELIST)) {
if (request.method !== 'GET') {
return next.handle(request); return next.handle(request);
} }
@ -38,4 +40,18 @@ export class HttpInterceptorService implements HttpInterceptor {
} }
} }
} }
isService(req: HttpRequest<any>, service: string | string[]):boolean {
if(Array.isArray(service)) {
return !!service.find(element => req.url.indexOf(element) !== -1);
} else {
return req.url.indexOf(service) !== -1;
}
}
// public cleanRequest(requestUrl: string) {
// console.log("cleaned");
// const key: StateKey<string> = makeStateKey<string>(requestUrl);
// this.transferState.remove(key);
// }
} }

View File

@ -1,5 +1,3 @@
import {StringUtils} from "../../utils/string-utils.class";
export class User { export class User {
email: string; email: string;
firstname: string; firstname: string;
@ -15,8 +13,6 @@ export class User {
export class Session { export class Session {
public static removeUser() { public static removeUser() {
COOKIE.deleteCookie(COOKIE.cookieName_id); COOKIE.deleteCookie(COOKIE.cookieName_id);
//COOKIE.deleteCookie("openAIRESession");
COOKIE.deleteCookie("openAIREUser");
} }
public static isLoggedIn(): boolean { public static isLoggedIn(): boolean {

View File

@ -1,25 +1,33 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, from, Observable, of} from "rxjs"; import {BehaviorSubject, from, Observable} from "rxjs";
import {COOKIE, Session, User} from "../login/utils/helper.class"; import {COOKIE, Session, User} from "../login/utils/helper.class";
import {map} from "rxjs/operators"; import {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router"; import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment"; import {properties} from "../../../environments/environment";
import {StringUtils} from "../utils/string-utils.class"; import {StringUtils} from "../utils/string-utils.class";
import {CustomOptions} from "./servicesUtils/customOptions.class";
import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class UserManagementService{ export class UserManagementService {
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null); private getUserInfoSubject: AdvancedAsyncSubject<User> | BehaviorSubject<User>;
public fixRedirectURL: string = null; public fixRedirectURL: string = null;
private redirectUrl: string = null;
private readonly promise: Promise<User>; private readonly promise: Promise<User>;
sub; sub;
routeSub; routeSub;
constructor(private http: HttpClient, constructor(private http: HttpClient,
private router: Router) { private router: Router) {
if(properties.environment === "development") {
this.getUserInfoSubject = new AdvancedAsyncSubject<User>();
} else {
this.getUserInfoSubject = new BehaviorSubject<User>(null)
}
this.promise = new Promise<any>((resolve => { this.promise = new Promise<any>((resolve => {
this.updateUserInfo(resolve); this.updateUserInfo(resolve);
})); }));
@ -32,12 +40,12 @@ export class UserManagementService{
} }
}); });
} }
clearSubscriptions() { clearSubscriptions() {
if(this.routeSub){ if (this.routeSub) {
this.routeSub.unsubscribe(); this.routeSub.unsubscribe();
} }
if(this.sub){ if (this.sub) {
this.sub.unsubscribe(); this.sub.unsubscribe();
} }
} }
@ -54,29 +62,32 @@ export class UserManagementService{
const token = COOKIE.getCookie('AccessToken'); const token = COOKIE.getCookie('AccessToken');
if (!token) { if (!token) {
this.getUserInfoSubject.next(null); this.getUserInfoSubject.next(null);
if(resolve) { if (resolve) {
resolve(); resolve();
} }
} else { } else {
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => { let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl);
this.sub = this.http.get<User>(userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => {
return this.parseUserInfo(userInfo); return this.parseUserInfo(userInfo);
})).subscribe(user => { })).subscribe(user => {
this.getUserInfoSubject.next(user); this.getUserInfoSubject.next(user);
if(resolve) { if (resolve) {
resolve(); resolve();
} }
}, error => { }, error => {
this.getUserInfoSubject.next(null); if(this.getUserInfoSubject.getValue()) {
if(resolve) { this.getUserInfoSubject.next(null);
resolve(); }
} if (resolve) {
}); resolve();
}
});
} }
} }
private async getUserInfoAsync(): Promise<User> { private async getUserInfoAsync(): Promise<User> {
await this.promise; await this.promise;
if(this.sub){ if (this.sub) {
this.sub.unsubscribe(); this.sub.unsubscribe();
} }
return this.getUserInfoSubject.getValue(); return this.getUserInfoSubject.getValue();
@ -105,7 +116,7 @@ export class UserManagementService{
info.roles.forEach(role => { info.roles.forEach(role => {
user.role.push(role); user.role.push(role);
}); });
}else{ } else {
if (info.edu_person_entitlements) { if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements; user.role = info.edu_person_entitlements;
} }
@ -115,7 +126,7 @@ export class UserManagementService{
} }
public setRedirectUrl(url: string = null) { public setRedirectUrl(url: string = null) {
if(url) { if (url) {
let parts = url.split('?'); let parts = url.split('?');
let path = properties.baseLink + parts[0]; let path = properties.baseLink + parts[0];
let params = null; let params = null;
@ -123,34 +134,36 @@ export class UserManagementService{
params = parts[1]; params = parts[1];
} }
let hash = path.indexOf("#"); let hash = path.indexOf("#");
let fragment = (hash !== -1)?path.slice(hash + 1):null; let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
if(fragment) { if (fragment) {
path = path.slice(0, hash); path = path.slice(0, hash);
} else { } else {
fragment = ""; fragment = "";
} }
if(!path.includes('/reload')) { if (!path.includes('/reload')) {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment); Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
} }
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
} else { } else {
this.redirectUrl = StringUtils.URIEncode(location.href);
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash); Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
} }
} }
public login() { public login() {
if(this.fixRedirectURL) { if (this.fixRedirectURL) {
this.setRedirectUrl(this.fixRedirectURL); this.setRedirectUrl(this.fixRedirectURL);
} else { } else {
this.setRedirectUrl(); this.setRedirectUrl();
} }
window.location.href = properties.loginUrl; window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl;
} }
public logout() { public logout() {
this.setRedirectUrl(); this.setRedirectUrl();
Session.removeUser(); Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) { if (properties.logoutUrl.includes('openid_logout')) {
window.location.href = properties.logoutUrl; window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} else { } else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href); window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
} }

View File

@ -0,0 +1,50 @@
import {Subject, Subscriber, Subscription} from "rxjs";
export class AdvancedAsyncSubject<T> extends Subject<T>{
private _value: T = null;
private hasNext: boolean = false;
private hasFinished: boolean = false;
/** @deprecated This is an internal implementation detail, do not use. */
_subscribe(subscriber: Subscriber<any>): Subscription {
if (this.hasError) {
subscriber.error(this.thrownError);
return Subscription.EMPTY;
} else if (this.hasFinished && this.hasNext) {
subscriber.next(this._value);
}
return super._subscribe(subscriber);
}
next(value: T): void {
if (!this.hasFinished) {
this._value = value;
this.hasNext = true;
this.finish();
} else {
this._value = value;
super.next(value);
}
}
get value() {
return this.getValue();
}
getValue() {
return this._value;
}
error(error: any): void {
if (!this.hasFinished) {
super.error(error);
}
}
finish(): void {
this.hasFinished = true;
if (this.hasNext) {
super.next(this._value);
}
}
}