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