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