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";
|
2019-11-08 12:01:50 +01:00
|
|
|
import {catchError, map, timeout} from "rxjs/operators";
|
2019-12-03 10:49:10 +01:00
|
|
|
import {NavigationEnd, Router} from "@angular/router";
|
2019-12-23 16:09:17 +01:00
|
|
|
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
2020-06-12 15:22:04 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
2019-09-13 09:03:07 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
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>;
|
2019-12-15 13:44:20 +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) {
|
2020-07-02 17:02:14 +02:00
|
|
|
this.promise = new Promise<any>((resolve => {
|
2019-12-23 16:09:17 +01:00
|
|
|
const token = COOKIE.getCookie('AccessToken');
|
|
|
|
if (!token) {
|
|
|
|
this.getUserInfoSubject.next(null);
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
|
|
|
|
return this.parseUserInfo(userInfo);
|
|
|
|
})).pipe(timeout(3000), catchError(() => {
|
|
|
|
return of(null);
|
|
|
|
})).subscribe(user => {
|
|
|
|
this.getUserInfoSubject.next(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
2020-07-02 17:02:14 +02:00
|
|
|
}));
|
2019-12-15 13:44:20 +01:00
|
|
|
this.router.events.subscribe(event => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
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> {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private async getUserInfoAsync(): Promise<User> {
|
|
|
|
await this.promise;
|
|
|
|
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();
|
|
|
|
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.edu_person_entitlements) {
|
|
|
|
user.role = info.edu_person_entitlements;
|
|
|
|
} else {
|
|
|
|
user.role = [];
|
|
|
|
}
|
|
|
|
user.expirationDate = info.exp;
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|