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 { TransferState, makeStateKey, StateKey } from '@angular/platform-browser';
import { isPlatformServer } from '@angular/common';
import {properties} from "../../environments/environment";
@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
private static HTTP_WHITELIST = [ properties.monitorServiceAPIURL + '/stakeholder' ];
constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) {}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//console.info("intercept transferstate");
if (request.method !== 'GET') {
if (request.method !== 'GET' || this.isService(request, HttpInterceptorService.HTTP_WHITELIST)) {
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 {
email: string;
firstname: string;
@ -15,8 +13,6 @@ export class User {
export class Session {
public static removeUser() {
COOKIE.deleteCookie(COOKIE.cookieName_id);
//COOKIE.deleteCookie("openAIRESession");
COOKIE.deleteCookie("openAIREUser");
}
public static isLoggedIn(): boolean {

View File

@ -1,25 +1,33 @@
import {Injectable} from '@angular/core';
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 {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment";
import {StringUtils} from "../utils/string-utils.class";
import {CustomOptions} from "./servicesUtils/customOptions.class";
import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
@Injectable({
providedIn: 'root'
})
export class UserManagementService {
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
private getUserInfoSubject: AdvancedAsyncSubject<User> | BehaviorSubject<User>;
public fixRedirectURL: string = null;
private redirectUrl: string = null;
private readonly promise: Promise<User>;
sub;
routeSub;
constructor(private http: HttpClient,
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.updateUserInfo(resolve);
}));
@ -58,7 +66,8 @@ export class UserManagementService{
resolve();
}
} 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);
})).subscribe(user => {
this.getUserInfoSubject.next(user);
@ -66,7 +75,9 @@ export class UserManagementService{
resolve();
}
}, error => {
if(this.getUserInfoSubject.getValue()) {
this.getUserInfoSubject.next(null);
}
if (resolve) {
resolve();
}
@ -132,7 +143,9 @@ export class UserManagementService{
if (!path.includes('/reload')) {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
}
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
} else {
this.redirectUrl = StringUtils.URIEncode(location.href);
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
}
}
@ -143,14 +156,14 @@ export class UserManagementService{
} else {
this.setRedirectUrl();
}
window.location.href = properties.loginUrl;
window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl;
}
public logout() {
this.setRedirectUrl();
Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) {
window.location.href = properties.logoutUrl;
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} else {
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);
}
}
}