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{
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);
}));
@ -34,10 +42,10 @@ export class UserManagementService{
}
clearSubscriptions() {
if(this.routeSub){
if (this.routeSub) {
this.routeSub.unsubscribe();
}
if(this.sub){
if (this.sub) {
this.sub.unsubscribe();
}
}
@ -54,20 +62,23 @@ export class UserManagementService{
const token = COOKIE.getCookie('AccessToken');
if (!token) {
this.getUserInfoSubject.next(null);
if(resolve) {
if (resolve) {
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);
if(resolve) {
if (resolve) {
resolve();
}
}, error => {
if(this.getUserInfoSubject.getValue()) {
this.getUserInfoSubject.next(null);
if(resolve) {
}
if (resolve) {
resolve();
}
});
@ -76,7 +87,7 @@ export class UserManagementService{
private async getUserInfoAsync(): Promise<User> {
await this.promise;
if(this.sub){
if (this.sub) {
this.sub.unsubscribe();
}
return this.getUserInfoSubject.getValue();
@ -105,7 +116,7 @@ export class UserManagementService{
info.roles.forEach(role => {
user.role.push(role);
});
}else{
} else {
if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements;
}
@ -115,7 +126,7 @@ export class UserManagementService{
}
public setRedirectUrl(url: string = null) {
if(url) {
if (url) {
let parts = url.split('?');
let path = properties.baseLink + parts[0];
let params = null;
@ -123,34 +134,36 @@ export class UserManagementService{
params = parts[1];
}
let hash = path.indexOf("#");
let fragment = (hash !== -1)?path.slice(hash + 1):null;
if(fragment) {
let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
if (fragment) {
path = path.slice(0, hash);
} else {
fragment = "";
}
if(!path.includes('/reload')) {
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);
}
}
public login() {
if(this.fixRedirectURL) {
if (this.fixRedirectURL) {
this.setRedirectUrl(this.fixRedirectURL);
} 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);
}
}
}