Project

General

Profile

1
import {StringUtils} from "../../utils/string-utils.class";
2

    
3
export class User {
4
  email: string;
5
  firstname: string;
6
  lastname: string;
7
  id: string;
8
  fullname: string;
9
  expirationDate: number;
10
  role: string[];
11
  jwt: string;
12
  
13
}
14

    
15
export class Session {
16
  public static removeUser() {
17
    COOKIE.deleteCookie(COOKIE.cookieName_id);
18
    //COOKIE.deleteCookie("openAIRESession");
19
    COOKIE.deleteCookie("openAIREUser");
20
  }
21
  
22
  public static isLoggedIn(): boolean {
23
    var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
24
    return (cookie != null && cookie != "");
25
  }
26
  
27
  public static setReloadUrl(host: string, path: string, params: string) {
28
    var URL = {};
29
    URL["host"] = host;
30
    URL["path"] = path;
31
    URL["params"] = params;
32
    COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
33
  }
34
  
35
  public static getReloadUrl(plainText: boolean = false) {
36
    var URL = COOKIE.getCookie("reloadURL");
37
    URL = JSON.parse(URL);
38
    return URL;
39
    
40
  }
41
  
42
  public static getParamsObj(params: string) {
43
    var object = null;
44
    if (params.split("&").length > 0) {
45
      object = {};
46
    }
47
    params = (params && params.split("?").length > 1) ? params.split("?")[1] : params;
48
    for (var i = 0; i < params.split("&").length; i++) {
49
      object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
50
    }
51
    
52
    return object;
53
    
54
  }
55
  
56
  //Methods to check roles
57
  public static isClaimsCurator(user: User): boolean {
58
    return user && user.role.indexOf(Role.CURATOR_CLAIM) !== -1;
59
  }
60
  
61
  public static isMonitorCurator(user: User): boolean {
62
    return this.isCommunityCurator(user) || this.isProjectCurator(user) || this.isFunderCurator(user) || this.isOrganizationCurator(user);
63
  }
64
  
65
  public static isCommunityCurator(user: User): boolean {
66
    return this.isTypeCurator("Community", user);
67
  }
68
  
69
  public static isFunderCurator(user: User): boolean {
70
    return this.isTypeCurator("Funder", user);
71
  }
72
  
73
  public static isProjectCurator(user: User): boolean {
74
    return this.isTypeCurator("Project", user);
75
  }
76
  
77
  public static isOrganizationCurator(user: User): boolean {
78
    return this.isTypeCurator("Institution", user);
79
  }
80
  
81
  private static isTypeCurator(type: string, user: User): boolean {
82
    return user && user.role.indexOf(Role.curator(type)) !== -1;
83
  }
84
  
85
  public static isCurator(type: string, user: User): boolean {
86
    if (type == 'funder') {
87
      return user && this.isFunderCurator(user);
88
    } else if (type == 'ri' || type == 'community') {
89
      return user && this.isCommunityCurator(user);
90
    } else if (type == 'organization' || type == 'institution') {
91
      return user && this.isOrganizationCurator(user);
92
    } else if (type == 'project') {
93
      return user && this.isProjectCurator(user);
94
    }
95
  }
96
  
97
  public static isPortalAdministrator(user: User): boolean {
98
    return user && user.role.indexOf(Role.PORTAL_ADMIN) !== -1;
99
  }
100
  
101
  public static isUserManager(user: User): boolean {
102
    return user && user.role.indexOf(Role.USER_MANAGER) !== -1;
103
  }
104
  
105
  public static isSubscribedTo(type: string, id: string, user: User): boolean {
106
    return user && user.role.indexOf(Role.member(type, id)) !== -1;
107
  }
108
  
109
  public static isMember(type: string, id: string, user: User): boolean {
110
    return user && user.role.indexOf(Role.member(type, id)) !== -1;
111
  }
112
  
113
  public static isManager(type: string, id: string, user: User): boolean {
114
    return user && user.role.indexOf(Role.manager(type, id)) !== -1
115
  }
116
  
117
  public static isKindOfMonitorManager(user: User): boolean {
118
    if (user) {
119
      for (let role of user.role) {
120
        if (role.indexOf('_MANAGER') !== -1) {
121
          return true;
122
        }
123
      }
124
    }
125
    return false;
126
  }
127
  
128
  public static isKindOfCommunityManager(user: User): boolean {
129
    if (user) {
130
      for (let role of user.role) {
131
        if (role.indexOf('COMMUNITY') !== -1 && role.indexOf('_MANAGER') !== -1) {
132
          return true;
133
        }
134
      }
135
    }
136
    return false;
137
  }
138
  
139
  public static isRegisteredUser(user: User): boolean {
140
    return user && user.role.indexOf(Role.REGISTERED_USER) !== -1;
141
  }
142
}
143

    
144
export class COOKIE {
145
  public static cookieName_id: string = "AccessToken";
146
  
147
  public static getCookie(name: string): string {
148
    if (typeof document == 'undefined') {
149
      return null;
150
    }
151
    let ca: Array<string> = document.cookie.split(';');
152
    let caLen: number = ca.length;
153
    let cookieName = `${name}=`;
154
    let c: string;
155
    
156
    for (let i: number = 0; i < caLen; i += 1) {
157
      c = ca[i].replace(/^\s+/g, '');
158
      if (c.indexOf(cookieName) == 0) {
159
        return c.substring(cookieName.length, c.length);
160
      }
161
    }
162
    return null;
163
  }
164
  
165
  public static deleteCookie(name) {
166
    this.setCookie(name, '', -1);
167
  }
168
  
169
  public static setCookie(name: string, value: string, expireDays: number, path: string = '/') {
170
    //TODO fix domain?
171
    let d: Date = new Date();
172
    d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
173
    let expires: string = `expires=${d.toUTCString()}`;
174
    // let cpath:string = path ? `; path=${path}` : '';
175
    let domain = "";
176
    if (typeof document !== 'undefined') {
177
      if (document.domain.indexOf(".di.uoa.gr") != -1) { // for development
178
        domain = ".di.uoa.gr";
179
      } else if (document.domain.indexOf(".openaire.eu") != -1) {
180
        domain = ".openaire.eu";
181
      }
182
      document.cookie = name + '=' + value + '; path=' + path + '; domain=' + domain + ';SameSite=Lax;';
183
    }
184
  }
185
}
186

    
187
export class Role {
188
  public static PORTAL_ADMIN = 'PORTAL_ADMINISTRATOR';
189
  public static REGISTERED_USER = 'REGISTERED_USER';
190
  public static ANONYMOUS_USER = 'ROLE_ANONYMOUS';
191
  public static USER_MANAGER = 'USER_MANAGER';
192
  public static CURATOR_CLAIM = 'CURATOR_CLAIM';
193
  
194
  
195
  public static mapType(type: string, communityMap: boolean = true): string {
196
    if (type == "ri" && communityMap) {
197
      type = "community";
198
    } else if (type == "organization") {
199
      type = "institution";
200
    }
201
    return type;
202
  }
203
  
204
  /**
205
   * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
206
   *
207
   * */
208
  public static curator(type: string): string {
209
    return "CURATOR_" + this.mapType(type, true).toUpperCase();
210
  }
211
  
212
  /**
213
   * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
214
   *
215
   * Id = EE, EGI, etc
216
   * */
217
  public static manager(type: string, id: string) {
218
    return this.mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
219
  }
220
  
221
  /**
222
   * Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
223
   *
224
   * Id = EE, EGI, etc
225
   * */
226
  public static member(type: string, id: string) {
227
    return this.mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
228
  }
229
}
(2-2/2)