1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient} from '@angular/common/http';
|
3
|
import {BehaviorSubject, from, Observable, of} from "rxjs";
|
4
|
import {COOKIE, User} from "../login/utils/helper.class";
|
5
|
import {catchError, map, timeout} from "rxjs/operators";
|
6
|
import {NavigationEnd, Router} from "@angular/router";
|
7
|
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
8
|
|
9
|
@Injectable({
|
10
|
providedIn: 'root'
|
11
|
})
|
12
|
export class UserManagementService {
|
13
|
|
14
|
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
15
|
private readonly promise: Promise<User>;
|
16
|
|
17
|
constructor(private http: HttpClient,
|
18
|
private router: Router,
|
19
|
private environmentSpecificService: EnvironmentSpecificService) {
|
20
|
this.promise = new Promise<any>((resolve => {
|
21
|
this.environmentSpecificService.subscribeEnvironment().subscribe(properties => {
|
22
|
const token = COOKIE.getCookie('AccessToken');
|
23
|
if (!token) {
|
24
|
this.getUserInfoSubject.next(null);
|
25
|
resolve();
|
26
|
} else {
|
27
|
this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
|
28
|
return this.parseUserInfo(userInfo);
|
29
|
})).pipe(timeout(3000), catchError(() => {
|
30
|
return of(null);
|
31
|
})).subscribe(user => {
|
32
|
this.getUserInfoSubject.next(user);
|
33
|
resolve();
|
34
|
});
|
35
|
}
|
36
|
});
|
37
|
}));
|
38
|
this.router.events.subscribe(event => {
|
39
|
if (event instanceof NavigationEnd) {
|
40
|
const token = COOKIE.getCookie('AccessToken');
|
41
|
if (!token && this.getUserInfoSubject.getValue() !== null) {
|
42
|
this.getUserInfoSubject.next(null);
|
43
|
}
|
44
|
}
|
45
|
})
|
46
|
}
|
47
|
|
48
|
public getUserInfo(subject: boolean = true): Observable<User> {
|
49
|
if(subject) {
|
50
|
return this.getUserInfoSubject.asObservable();
|
51
|
} else {
|
52
|
return from(this.getUserInfoAsync());
|
53
|
}
|
54
|
}
|
55
|
|
56
|
private async getUserInfoAsync(): Promise<User> {
|
57
|
await this.promise;
|
58
|
return this.getUserInfoSubject.getValue();
|
59
|
}
|
60
|
|
61
|
private parseUserInfo(info: any) {
|
62
|
const user: User = new User();
|
63
|
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
|
64
|
user.firstname = (info.given_name) ? info.given_name : "";
|
65
|
user.lastname = (info.family_name) ? info.family_name : "";
|
66
|
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
|
67
|
user.fullname = (info.name) ? info.name : "";
|
68
|
if (user.fullname == "") {
|
69
|
if (user.firstname != "") {
|
70
|
user.fullname += user.firstname;
|
71
|
}
|
72
|
if (user.lastname != "") {
|
73
|
user.fullname += user.lastname;
|
74
|
}
|
75
|
if (user.fullname == "") { //fullname is still empty set a default
|
76
|
user.fullname = "Anonymous user";
|
77
|
}
|
78
|
}
|
79
|
if (info.edu_person_entitlements) {
|
80
|
user.role = info.edu_person_entitlements;
|
81
|
} else {
|
82
|
user.role = [];
|
83
|
}
|
84
|
user.expirationDate = info.exp;
|
85
|
return user;
|
86
|
}
|
87
|
}
|