275 lines
8.3 KiB
TypeScript
275 lines
8.3 KiB
TypeScript
import {StringUtils} from '../../utils/string-utils.class';
|
|
|
|
export class User {
|
|
email:string;
|
|
firstname: string;
|
|
lastname: string;
|
|
id: string;
|
|
fullname: string;
|
|
expirationDate: number;
|
|
role:string[];
|
|
jwt:string;
|
|
|
|
}
|
|
|
|
export class Session{
|
|
// public static setUser(user:User): User {
|
|
//
|
|
// localStorage.setItem("user", JSON.stringify(user));
|
|
//
|
|
// return user;
|
|
// }
|
|
public static removeUser() {
|
|
if(Session.isLoggedIn()){
|
|
localStorage.removeItem("user");
|
|
COOKIE.deleteCookie(COOKIE.cookieName_id)
|
|
COOKIE.deleteCookie(COOKIE.cookieName_u);
|
|
}
|
|
}
|
|
public static getUser():User {
|
|
if(Session.isLoggedIn()){
|
|
return JSON.parse(localStorage.getItem("user"));
|
|
}else{
|
|
return null;
|
|
}
|
|
}
|
|
public static isLoggedIn(): boolean {
|
|
var loggedIn:boolean = false;
|
|
var user:User = null;
|
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
|
if( typeof localStorage !== 'undefined') {
|
|
if(localStorage.getItem("user") && (cookie != null && cookie != "")) {
|
|
user = JSON.parse(localStorage.getItem("user"));
|
|
if(user && (user.fullname != null || user.lastname !=null || user.firstname !=null)){
|
|
|
|
loggedIn = true;
|
|
}else if(cookie != null && this.getUserFromCookie()!= null){
|
|
loggedIn = true
|
|
}else{
|
|
loggedIn = false;
|
|
}
|
|
}else{
|
|
if(cookie != null && this.getUserFromCookie()!= null){
|
|
loggedIn = true
|
|
}else{
|
|
loggedIn = false;
|
|
}
|
|
|
|
}
|
|
}else{
|
|
loggedIn = false;
|
|
}
|
|
return loggedIn;
|
|
}
|
|
public static getUserFromCookie():User{
|
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
|
if(cookie != null){
|
|
var user:User = MyJWT.parseUserInfo(cookie);
|
|
if( typeof localStorage !== 'undefined') {
|
|
localStorage.setItem("user", JSON.stringify(user));
|
|
if(user && user.email){
|
|
// COOKIE.deleteCookie(COOKIE.cookieName_u); // delete cookie to avoid transfer through requests
|
|
return user;
|
|
}else{
|
|
return null;
|
|
}
|
|
}else{
|
|
return null;
|
|
}
|
|
}else{
|
|
return null;
|
|
}
|
|
}
|
|
public static getUserJwt():string {
|
|
if(Session.isLoggedIn()){
|
|
return Session.getUser().jwt;
|
|
}else{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
public static getUserEmail():string {
|
|
if(Session.isLoggedIn()){
|
|
return Session.getUser().email;
|
|
}else{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
public static getCurrentUrl():string {
|
|
if( typeof localStorage !== 'undefined') {
|
|
return localStorage.getItem("url");
|
|
}
|
|
return "";
|
|
|
|
}
|
|
public static setCurrentUrl(url:string) {
|
|
if( typeof localStorage !== 'undefined') {
|
|
localStorage.setItem("url", url);
|
|
}
|
|
}
|
|
public static getCurrentParameters():any {
|
|
if( typeof localStorage !== 'undefined') {
|
|
var params = localStorage.getItem("params");
|
|
console.log(params);
|
|
var object = null;
|
|
if(params.split("&").length > 0){
|
|
object = {};
|
|
}
|
|
for(var i=0; i<params.split("&").length; i++){
|
|
console.log(params.split("&")[i]);
|
|
console.log((params.split("&")[i]).split("=")[0]);
|
|
console.log((params.split("&")[i]).split("=")[1]);
|
|
object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
|
|
}
|
|
return object;
|
|
}
|
|
return {};
|
|
}
|
|
public static setCurrentParameters(params:string) {
|
|
if( typeof localStorage !== 'undefined') {
|
|
if(params.indexOf("?")==0){
|
|
params = params.substring(1);
|
|
}
|
|
localStorage.setItem("params",(params && params.length > 1)? params:"");
|
|
}
|
|
}
|
|
public static isAdminUser():boolean {
|
|
var isAdmin = false;
|
|
if(Session.isLoggedIn()){
|
|
var claimRoles = ["urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Curator+-+Claim","urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Portal+Administrator"]
|
|
for (var i = 0; i < claimRoles.length; i++) {
|
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
|
isAdmin = true;
|
|
break;
|
|
}
|
|
}
|
|
// console.log("Is admin:"+ isAdmin)
|
|
return (isAdmin);
|
|
}
|
|
// console.log("Is admin:"+ isAdmin)
|
|
return (isAdmin);
|
|
}
|
|
public static isRegisteredUser():boolean {
|
|
var isRegisteredUser = false;
|
|
if(Session.isLoggedIn()){
|
|
var claimRoles = ["urn:mace:openminted.eu:aai.openminted.eu:group:Registered+User"];
|
|
for (var i = 0; i < claimRoles.length; i++) {
|
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
|
isRegisteredUser = true;
|
|
break;
|
|
}
|
|
}
|
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
|
return (isRegisteredUser);
|
|
}
|
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
|
return (isRegisteredUser);
|
|
}
|
|
public static isUserValid() {
|
|
if(Session.isLoggedIn()){
|
|
var expires = Session.getUser().expirationDate;
|
|
var now = new Date().getTime() / 1000;
|
|
// console.log(" is still valid ? "+(now +0 < expires) +" Remaining:"+ (expires - (now+0))+ " now is:"+now + "expires at:"+expires);
|
|
return now +0 < expires;
|
|
}
|
|
return false;
|
|
}
|
|
public static isValidAndRemove() {
|
|
if(Session.isLoggedIn()){
|
|
if(!Session.isUserValid()){
|
|
Session.removeUser();
|
|
return false;
|
|
}else{
|
|
return true;
|
|
}
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
export class MyJWT{
|
|
private static validateJWTFormat(data){
|
|
if(data != null && (data.indexOf(".") !=-1 && data.split('.').length == 3)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
private static getPayload(data){
|
|
var payload = data.split('.')[1];
|
|
return StringUtils.b64DecodeUnicode(payload);
|
|
}
|
|
public static parseUserInfo(data: any): User {
|
|
if(this.validateJWTFormat(data)){
|
|
var info = JSON.parse(this.getPayload(data));
|
|
}else{
|
|
return null;
|
|
}
|
|
var user: User = new User();
|
|
|
|
user.firstname = (StringUtils.URIDecode((info.firstname && info.firstname!="")?info.firstname:"")).replace("+"," ");
|
|
user.lastname = (StringUtils.URIDecode((info.lastname && info.lastname!="")?info.lastname:"")).replace("+"," ");
|
|
user.email = info.email;
|
|
// user.id = info.userId;
|
|
user.fullname = (StringUtils.URIDecode((info.fullname && info.fullname!="")?info.fullname:"")).replace("+"," ");
|
|
if(user.fullname == ""){
|
|
if(user.firstname != ""){
|
|
user.fullname += user.firstname;
|
|
}
|
|
if(user.lastname !=""){
|
|
user.fullname += user.lastname;
|
|
}
|
|
if(user.fullname == ""){ //it is still empty set a default
|
|
user.fullname = "Anonymous user";
|
|
}
|
|
}
|
|
if(info.role && info.role != ""){
|
|
user.role =JSON.parse( StringUtils.URIDecode(info.role));
|
|
}else{
|
|
user.role =[];
|
|
}
|
|
|
|
// console.log("User Role is:");
|
|
// console.log(user.role)
|
|
user.jwt = data;
|
|
user.expirationDate = info.exp;
|
|
localStorage.setItem("user", JSON.stringify(user));
|
|
console.log(user)
|
|
return user;
|
|
}
|
|
|
|
}
|
|
export class COOKIE{
|
|
public static cookieName_u:string="XCsrfToken";
|
|
public static cookieName_id:string="AccessToken";
|
|
|
|
public static getCookie(name: string) : string {
|
|
if(typeof document == 'undefined'){
|
|
return null;
|
|
}
|
|
let ca: Array<string> = document.cookie.split(';');
|
|
let caLen: number = ca.length;
|
|
let cookieName = `${name}=`;
|
|
let c: string;
|
|
|
|
for (let i: number = 0; i < caLen; i += 1) {
|
|
c = ca[i].replace(/^\s+/g, '');
|
|
if (c.indexOf(cookieName) == 0) {
|
|
return c.substring(cookieName.length, c.length);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public static deleteCookie(name) {
|
|
this.setCookie(name, '', -1);
|
|
}
|
|
public static setCookie(name: string, value: string, expireDays: number, path: string = '/') {
|
|
let d:Date = new Date();
|
|
d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
|
|
let expires:string = `expires=${d.toUTCString()}`;
|
|
// let cpath:string = path ? `; path=${path}` : '';
|
|
document.cookie = name+'='+value+'; path='+path+'; domain='+document.domain+';';
|
|
}
|
|
}
|