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 {properties} from "../../../environments/environment";
|
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
|
sub;
|
17
|
routeSub;
|
18
|
constructor(private http: HttpClient,
|
19
|
private router: Router) {
|
20
|
this.promise = new Promise<any>((resolve => {
|
21
|
this.updateUserInfo(resolve);
|
22
|
}));
|
23
|
this.routeSub = this.router.events.subscribe(event => {
|
24
|
if (event instanceof NavigationEnd) {
|
25
|
const token = COOKIE.getCookie('AccessToken');
|
26
|
if (!token && this.getUserInfoSubject.getValue() !== null) {
|
27
|
this.getUserInfoSubject.next(null);
|
28
|
}
|
29
|
}
|
30
|
});
|
31
|
}
|
32
|
|
33
|
clearSubscriptions() {
|
34
|
if(this.routeSub){
|
35
|
this.routeSub.unsubscribe();
|
36
|
}
|
37
|
if(this.sub){
|
38
|
this.sub.unsubscribe();
|
39
|
}
|
40
|
}
|
41
|
|
42
|
public getUserInfo(subject: boolean = true): Observable<User> {
|
43
|
if (subject) {
|
44
|
return this.getUserInfoSubject.asObservable();
|
45
|
} else {
|
46
|
return from(this.getUserInfoAsync());
|
47
|
}
|
48
|
}
|
49
|
|
50
|
public updateUserInfo(resolve: Function = null) {
|
51
|
const token = COOKIE.getCookie('AccessToken');
|
52
|
if (!token) {
|
53
|
this.getUserInfoSubject.next(null);
|
54
|
if(resolve) {
|
55
|
resolve();
|
56
|
}
|
57
|
} else {
|
58
|
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
|
59
|
return this.parseUserInfo(userInfo);
|
60
|
})).pipe(timeout(3000), catchError(() => {
|
61
|
return of(null);
|
62
|
})).subscribe(user => {
|
63
|
this.getUserInfoSubject.next(user);
|
64
|
if(resolve) {
|
65
|
resolve();
|
66
|
}
|
67
|
});
|
68
|
}
|
69
|
}
|
70
|
|
71
|
private async getUserInfoAsync(): Promise<User> {
|
72
|
await this.promise;
|
73
|
if(this.sub){
|
74
|
this.sub.unsubscribe();
|
75
|
}
|
76
|
return this.getUserInfoSubject.getValue();
|
77
|
}
|
78
|
|
79
|
private parseUserInfo(info: any) {
|
80
|
const user: User = new User();
|
81
|
user.id = (info.subscriptions && info.subscriptions.indexOf('@')) ? info.subscriptions.substring(0, info.subscriptions.indexOf('@')) : info.subscriptions;
|
82
|
user.firstname = (info.given_name) ? info.given_name : "";
|
83
|
user.lastname = (info.family_name) ? info.family_name : "";
|
84
|
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
|
85
|
user.fullname = (info.name) ? info.name : "";
|
86
|
if (user.fullname == "") {
|
87
|
if (user.firstname != "") {
|
88
|
user.fullname += user.firstname;
|
89
|
}
|
90
|
if (user.lastname != "") {
|
91
|
user.fullname += user.lastname;
|
92
|
}
|
93
|
if (user.fullname == "") { //fullname is still empty set a default
|
94
|
user.fullname = "Anonymous user";
|
95
|
}
|
96
|
}
|
97
|
user.role = [];
|
98
|
if (info.roles) {
|
99
|
info.roles.forEach(role => {
|
100
|
user.role.push(role);
|
101
|
});
|
102
|
}else{
|
103
|
if (info.edu_person_entitlements) {
|
104
|
user.role = info.edu_person_entitlements;
|
105
|
}
|
106
|
}
|
107
|
user.expirationDate = info.exp_date;
|
108
|
return user;
|
109
|
}
|
110
|
}
|