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

205 lines
6.3 KiB
TypeScript
Raw Normal View History

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("openAIRESession");
COOKIE.deleteCookie("openAIREUser");
}
public static isLoggedIn(): boolean {
var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
return (cookie != null && cookie != "");
}
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(user: User): boolean {
return user &&
(user.role.indexOf('urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu') !== -1 ||
user.role.indexOf('CURATOR_CLAIM') !== -1);
}
public static isMonitorCurator(user: User): boolean {
return this.isCommunityCurator(user) || this.isProjectCurator(user) || this.isFunderCurator(user) || this.isOrganizationCurator(user);
}
public static isCommunityCurator(user: User): boolean {
return this.isTypeCurator("Community", user);
}
public static isFunderCurator(user: User): boolean {
return this.isTypeCurator("Funder", user);
}
public static isProjectCurator(user: User): boolean {
return this.isTypeCurator("Project", user);
}
public static isOrganizationCurator(user: User): boolean {
return this.isTypeCurator("Institution", user);
}
private static isTypeCurator(type: string, user: User): boolean {
return user &&
(user.role.indexOf('CURATOR_' + type.toUpperCase()) !== -1 ||
user.role.indexOf('urn:geant:openaire.eu:group:Curator+-+' + type + '#aai.openaire.eu') !== -1);
}
public static isCurator(type: string, user: User): boolean {
if (type == 'funder') {
return user && this.isFunderCurator(user);
} else if (type == 'ri' || type == 'community') {
return user && this.isCommunityCurator(user);
} else if (type == 'organization' || type == 'institution') {
return user && this.isOrganizationCurator(user);
} else if (type == 'project') {
return user && this.isProjectCurator(user);
}
}
public static isPortalAdministrator(user: User): boolean {
return user &&
(user.role.indexOf('urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu') !== -1 ||
user.role.indexOf('PORTAL_ADMINISTRATOR') !== -1);
}
public static isUserManager(user: User): boolean {
return user &&
(user.role.indexOf('urn:geant:openaire.eu:group:User+Manager#aai.openaire.eu') !== -1 ||
user.role.indexOf('USER_MANAGER') !== -1);
}
public static isSubscribedTo(type: string, id: string, user: User): boolean {
return user && user.role.indexOf(this.mapType(type).toUpperCase() + '_' + id.toUpperCase()) !== -1;
}
public static isMember(type: string, id: string, user: User): boolean {
return user && user.role.indexOf(this.mapType(type, false).toUpperCase() + '_' + id.toUpperCase()) !== -1;
}
public static isManager(type: string, id: string, user: User): boolean {
return user && user.role.indexOf(this.mapType(type).toUpperCase() + '_' + id.toUpperCase() + '_MANAGER') !== -1
}
public static isKindOfMonitorManager(user: User): boolean {
if (user) {
for (let role of user.role) {
if (role.indexOf('_MANAGER') !== -1) {
return true;
}
}
}
return false;
}
public static isKindOfCommunityManager(user: User): boolean {
if (user) {
for (let role of user.role) {
if (role.indexOf('COMMUNITY') !== -1 && role.indexOf('_MANAGER') !== -1) {
return true;
}
}
}
return false;
}
public static isRegisteredUser(user: User): boolean {
return user &&
(user.role.indexOf('urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu') !== -1 ||
user.role.indexOf('REGISTERED_USER') !== -1);
}
public static mapType(type: string, communityMap: boolean = true): string {
if (type == "ri" && communityMap) {
type = "community";
} else if (type == "organization") {
type = "institution";
}
return type;
}
}
export class COOKIE {
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 = '/') {
//TODO fix domain?
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 (typeof document !== 'undefined') {
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 + ';SameSite=Lax;';
}
}
}