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 {
|
2022-04-13 11:24:14 +02:00
|
|
|
private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<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;
|
2022-04-13 11:24:14 +02:00
|
|
|
private subscription;
|
2022-04-13 13:20:07 +02:00
|
|
|
private readonly routerSubscription;
|
2021-03-17 10:21:37 +01:00
|
|
|
|
2022-09-08 15:43:59 +02:00
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
this.updateUserInfo();
|
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() {
|
2022-04-13 11:24:14 +02:00
|
|
|
if (this.subscription) {
|
|
|
|
this.subscription.unsubscribe();
|
2020-11-11 15:43:13 +01:00
|
|
|
}
|
2022-04-13 13:20:07 +02:00
|
|
|
if (this.routerSubscription) {
|
|
|
|
this.routerSubscription.unsubscribe();
|
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|
2019-12-15 13:44:20 +01:00
|
|
|
|
2022-09-08 15:43:59 +02:00
|
|
|
public get user(): User {
|
|
|
|
return this.getUserInfoSubject.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getUserInfo(): Observable<User> {
|
|
|
|
return this.getUserInfoSubject.asObservable();
|
2019-12-23 16:09:17 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 13:50:08 +02:00
|
|
|
public updateUserInfo(resolve: Function = null) {
|
2022-04-13 11:24:14 +02:00
|
|
|
this.subscription = this.http.get<User>(properties.userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => {
|
|
|
|
return this.parseUserInfo(userInfo);
|
|
|
|
})).subscribe(user => {
|
|
|
|
this.getUserInfoSubject.next(user);
|
|
|
|
if (resolve) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, error => {
|
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();
|
|
|
|
}
|
2022-04-13 11:24:14 +02:00
|
|
|
});
|
2020-08-06 13:50:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:03:07 +02:00
|
|
|
private parseUserInfo(info: any) {
|
|
|
|
const user: User = new User();
|
2022-08-05 14:23:21 +02: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();
|
2022-09-08 15:43:59 +02:00
|
|
|
this.getUserInfoSubject.next(null);
|
2022-04-13 11:24:14 +02:00
|
|
|
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
|
2021-03-17 10:21:37 +01:00
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|