1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient} from '@angular/common/http';
|
3
|
import {BehaviorSubject, from, Observable, of} from "rxjs";
|
4
|
import {COOKIE, Session, User} from "../login/utils/helper.class";
|
5
|
import {map} from "rxjs/operators";
|
6
|
import {NavigationEnd, Router} from "@angular/router";
|
7
|
import {properties} from "../../../environments/environment";
|
8
|
import {el} from "@angular/platform-browser/testing/src/browser_util";
|
9
|
import {StringUtils} from "../utils/string-utils.class";
|
10
|
|
11
|
@Injectable({
|
12
|
providedIn: 'root'
|
13
|
})
|
14
|
export class UserManagementService{
|
15
|
|
16
|
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
17
|
public fixRedirectURL: string = null;
|
18
|
private readonly promise: Promise<User>;
|
19
|
sub;
|
20
|
routeSub;
|
21
|
|
22
|
constructor(private http: HttpClient,
|
23
|
private router: Router) {
|
24
|
this.promise = new Promise<any>((resolve => {
|
25
|
this.updateUserInfo(resolve);
|
26
|
}));
|
27
|
this.routeSub = this.router.events.subscribe(event => {
|
28
|
if (event instanceof NavigationEnd) {
|
29
|
const token = COOKIE.getCookie('AccessToken');
|
30
|
if (!token && this.getUserInfoSubject.getValue() !== null) {
|
31
|
this.getUserInfoSubject.next(null);
|
32
|
}
|
33
|
}
|
34
|
});
|
35
|
}
|
36
|
|
37
|
clearSubscriptions() {
|
38
|
if(this.routeSub){
|
39
|
this.routeSub.unsubscribe();
|
40
|
}
|
41
|
if(this.sub){
|
42
|
this.sub.unsubscribe();
|
43
|
}
|
44
|
}
|
45
|
|
46
|
public getUserInfo(subject: boolean = true): Observable<User> {
|
47
|
if (subject) {
|
48
|
return this.getUserInfoSubject.asObservable();
|
49
|
} else {
|
50
|
return from(this.getUserInfoAsync());
|
51
|
}
|
52
|
}
|
53
|
|
54
|
public updateUserInfo(resolve: Function = null) {
|
55
|
const token = COOKIE.getCookie('AccessToken');
|
56
|
if (!token) {
|
57
|
this.getUserInfoSubject.next(null);
|
58
|
if(resolve) {
|
59
|
resolve();
|
60
|
}
|
61
|
} else {
|
62
|
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
|
63
|
return this.parseUserInfo(userInfo);
|
64
|
})).subscribe(user => {
|
65
|
this.getUserInfoSubject.next(user);
|
66
|
if(resolve) {
|
67
|
resolve();
|
68
|
}
|
69
|
}, error => {
|
70
|
this.getUserInfoSubject.next(null);
|
71
|
if(resolve) {
|
72
|
resolve();
|
73
|
}
|
74
|
});
|
75
|
}
|
76
|
}
|
77
|
|
78
|
private async getUserInfoAsync(): Promise<User> {
|
79
|
await this.promise;
|
80
|
if(this.sub){
|
81
|
this.sub.unsubscribe();
|
82
|
}
|
83
|
return this.getUserInfoSubject.getValue();
|
84
|
}
|
85
|
|
86
|
private parseUserInfo(info: any) {
|
87
|
const user: User = new User();
|
88
|
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
|
89
|
user.firstname = (info.given_name) ? info.given_name : "";
|
90
|
user.lastname = (info.family_name) ? info.family_name : "";
|
91
|
user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
|
92
|
user.fullname = (info.name) ? info.name : "";
|
93
|
if (user.fullname == "") {
|
94
|
if (user.firstname != "") {
|
95
|
user.fullname += user.firstname;
|
96
|
}
|
97
|
if (user.lastname != "") {
|
98
|
user.fullname += user.lastname;
|
99
|
}
|
100
|
if (user.fullname == "") { //fullname is still empty set a default
|
101
|
user.fullname = "Anonymous user";
|
102
|
}
|
103
|
}
|
104
|
user.role = [];
|
105
|
if (info.roles) {
|
106
|
info.roles.forEach(role => {
|
107
|
user.role.push(role);
|
108
|
});
|
109
|
}else{
|
110
|
if (info.edu_person_entitlements) {
|
111
|
user.role = info.edu_person_entitlements;
|
112
|
}
|
113
|
}
|
114
|
user.expirationDate = info.exp_date;
|
115
|
return user;
|
116
|
}
|
117
|
|
118
|
public setRedirectUrl(url: string = null) {
|
119
|
if(url) {
|
120
|
let parts = url.split('?');
|
121
|
let path = properties.baseLink + parts[0];
|
122
|
let params = null;
|
123
|
if (parts.length == 2) {
|
124
|
params = parts[1];
|
125
|
}
|
126
|
let hash = path.indexOf("#");
|
127
|
let fragment = (hash !== -1)?path.slice(hash + 1):null;
|
128
|
if(fragment) {
|
129
|
path = path.slice(0, hash);
|
130
|
} else {
|
131
|
fragment = "";
|
132
|
}
|
133
|
if(!path.includes('/reload')) {
|
134
|
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
|
135
|
}
|
136
|
} else {
|
137
|
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
public login() {
|
142
|
if(this.fixRedirectURL) {
|
143
|
this.setRedirectUrl(this.fixRedirectURL);
|
144
|
} else {
|
145
|
this.setRedirectUrl();
|
146
|
}
|
147
|
window.location.href = properties.loginUrl;
|
148
|
}
|
149
|
|
150
|
public logout() {
|
151
|
this.setRedirectUrl();
|
152
|
Session.removeUser();
|
153
|
if (properties.logoutUrl.includes('openid_logout')) {
|
154
|
window.location.href = properties.logoutUrl;
|
155
|
} else {
|
156
|
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
|
157
|
}
|
158
|
}
|
159
|
}
|