Project

General

Profile

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 60653 k.triantaf
import {COOKIE, Session, User} from "../login/utils/helper.class";
5 59838 argiro.kok
import {map} from "rxjs/operators";
6 57788 k.triantaf
import {NavigationEnd, Router} from "@angular/router";
7 58902 k.triantaf
import {properties} from "../../../environments/environment";
8 60653 k.triantaf
import {StringUtils} from "../utils/string-utils.class";
9 57059 k.triantaf
10
@Injectable({
11
  providedIn: 'root'
12
})
13 59816 argiro.kok
export class UserManagementService{
14 57885 k.triantaf
15 57788 k.triantaf
  private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
16 60654 k.triantaf
  public fixRedirectURL: string = null;
17 57939 k.triantaf
  private readonly promise: Promise<User>;
18 59816 argiro.kok
  sub;
19
  routeSub;
20 60653 k.triantaf
21 57788 k.triantaf
  constructor(private http: HttpClient,
22 58902 k.triantaf
              private router: Router) {
23 59041 k.triantaf
    this.promise = new Promise<any>((resolve => {
24 59250 k.triantaf
      this.updateUserInfo(resolve);
25 59041 k.triantaf
    }));
26 59816 argiro.kok
    this.routeSub = this.router.events.subscribe(event => {
27 57885 k.triantaf
      if (event instanceof NavigationEnd) {
28 57788 k.triantaf
        const token = COOKIE.getCookie('AccessToken');
29 57885 k.triantaf
        if (!token && this.getUserInfoSubject.getValue() !== null) {
30 57788 k.triantaf
          this.getUserInfoSubject.next(null);
31
        }
32
      }
33 59816 argiro.kok
    });
34 57059 k.triantaf
  }
35 59816 argiro.kok
36
  clearSubscriptions() {
37
    if(this.routeSub){
38
      this.routeSub.unsubscribe();
39
    }
40
    if(this.sub){
41
      this.sub.unsubscribe();
42
    }
43
  }
44 57885 k.triantaf
45 59041 k.triantaf
  public getUserInfo(subject: boolean = true): Observable<User> {
46 59245 k.triantaf
    if (subject) {
47 57939 k.triantaf
      return this.getUserInfoSubject.asObservable();
48 57788 k.triantaf
    } else {
49 57939 k.triantaf
      return from(this.getUserInfoAsync());
50 57788 k.triantaf
    }
51 57059 k.triantaf
  }
52 57885 k.triantaf
53 59228 k.triantaf
  public updateUserInfo(resolve: Function = null) {
54 59250 k.triantaf
    const token = COOKIE.getCookie('AccessToken');
55
    if (!token) {
56
      this.getUserInfoSubject.next(null);
57
      if(resolve) {
58 59228 k.triantaf
        resolve();
59
      }
60 59250 k.triantaf
    } else {
61 59816 argiro.kok
     this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
62 59250 k.triantaf
        return this.parseUserInfo(userInfo);
63
      })).subscribe(user => {
64
        this.getUserInfoSubject.next(user);
65
        if(resolve) {
66
          resolve();
67
        }
68 59965 k.triantaf
      }, error => {
69
       this.getUserInfoSubject.next(null);
70
       if(resolve) {
71
         resolve();
72
       }
73
     });
74 59250 k.triantaf
    }
75 59228 k.triantaf
  }
76
77 57939 k.triantaf
  private async getUserInfoAsync(): Promise<User> {
78
    await this.promise;
79 59816 argiro.kok
    if(this.sub){
80
      this.sub.unsubscribe();
81
    }
82 57939 k.triantaf
    return this.getUserInfoSubject.getValue();
83
  }
84
85 57059 k.triantaf
  private parseUserInfo(info: any) {
86
    const user: User = new User();
87 60284 k.triantaf
    user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
88 57059 k.triantaf
    user.firstname = (info.given_name) ? info.given_name : "";
89
    user.lastname = (info.family_name) ? info.family_name : "";
90 58624 k.triantaf
    user.email = info.email.toLowerCase(); // TODO remove, is a quick fix
91 57059 k.triantaf
    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 59250 k.triantaf
    user.role = [];
104
    if (info.roles) {
105
      info.roles.forEach(role => {
106
        user.role.push(role);
107
      });
108 59746 argiro.kok
    }else{
109
      if (info.edu_person_entitlements) {
110
        user.role = info.edu_person_entitlements;
111
      }
112 59250 k.triantaf
    }
113 59228 k.triantaf
    user.expirationDate = info.exp_date;
114 57059 k.triantaf
    return user;
115
  }
116 60653 k.triantaf
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 60654 k.triantaf
    if(this.fixRedirectURL) {
142
      this.setRedirectUrl(this.fixRedirectURL);
143
    } else {
144
      this.setRedirectUrl();
145
    }
146 60653 k.triantaf
    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 57059 k.triantaf
}