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 setUser(user:User): User {
|
17
|
//
|
18
|
// localStorage.setItem("user", JSON.stringify(user));
|
19
|
//
|
20
|
// return user;
|
21
|
// }
|
22
|
public static removeUser() {
|
23
|
if(Session.isLoggedIn()){
|
24
|
localStorage.removeItem("user");
|
25
|
COOKIE.deleteCookie(COOKIE.cookieName_id)
|
26
|
COOKIE.deleteCookie(COOKIE.cookieName_u);
|
27
|
}
|
28
|
}
|
29
|
public static getUser():User {
|
30
|
if(Session.isLoggedIn()){
|
31
|
return JSON.parse(localStorage.getItem("user"));
|
32
|
}else{
|
33
|
return null;
|
34
|
}
|
35
|
}
|
36
|
public static isLoggedIn(): boolean {
|
37
|
var loggedIn:boolean = false;
|
38
|
var user:User = null;
|
39
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
40
|
if( typeof localStorage !== 'undefined') {
|
41
|
if(localStorage.getItem("user") || (cookie != null && cookie != "")) {
|
42
|
user = JSON.parse(localStorage.getItem("user"));
|
43
|
if(user && (user.fullname != null || user.lastname !=null || user.firstname !=null)){
|
44
|
loggedIn = true;
|
45
|
}else if(cookie != null && this.getUserFromCookie()!= null){
|
46
|
loggedIn = true
|
47
|
}else{
|
48
|
loggedIn = false;
|
49
|
}
|
50
|
}else{
|
51
|
if(cookie != null && this.getUserFromCookie()!= null){
|
52
|
loggedIn = true
|
53
|
}else{
|
54
|
loggedIn = false;
|
55
|
}
|
56
|
|
57
|
}
|
58
|
}else{
|
59
|
loggedIn = false;
|
60
|
}
|
61
|
return loggedIn;
|
62
|
}
|
63
|
public static getUserFromCookie():User{
|
64
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
65
|
if(cookie != null){
|
66
|
var user:User = MyJWT.parseUserInfo(cookie);
|
67
|
if( typeof localStorage !== 'undefined') {
|
68
|
localStorage.setItem("user", JSON.stringify(user));
|
69
|
if(user && user.email){
|
70
|
// COOKIE.deleteCookie(COOKIE.cookieName_u); // delete cookie to avoid transfer through requests
|
71
|
return user;
|
72
|
}else{
|
73
|
return null;
|
74
|
}
|
75
|
}else{
|
76
|
return null;
|
77
|
}
|
78
|
}else{
|
79
|
return null;
|
80
|
}
|
81
|
}
|
82
|
public static getUserJwt():string {
|
83
|
if(Session.isLoggedIn()){
|
84
|
return Session.getUser().jwt;
|
85
|
}else{
|
86
|
return null;
|
87
|
}
|
88
|
|
89
|
}
|
90
|
public static getUserEmail():string {
|
91
|
if(Session.isLoggedIn()){
|
92
|
return Session.getUser().email;
|
93
|
}else{
|
94
|
return null;
|
95
|
}
|
96
|
|
97
|
}
|
98
|
public static getCurrentUrl():string {
|
99
|
if( typeof localStorage !== 'undefined') {
|
100
|
return localStorage.getItem("url");
|
101
|
}
|
102
|
return "";
|
103
|
|
104
|
}
|
105
|
public static setCurrentUrl(url:string) {
|
106
|
if( typeof localStorage !== 'undefined') {
|
107
|
localStorage.setItem("url", url);
|
108
|
}
|
109
|
}
|
110
|
public static getCurrentParameters():any {
|
111
|
if( typeof localStorage !== 'undefined') {
|
112
|
var params = localStorage.getItem("params");
|
113
|
var object = null;
|
114
|
if(params.split("&").length > 0){
|
115
|
object = {};
|
116
|
}
|
117
|
for(var i=0; i<params.split("&").length; i++){
|
118
|
object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
|
119
|
}
|
120
|
return object;
|
121
|
}
|
122
|
return {};
|
123
|
}
|
124
|
public static setCurrentParameters(params:string) {
|
125
|
if( typeof localStorage !== 'undefined') {
|
126
|
if(params.indexOf("?")==0){
|
127
|
params = params.substring(1);
|
128
|
}
|
129
|
localStorage.setItem("params",(params && params.length > 1)? params:"");
|
130
|
}
|
131
|
}
|
132
|
public static isClaimsCurator():boolean {
|
133
|
var isAuthorized = false;
|
134
|
if(Session.isLoggedIn()){
|
135
|
|
136
|
var claimRoles = ["urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu"]
|
137
|
for (var i = 0; i < claimRoles.length; i++) {
|
138
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
139
|
isAuthorized = true;
|
140
|
break;
|
141
|
}
|
142
|
}
|
143
|
return (isAuthorized);
|
144
|
}
|
145
|
return (isAuthorized);
|
146
|
}
|
147
|
public static isCommunityCurator():boolean {
|
148
|
var isAuthorized = false;
|
149
|
if(Session.isLoggedIn()){
|
150
|
var roles = ["urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu"]
|
151
|
for (var i = 0; i < roles.length; i++) {
|
152
|
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
|
153
|
isAuthorized = true;
|
154
|
break;
|
155
|
}
|
156
|
}
|
157
|
return (isAuthorized);
|
158
|
}
|
159
|
return (isAuthorized);
|
160
|
}
|
161
|
public static isPortalAdministrator():boolean {
|
162
|
var isAuthorized = false;
|
163
|
if(Session.isLoggedIn()){
|
164
|
var roles = ["urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu"]
|
165
|
for (var i = 0; i < roles.length; i++) {
|
166
|
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
|
167
|
isAuthorized = true;
|
168
|
break;
|
169
|
}
|
170
|
}
|
171
|
// console.log("Is admin:"+ isAdmin)
|
172
|
return (isAuthorized);
|
173
|
}
|
174
|
// console.log("Is admin:"+ isAdmin)
|
175
|
return (isAuthorized);
|
176
|
}
|
177
|
public static isRegisteredUser():boolean {
|
178
|
var isRegisteredUser = false;
|
179
|
if(Session.isLoggedIn()){
|
180
|
var claimRoles = ["urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu"];
|
181
|
for (var i = 0; i < claimRoles.length; i++) {
|
182
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
183
|
isRegisteredUser = true;
|
184
|
break;
|
185
|
}
|
186
|
}
|
187
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
188
|
return (isRegisteredUser);
|
189
|
}
|
190
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
191
|
return (isRegisteredUser);
|
192
|
}
|
193
|
public static isUserValid() {
|
194
|
if(Session.isLoggedIn()){
|
195
|
var expires = Session.getUser().expirationDate;
|
196
|
var now = new Date().getTime() / 1000;
|
197
|
// console.log(" is still valid ? "+(now +0 < expires) +" Remaining:"+ (expires - (now+0))+ " now is:"+now + "expires at:"+expires);
|
198
|
return now +0 < expires;
|
199
|
}
|
200
|
return false;
|
201
|
}
|
202
|
public static isValidAndRemove() {
|
203
|
if(Session.isLoggedIn()){
|
204
|
if(!Session.isUserValid()){
|
205
|
Session.removeUser();
|
206
|
return false;
|
207
|
}else{
|
208
|
return true;
|
209
|
}
|
210
|
}else{
|
211
|
return false;
|
212
|
}
|
213
|
}
|
214
|
|
215
|
}
|
216
|
export class MyJWT{
|
217
|
private static validateJWTFormat(data){
|
218
|
if(data != null && (data.indexOf(".") !=-1 && data.split('.').length == 3)){
|
219
|
return true;
|
220
|
}
|
221
|
return false;
|
222
|
}
|
223
|
private static getPayload(data){
|
224
|
var payload = data.split('.')[1];
|
225
|
return StringUtils.b64DecodeUnicode(payload);
|
226
|
}
|
227
|
public static parseUserInfo(data: any): User {
|
228
|
if(this.validateJWTFormat(data)){
|
229
|
var info = JSON.parse(this.getPayload(data));
|
230
|
}else{
|
231
|
return null;
|
232
|
}
|
233
|
var user: User = new User();
|
234
|
user.id = (info.sub && info.sub.indexOf('@'))?info.sub.substring(0,info.sub.indexOf('@')):info.sub;
|
235
|
|
236
|
user.firstname = (StringUtils.URIDecode((info.firstname && info.firstname!="")?info.firstname:"")).replace("+"," ");
|
237
|
user.lastname = (StringUtils.URIDecode((info.lastname && info.lastname!="")?info.lastname:"")).replace("+"," ");
|
238
|
user.email = info.email;
|
239
|
// user.id = info.userId;
|
240
|
user.fullname = (StringUtils.URIDecode((info.fullname && info.fullname!="")?info.fullname:"")).replace("+"," ");
|
241
|
if(user.fullname == ""){
|
242
|
if(user.firstname != ""){
|
243
|
user.fullname += user.firstname;
|
244
|
}
|
245
|
if(user.lastname !=""){
|
246
|
user.fullname += user.lastname;
|
247
|
}
|
248
|
if(user.fullname == ""){ //it is still empty set a default
|
249
|
user.fullname = "Anonymous user";
|
250
|
}
|
251
|
}
|
252
|
if(info.role && info.role != ""){
|
253
|
user.role =JSON.parse( StringUtils.URIDecode(info.role));
|
254
|
}else{
|
255
|
user.role =[];
|
256
|
}
|
257
|
|
258
|
// console.log("User Role is:");
|
259
|
// console.log(user.role)
|
260
|
user.jwt = data;
|
261
|
user.expirationDate = info.exp;
|
262
|
localStorage.setItem("user", JSON.stringify(user));
|
263
|
console.log(user)
|
264
|
return user;
|
265
|
}
|
266
|
|
267
|
}
|
268
|
export class COOKIE{
|
269
|
public static cookieName_u:string="XCsrfToken";
|
270
|
public static cookieName_id:string="AccessToken";
|
271
|
|
272
|
public static getCookie(name: string) : string {
|
273
|
if(typeof document == 'undefined'){
|
274
|
return null;
|
275
|
}
|
276
|
let ca: Array<string> = document.cookie.split(';');
|
277
|
let caLen: number = ca.length;
|
278
|
let cookieName = `${name}=`;
|
279
|
let c: string;
|
280
|
|
281
|
for (let i: number = 0; i < caLen; i += 1) {
|
282
|
c = ca[i].replace(/^\s+/g, '');
|
283
|
if (c.indexOf(cookieName) == 0) {
|
284
|
return c.substring(cookieName.length, c.length);
|
285
|
}
|
286
|
}
|
287
|
return null;
|
288
|
}
|
289
|
public static deleteCookie(name) {
|
290
|
this.setCookie(name, '', -1);
|
291
|
}
|
292
|
public static setCookie(name: string, value: string, expireDays: number, path: string = '/') {
|
293
|
let d:Date = new Date();
|
294
|
d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
|
295
|
let expires:string = `expires=${d.toUTCString()}`;
|
296
|
// let cpath:string = path ? `; path=${path}` : '';
|
297
|
let domain = "";
|
298
|
if(document.domain.indexOf(".di.uoa.gr")!= -1){
|
299
|
domain = ".di.uoa.gr";
|
300
|
}else if(document.domain.indexOf(".openaire.eu") != -1){
|
301
|
domain = ".openaire.eu";
|
302
|
}
|
303
|
document.cookie = name+'='+value+'; path='+path+'; domain='+domain+';';
|
304
|
}
|
305
|
}
|