Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from '@angular/common/http';
3
import {Observable, of} from "rxjs";
4
import {COOKIE, User} from "../login/utils/helper.class";
5
import {catchError, map, timeout} from "rxjs/operators";
6

    
7
@Injectable({
8
  providedIn: 'root'
9
})
10
export class UserManagementService {
11

    
12
  constructor(private http: HttpClient) {
13
  }
14

    
15
  public getUserInfo(url: string): Observable<User> {
16
    const token = COOKIE.getCookie('AccessToken');
17
    if(!token) {
18
      return of(null);
19
    } else return this.http.get<User>(url + token).pipe(map(userInfo => {
20
      return this.parseUserInfo(userInfo);
21
    })).pipe(timeout(2000), catchError(() => {
22
      return of(null);
23
    }));
24
  }
25

    
26
  private parseUserInfo(info: any) {
27
    const user: User = new User();
28
    user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
29

    
30
    user.firstname = (info.given_name) ? info.given_name : "";
31
    user.lastname = (info.family_name) ? info.family_name : "";
32
    user.email = info.email;
33
    user.fullname = (info.name) ? info.name : "";
34
    if (user.fullname == "") {
35
      if (user.firstname != "") {
36
        user.fullname += user.firstname;
37
      }
38
      if (user.lastname != "") {
39
        user.fullname += user.lastname;
40
      }
41
      if (user.fullname == "") { //fullname is still empty set a default
42
        user.fullname = "Anonymous user";
43
      }
44
    }
45
    if (info.edu_person_entitlements) {
46
      user.role = info.edu_person_entitlements;
47
    } else {
48
      user.role = [];
49
    }
50
    user.expirationDate = info.exp;
51
    return user;
52
  }
53
}
(18-18/19)