2019-09-13 09:03:07 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2022-12-23 16:36:35 +01:00
|
|
|
import {Observable} from "rxjs";
|
|
|
|
import {Session, User} from "../login/utils/helper.class";
|
2020-11-13 15:52:59 +01:00
|
|
|
import {map} from "rxjs/operators";
|
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";
|
2023-12-18 16:35:36 +01:00
|
|
|
import {isArray} from "rxjs/internal-compatibility";
|
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>();
|
2024-01-07 00:41:59 +01:00
|
|
|
private static LOGIN = 'openid_connect_login';
|
|
|
|
private static LOGOUT: string = 'openid_logout';
|
|
|
|
private static USERINFO: string = 'userInfo';
|
2021-03-17 10:58:12 +01:00
|
|
|
public fixRedirectURL: string = null;
|
2024-01-09 09:07:04 +01:00
|
|
|
public allowDoubleRedirectToFixAndCurrentPage: boolean = false;
|
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
|
|
|
}
|
2024-02-13 10:57:46 +01:00
|
|
|
|
|
|
|
public static userInfoUrl(index = 0): string {
|
|
|
|
return (isArray(properties.loginServiceURL)?properties.loginServiceURL[index]:properties.loginServiceURL) + UserManagementService.USERINFO;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-01-07 00:41:59 +01:00
|
|
|
|
2024-01-09 09:51:03 +01:00
|
|
|
public getUserInfoAt(index = 0): Observable<User> {
|
2024-02-13 10:57:46 +01:00
|
|
|
return this.http.get<User>(UserManagementService.userInfoUrl(index), CustomOptions.registryOptions()).pipe(map(userInfo => {
|
2023-06-07 13:22:14 +02:00
|
|
|
return new User(userInfo);
|
2024-01-07 00:41:59 +01:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public updateUserInfo(resolve: Function = null) {
|
|
|
|
this.subscription = this.getUserInfoAt().subscribe(user => {
|
2022-04-13 11:24:14 +02:00
|
|
|
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
|
|
|
}
|
2023-06-26 10:31:22 +02:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
public setRedirectUrl(redirectTo: string | string [] = null) {
|
|
|
|
let urls = Array.isArray(redirectTo)?redirectTo:(redirectTo?[redirectTo]:[])
|
|
|
|
for(let url of urls) {
|
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);
|
2024-01-09 09:07:04 +01:00
|
|
|
}
|
|
|
|
if(urls.length == 0){
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 16:35:36 +01:00
|
|
|
public login(redirect: string = null) {
|
2024-01-12 10:26:16 +01:00
|
|
|
Session.clearReloadUrl();
|
2023-12-18 16:35:36 +01:00
|
|
|
if(redirect) {
|
|
|
|
this.redirectUrl = redirect;
|
|
|
|
} else if (this.fixRedirectURL) {
|
2024-01-09 09:07:04 +01:00
|
|
|
this.setRedirectUrl(this.allowDoubleRedirectToFixAndCurrentPage?[this.fixRedirectURL,location.href.split(location.host)[1]]:this.fixRedirectURL);
|
2021-03-17 10:58:12 +01:00
|
|
|
} else {
|
|
|
|
this.setRedirectUrl();
|
|
|
|
}
|
2024-01-07 00:41:59 +01:00
|
|
|
let loginURL: string | string[] = isArray(properties.loginServiceURL)?properties.loginServiceURL.map(url => url + UserManagementService.LOGIN):(properties.loginServiceURL + UserManagementService.LOGIN);
|
|
|
|
window.location.href = this.setURL(loginURL) + this.redirectUrl;
|
2021-03-17 10:21:37 +01:00
|
|
|
}
|
2024-01-09 09:07:04 +01:00
|
|
|
|
2021-03-17 10:21:37 +01:00
|
|
|
public logout() {
|
2024-01-12 10:26:16 +01:00
|
|
|
Session.clearReloadUrl();
|
2021-03-17 10:21:37 +01:00
|
|
|
this.setRedirectUrl();
|
|
|
|
Session.removeUser();
|
2024-01-07 00:41:59 +01:00
|
|
|
let logoutURL: string | string[] = isArray(properties.loginServiceURL)?properties.loginServiceURL.map(url => url + UserManagementService.LOGOUT):(properties.loginServiceURL + UserManagementService.LOGOUT);
|
|
|
|
window.location.href = this.setURL(logoutURL) + this.redirectUrl;
|
2023-12-18 16:35:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setURL(url: string | string[]) {
|
|
|
|
if(isArray(url)) {
|
|
|
|
let redirectURL = '';
|
|
|
|
url.forEach((url, index) => {
|
|
|
|
if(index === 0) {
|
|
|
|
redirectURL = url + "?redirect=";
|
|
|
|
} else {
|
|
|
|
redirectURL += encodeURIComponent(url + "?redirect=");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return redirectURL;
|
|
|
|
} else {
|
|
|
|
return url + "?redirect=";
|
|
|
|
}
|
2021-03-17 10:21:37 +01:00
|
|
|
}
|
2019-09-13 09:03:07 +02:00
|
|
|
}
|