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>();
|
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 => {
|
2023-06-07 13:22:14 +02:00
|
|
|
return new User(userInfo);
|
2022-04-13 11:24:14 +02:00
|
|
|
})).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
|
|
|
}
|
2023-06-26 10:31:22 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 16:35:36 +01:00
|
|
|
public login(redirect: string = null) {
|
|
|
|
if(redirect) {
|
|
|
|
this.redirectUrl = redirect;
|
|
|
|
} else if (this.fixRedirectURL) {
|
2021-03-17 10:58:12 +01:00
|
|
|
this.setRedirectUrl(this.fixRedirectURL);
|
|
|
|
} else {
|
|
|
|
this.setRedirectUrl();
|
|
|
|
}
|
2023-12-18 16:35:36 +01:00
|
|
|
window.location.href = this.setURL(properties.loginUrl) + encodeURIComponent(this.redirectUrl);
|
2021-03-17 10:21:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public logout() {
|
|
|
|
this.setRedirectUrl();
|
|
|
|
Session.removeUser();
|
2023-12-18 16:35:36 +01:00
|
|
|
window.location.href = this.setURL(properties.logoutUrl) + encodeURIComponent(this.redirectUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|