openaire-library/services/user-management.service.ts

172 lines
5.4 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
2021-11-08 10:36:28 +01:00
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 {
2021-11-08 10:36:28 +01:00
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) {
2021-11-08 10:36:28 +01:00
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);
}));
this.routeSub = this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
const token = COOKIE.getCookie('AccessToken');
if (!token && this.getUserInfoSubject.getValue() !== null) {
this.getUserInfoSubject.next(null);
}
}
});
}
clearSubscriptions() {
if (this.routeSub) {
this.routeSub.unsubscribe();
}
if (this.sub) {
this.sub.unsubscribe();
}
}
public getUserInfo(subject: boolean = true): Observable<User> {
if (subject) {
return this.getUserInfoSubject.asObservable();
} else {
return from(this.getUserInfoAsync());
}
}
public updateUserInfo(resolve: Function = null) {
const token = COOKIE.getCookie('AccessToken');
if (!token) {
this.getUserInfoSubject.next(null);
if (resolve) {
resolve();
}
} else {
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) {
resolve();
}
}, error => {
2022-01-20 10:18:32 +01:00
if(this.getUserInfoSubject.getValue() || this.getUserInfoSubject instanceof AdvancedAsyncSubject) {
this.getUserInfoSubject.next(null);
}
if (resolve) {
resolve();
}
});
}
}
private async getUserInfoAsync(): Promise<User> {
await this.promise;
if (this.sub) {
this.sub.unsubscribe();
}
return this.getUserInfoSubject.getValue();
}
private parseUserInfo(info: any) {
const user: User = new User();
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
user.firstname = (info.given_name) ? info.given_name : "";
user.lastname = (info.family_name) ? info.family_name : "";
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
user.fullname = (info.name) ? info.name : "";
if (user.fullname == "") {
if (user.firstname != "") {
user.fullname += user.firstname;
}
if (user.lastname != "") {
user.fullname += user.lastname;
}
if (user.fullname == "") { //fullname is still empty set a default
user.fullname = "Anonymous user";
}
}
user.role = [];
if (info.roles) {
info.roles.forEach(role => {
user.role.push(role);
});
} else {
if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements;
}
}
user.expirationDate = info.exp_date;
return user;
}
public setRedirectUrl(url: string = null) {
if (url) {
let parts = url.split('?');
let path = properties.baseLink + parts[0];
let params = null;
if (parts.length == 2) {
params = parts[1];
}
let hash = path.indexOf("#");
let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
if (fragment) {
path = path.slice(0, hash);
} else {
fragment = "";
}
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) {
this.setRedirectUrl(this.fixRedirectURL);
} else {
this.setRedirectUrl();
}
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 + "?redirect=" + this.redirectUrl;
} else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
}
}
}