2019-09-13 09:03:07 +02:00
|
|
|
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";
|
2021-03-17 10:21:37 +01:00
|
|
|
import {COOKIE, Session, User} from "../login/utils/helper.class";
|
2020-11-13 15:52:59 +01:00
|
|
|
import {map} from "rxjs/operators";
|
2019-12-03 10:49:10 +01:00
|
|
|
import {NavigationEnd, Router} from "@angular/router";
|
2020-06-12 15:22:04 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
2021-03-17 10:21:37 +01:00
|
|
|
import {StringUtils} from "../utils/string-utils.class";
|
2021-11-05 12:29:54 +01:00
|
|
|
import {CustomOptions} from "./servicesUtils/customOptions.class";
|
2021-11-05 16:22:41 +01:00
|
|
|
import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
|
2019-09-13 09:03:07 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2021-11-05 12:29:54 +01:00
|
|
|
export class UserManagementService {
|
2019-12-15 13:44:20 +01:00
|
|
|
|
2021-11-08 10:36:28 +01:00
|
|
|
private getUserInfoSubject: AdvancedAsyncSubject<User> | BehaviorSubject<User>;
|
2021-03-17 10:58:12 +01:00
|
|
|
public fixRedirectURL: string = null;
|
2021-11-05 12:29:54 +01:00
|
|
|
private redirectUrl: string = null;
|
2019-12-23 16:09:17 +01:00
|
|
|
private readonly promise: Promise<User>;
|
2020-11-11 15:43:13 +01:00
|
|
|
sub;
|
|
|
|
routeSub;
|
2021-03-17 10:21:37 +01:00
|
|
|
|
2019-12-03 10:49:10 +01:00
|
|
|
constructor(private http: HttpClient,
|
2020-06-12 15:22:04 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-07-02 17:02:14 +02:00
|
|
|
this.promise = new Promise<any>((resolve => {
|
2020-08-11 14:54:14 +02:00
|
|
|
this.updateUserInfo(resolve);
|
2020-07-02 17:02:14 +02:00
|
|
|
}));
|
2020-11-11 15:43:13 +01:00
|
|
|
this.routeSub = this.router.events.subscribe(event => {
|
2019-12-15 13:44:20 +01:00
|
|
|
if (event instanceof NavigationEnd) {
|
2019-12-03 10:49:10 +01:00
|
|
|
const token = COOKIE.getCookie('AccessToken');
|
2019-12-15 13:44:20 +01:00
|
|
|
if (!token && this.getUserInfoSubject.getValue() !== null) {
|
2019-12-03 10:49:10 +01:00
|
|
|
this.getUserInfoSubject.next(null);
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
});
|
|
|
|
}
|
2021-11-05 12:29:54 +01:00
|
|
|
|
2020-11-11 15:43:13 +01:00
|
|
|
clearSubscriptions() {
|
2021-11-05 12:29:54 +01:00
|
|
|
if (this.routeSub) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.routeSub.unsubscribe();
|
|
|
|
}
|
2021-11-05 12:29:54 +01:00
|
|
|
if (this.sub) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|
2019-12-15 13:44:20 +01:00
|
|
|
|
2020-07-02 17:02:14 +02:00
|
|
|
public getUserInfo(subject: boolean = true): Observable<User> {
|
2020-08-11 14:16:50 +02:00
|
|
|
if (subject) {
|
2019-12-23 16:09:17 +01:00
|
|
|
return this.getUserInfoSubject.asObservable();
|
2019-12-03 10:49:10 +01:00
|
|
|
} else {
|
2019-12-23 16:09:17 +01:00
|
|
|
return from(this.getUserInfoAsync());
|
2019-12-03 10:49:10 +01:00
|
|
|
}
|
2019-12-23 16:09:17 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 13:50:08 +02:00
|
|
|
public updateUserInfo(resolve: Function = null) {
|
2020-08-11 14:54:14 +02:00
|
|
|
const token = COOKIE.getCookie('AccessToken');
|
2021-11-05 16:22:41 +01:00
|
|
|
if (!token) {
|
2020-08-11 14:54:14 +02:00
|
|
|
this.getUserInfoSubject.next(null);
|
2021-11-05 12:29:54 +01:00
|
|
|
if (resolve) {
|
2020-08-06 13:50:08 +02:00
|
|
|
resolve();
|
|
|
|
}
|
2020-08-11 14:54:14 +02:00
|
|
|
} else {
|
2021-11-05 12:29:54 +01:00
|
|
|
let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl);
|
|
|
|
this.sub = this.http.get<User>(userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => {
|
2020-08-11 14:54:14 +02:00
|
|
|
return this.parseUserInfo(userInfo);
|
|
|
|
})).subscribe(user => {
|
|
|
|
this.getUserInfoSubject.next(user);
|
2021-11-05 12:29:54 +01:00
|
|
|
if (resolve) {
|
2020-08-11 14:54:14 +02:00
|
|
|
resolve();
|
|
|
|
}
|
2020-11-25 17:45:41 +01:00
|
|
|
}, error => {
|
2021-11-05 16:22:41 +01:00
|
|
|
if(this.getUserInfoSubject.getValue()) {
|
2021-11-05 12:29:54 +01:00
|
|
|
this.getUserInfoSubject.next(null);
|
|
|
|
}
|
|
|
|
if (resolve) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2020-08-11 14:54:14 +02:00
|
|
|
}
|
2020-08-06 13:50:08 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 16:09:17 +01:00
|
|
|
private async getUserInfoAsync(): Promise<User> {
|
|
|
|
await this.promise;
|
2021-11-05 12:29:54 +01:00
|
|
|
if (this.sub) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
2019-12-23 16:09:17 +01:00
|
|
|
return this.getUserInfoSubject.getValue();
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|
2019-12-15 13:44:20 +01:00
|
|
|
|
2019-09-13 09:03:07 +02:00
|
|
|
private parseUserInfo(info: any) {
|
|
|
|
const user: User = new User();
|
2021-01-22 19:09:44 +01:00
|
|
|
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
|
2019-09-13 09:03:07 +02:00
|
|
|
user.firstname = (info.given_name) ? info.given_name : "";
|
|
|
|
user.lastname = (info.family_name) ? info.family_name : "";
|
2020-05-07 11:50:59 +02:00
|
|
|
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
|
2019-09-13 09:03:07 +02:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 14:54:14 +02:00
|
|
|
user.role = [];
|
|
|
|
if (info.roles) {
|
|
|
|
info.roles.forEach(role => {
|
|
|
|
user.role.push(role);
|
|
|
|
});
|
2021-11-05 12:29:54 +01:00
|
|
|
} else {
|
2020-11-03 10:38:11 +01:00
|
|
|
if (info.edu_person_entitlements) {
|
|
|
|
user.role = info.edu_person_entitlements;
|
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|
2020-08-06 13:50:08 +02:00
|
|
|
user.expirationDate = info.exp_date;
|
2019-09-13 09:03:07 +02:00
|
|
|
return user;
|
|
|
|
}
|
2021-03-17 10:21:37 +01:00
|
|
|
|
|
|
|
public setRedirectUrl(url: string = null) {
|
2021-11-05 12:29:54 +01:00
|
|
|
if (url) {
|
2021-03-17 10:21:37 +01:00
|
|
|
let parts = url.split('?');
|
|
|
|
let path = properties.baseLink + parts[0];
|
|
|
|
let params = null;
|
|
|
|
if (parts.length == 2) {
|
|
|
|
params = parts[1];
|
|
|
|
}
|
|
|
|
let hash = path.indexOf("#");
|
2021-11-05 12:29:54 +01:00
|
|
|
let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
|
|
|
|
if (fragment) {
|
2021-03-17 10:21:37 +01:00
|
|
|
path = path.slice(0, hash);
|
|
|
|
} else {
|
|
|
|
fragment = "";
|
|
|
|
}
|
2021-11-05 12:29:54 +01:00
|
|
|
if (!path.includes('/reload')) {
|
2021-03-17 10:21:37 +01:00
|
|
|
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
|
|
|
|
}
|
2021-11-05 12:29:54 +01:00
|
|
|
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
|
2021-03-17 10:21:37 +01:00
|
|
|
} else {
|
2021-11-05 12:29:54 +01:00
|
|
|
this.redirectUrl = StringUtils.URIEncode(location.href);
|
2021-03-17 10:21:37 +01:00
|
|
|
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public login() {
|
2021-11-05 12:29:54 +01:00
|
|
|
if (this.fixRedirectURL) {
|
2021-03-17 10:58:12 +01:00
|
|
|
this.setRedirectUrl(this.fixRedirectURL);
|
|
|
|
} else {
|
|
|
|
this.setRedirectUrl();
|
|
|
|
}
|
2021-11-05 12:29:54 +01:00
|
|
|
window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl;
|
2021-03-17 10:21:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public logout() {
|
|
|
|
this.setRedirectUrl();
|
|
|
|
Session.removeUser();
|
|
|
|
if (properties.logoutUrl.includes('openid_logout')) {
|
2021-11-05 12:29:54 +01:00
|
|
|
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
|
2021-03-17 10:21:37 +01:00
|
|
|
} else {
|
|
|
|
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|