2019-09-13 09:03:07 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2019-12-23 16:09:17 +01:00
|
|
|
import {BehaviorSubject, from, Observable, of} from "rxjs";
|
2019-09-13 09:03:07 +02:00
|
|
|
import {COOKIE, 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";
|
2019-09-13 09:03:07 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2020-11-11 15:43:13 +01:00
|
|
|
export class UserManagementService{
|
2019-12-15 13:44:20 +01:00
|
|
|
|
2019-12-03 10:49:10 +01:00
|
|
|
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
2019-12-23 16:09:17 +01:00
|
|
|
private readonly promise: Promise<User>;
|
2020-11-11 15:43:13 +01:00
|
|
|
sub;
|
|
|
|
routeSub;
|
2019-12-03 10:49:10 +01:00
|
|
|
constructor(private http: HttpClient,
|
2020-06-12 15:22:04 +02:00
|
|
|
private router: Router) {
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSubscriptions() {
|
|
|
|
if(this.routeSub){
|
|
|
|
this.routeSub.unsubscribe();
|
|
|
|
}
|
|
|
|
if(this.sub){
|
|
|
|
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');
|
|
|
|
if (!token) {
|
|
|
|
this.getUserInfoSubject.next(null);
|
|
|
|
if(resolve) {
|
2020-08-06 13:50:08 +02:00
|
|
|
resolve();
|
|
|
|
}
|
2020-08-11 14:54:14 +02:00
|
|
|
} else {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
|
2020-08-11 14:54:14 +02:00
|
|
|
return this.parseUserInfo(userInfo);
|
|
|
|
})).subscribe(user => {
|
|
|
|
this.getUserInfoSubject.next(user);
|
|
|
|
if(resolve) {
|
|
|
|
resolve();
|
|
|
|
}
|
2020-11-25 17:45:41 +01:00
|
|
|
}, error => {
|
|
|
|
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;
|
2020-11-11 15:43:13 +01:00
|
|
|
if(this.sub){
|
|
|
|
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();
|
2020-11-11 15:43:13 +01:00
|
|
|
user.id = (info.subscriptions && info.subscriptions.indexOf('@')) ? info.subscriptions.substring(0, info.subscriptions.indexOf('@')) : info.subscriptions;
|
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);
|
|
|
|
});
|
2020-11-03 10:38:11 +01:00
|
|
|
}else{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|