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} 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
    return this.http.get<User>(url + token).pipe(map(userInfo => {
18
      return this.parseUserInfo(userInfo);
19
    })).pipe(catchError(() => {
20
      return of(null);
21
    }));
22
  }
23

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

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