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

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

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