openaire-library/login/utils/helper.class.ts

227 lines
6.8 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 removeUser() {
COOKIE.deleteCookie(COOKIE.cookieName_id)
COOKIE.deleteCookie(COOKIE.cookieName_u);
COOKIE.deleteCookie("openAIRESession");
}
public static getUser():User {
return this.getUserFromCookie();
}
public static isLoggedIn(): boolean {
var loggedIn:boolean = false;
var user:User = null;
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
if(cookie == null ||cookie == ""){
return false;
}else{
var user:User = this.getUserFromCookie();
if(user ==null){
return false;
}
return true;
}
}
public static getUserFromCookie():User{
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
if(cookie != null && cookie!=""){
var user:User = this.parseUserInfo(cookie);
if(user && user.email){
return user;
}else{
return null;
}
}else{
return null;
}
}
public static parseUserInfo(data: any): User {
var dataStr:string = data.substr(1,data.length-2).replace(/\\/g,"");
var info = JSON.parse(dataStr);
var user: User = new User();
var sub = (info.sub)?StringUtils.URIDecode(info.sub):"";
user.id = (sub && sub.indexOf('@'))?sub.substring(0,sub.indexOf('@')):sub;
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.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 == ""){ //fullname 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 =[];
}
user.expirationDate = info.exp;
return user;
}
public static getUserEmail():string {
var user:User = this.getUserFromCookie();
if(user!=null){
return user.email;
}
return null;
}
public static getUserFullName():string {
var user:User = this.getUserFromCookie();
if(user!=null){
return user.fullname;
}
return null;
}
public static setReloadUrl(host:string,path:string, params:string) {
var URL = {};
URL["host"]=host;
URL["path"]=path;
URL["params"]=params;
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
}
public static getReloadUrl(plainText:boolean =false) {
var URL = COOKIE.getCookie("reloadURL");
URL = JSON.parse(URL);
return URL;
}
public static getParamsObj(params:string) {
var object = null;
if(params.split("&").length > 0){
object = {};
}
params =(params && params.split("?").length > 1 )?params.split("?")[1]:params;
for(var i=0; i<params.split("&").length; i++){
object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
}
return object;
}
//Methods to check roles
public static isClaimsCurator():boolean {
var isAuthorized = false;
var user:User = this.getUserFromCookie();
if(user!=null){
return false;
}
var claimRoles = ["urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu"]
for (var i = 0; i < claimRoles.length; i++) {
if ((user.role).indexOf(claimRoles[i]) > -1) {
isAuthorized = true;
break;
}
}
return isAuthorized;
}
public static isCommunityCurator():boolean {
var isAuthorized = false;
if(Session.isLoggedIn()){
var roles = ["urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu"]
for (var i = 0; i < roles.length; i++) {
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
isAuthorized = true;
break;
}
}
return isAuthorized;
}
return isAuthorized;
}
public static isPortalAdministrator():boolean {
var isAuthorized = false;
if(Session.isLoggedIn()){
var roles = ["urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu"]
for (var i = 0; i < roles.length; i++) {
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
isAuthorized = true;
break;
}
}
return isAuthorized;
}
return isAuthorized;
}
public static isRegisteredUser():boolean {
var isAuthorized = false;
if(Session.isLoggedIn()){
var claimRoles = ["urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu"];
for (var i = 0; i < claimRoles.length; i++) {
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
isAuthorized = true;
break;
}
}
return isAuthorized;
}
return isAuthorized;
}
}
export class COOKIE{
public static cookieName_u:string="openAIREUser"//"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}` : '';
let domain = "";
if(document.domain.indexOf(".di.uoa.gr")!= -1){ // for development
domain = ".di.uoa.gr";
}else if(document.domain.indexOf(".openaire.eu") != -1){
domain = ".openaire.eu";
}
document.cookie = name+'='+value+'; path='+path+'; domain='+domain+';';
}
}