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