2017-12-19 13:53:46 +01:00
|
|
|
export class User {
|
2020-11-03 19:25:03 +01:00
|
|
|
email: string;
|
|
|
|
firstname: string;
|
|
|
|
lastname: string;
|
|
|
|
id: string;
|
|
|
|
fullname: string;
|
|
|
|
expirationDate: number;
|
|
|
|
role: string[];
|
2023-06-26 10:31:22 +02:00
|
|
|
accessToken?: string;
|
|
|
|
refreshToken?: string;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 19:25:03 +01:00
|
|
|
export class Session {
|
2017-12-19 13:53:46 +01:00
|
|
|
public static removeUser() {
|
2019-11-12 13:56:43 +01:00
|
|
|
COOKIE.deleteCookie(COOKIE.cookieName_id);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2022-04-13 11:24:14 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*
|
|
|
|
* Use userInfoSubject @UserManagementService in order to check if user is logged in
|
|
|
|
*
|
|
|
|
* */
|
2017-12-19 13:53:46 +01:00
|
|
|
public static isLoggedIn(): boolean {
|
2020-11-03 19:25:03 +01:00
|
|
|
var cookie = COOKIE.getCookie(COOKIE.cookieName_id);
|
|
|
|
return (cookie != null && cookie != "");
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2021-03-17 10:21:37 +01:00
|
|
|
public static setReloadUrl(host: string, path: string, params: string, fragment: string) {
|
2018-05-23 16:47:27 +02:00
|
|
|
var URL = {};
|
2020-11-03 19:25:03 +01:00
|
|
|
URL["host"] = host;
|
|
|
|
URL["path"] = path;
|
|
|
|
URL["params"] = params;
|
2021-03-17 10:21:37 +01:00
|
|
|
URL["fragment"] = fragment;
|
2018-05-23 16:47:27 +02:00
|
|
|
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
|
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2022-04-13 11:24:14 +02:00
|
|
|
public static getReloadUrl() {
|
2018-05-23 16:47:27 +02:00
|
|
|
var URL = COOKIE.getCookie("reloadURL");
|
|
|
|
URL = JSON.parse(URL);
|
|
|
|
return URL;
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
|
|
|
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++) {
|
2021-03-11 15:11:23 +01:00
|
|
|
object[(params.split("&")[i]).split("=")[0]] = decodeURIComponent((params.split("&")[i]).split("=")[1]);
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 16:47:27 +02:00
|
|
|
return object;
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2018-06-12 13:15:42 +02:00
|
|
|
//Methods to check roles
|
2019-09-13 09:01:19 +02:00
|
|
|
public static isClaimsCurator(user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.CURATOR_CLAIM) !== -1;
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
public static isMonitorCurator(user: User): boolean {
|
2020-11-03 19:25:03 +01:00
|
|
|
return this.isCommunityCurator(user) || this.isProjectCurator(user) || this.isFunderCurator(user) || this.isOrganizationCurator(user);
|
2020-11-03 10:38:11 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
public static isCommunityCurator(user: User): boolean {
|
2020-11-03 10:38:11 +01:00
|
|
|
return this.isTypeCurator("Community", user);
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
public static isFunderCurator(user: User): boolean {
|
2020-11-03 19:25:03 +01:00
|
|
|
return this.isTypeCurator("Funder", user);
|
2020-11-03 10:38:11 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
public static isProjectCurator(user: User): boolean {
|
|
|
|
return this.isTypeCurator("Project", user);
|
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
public static isOrganizationCurator(user: User): boolean {
|
|
|
|
return this.isTypeCurator("Institution", user);
|
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
private static isTypeCurator(type: string, user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.curator(type)) !== -1;
|
2020-11-03 10:38:11 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2020-11-03 10:38:11 +01:00
|
|
|
public static isCurator(type: string, user: User): boolean {
|
2020-11-03 19:25:03 +01:00
|
|
|
if (type == 'funder') {
|
|
|
|
return user && this.isFunderCurator(user);
|
|
|
|
} else if (type == 'ri' || type == 'community') {
|
2020-11-03 10:38:11 +01:00
|
|
|
return user && this.isCommunityCurator(user);
|
2020-11-03 19:25:03 +01:00
|
|
|
} else if (type == 'organization' || type == 'institution') {
|
2020-11-03 10:38:11 +01:00
|
|
|
return user && this.isOrganizationCurator(user);
|
2020-11-03 19:25:03 +01:00
|
|
|
} else if (type == 'project') {
|
2020-11-03 10:38:11 +01:00
|
|
|
return user && this.isProjectCurator(user);
|
|
|
|
}
|
2020-06-02 12:51:31 +02:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
public static isPortalAdministrator(user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.PORTAL_ADMIN) !== -1;
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
public static isUserManager(user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.USER_MANAGER) !== -1;
|
2020-08-06 13:50:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:54:14 +02:00
|
|
|
public static isSubscribedTo(type: string, id: string, user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.member(type, id)) !== -1;
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static isMember(type: string, id: string, user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.member(type, id)) !== -1;
|
2020-08-06 13:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static isManager(type: string, id: string, user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.manager(type, id)) !== -1
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static isKindOfMonitorManager(user: User): boolean {
|
|
|
|
if (user) {
|
|
|
|
for (let role of user.role) {
|
|
|
|
if (role.indexOf('_MANAGER') !== -1) {
|
|
|
|
return true;
|
2020-10-29 09:52:05 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
2020-10-29 09:52:05 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:09:44 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:01:19 +02:00
|
|
|
public static isRegisteredUser(user: User): boolean {
|
2021-02-04 14:36:29 +01:00
|
|
|
return user && user.role.indexOf(Role.REGISTERED_USER) !== -1;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:25:03 +01:00
|
|
|
export class COOKIE {
|
|
|
|
public static cookieName_id: string = "AccessToken";
|
|
|
|
|
|
|
|
public static getCookie(name: string): string {
|
|
|
|
if (typeof document == 'undefined') {
|
2017-12-19 13:53:46 +01:00
|
|
|
return null;
|
2020-11-03 19:25:03 +01:00
|
|
|
}
|
|
|
|
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;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static deleteCookie(name) {
|
2020-11-03 19:25:03 +01:00
|
|
|
this.setCookie(name, '', -1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static setCookie(name: string, value: string, expireDays: number, path: string = '/') {
|
2019-05-22 17:04:10 +02:00
|
|
|
//TODO fix domain?
|
2020-11-03 19:25:03 +01:00
|
|
|
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";
|
2018-02-12 11:21:42 +01:00
|
|
|
}
|
2020-11-03 19:25:03 +01:00
|
|
|
document.cookie = name + '=' + value + '; path=' + path + '; domain=' + domain + ';SameSite=Lax;';
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 14:36:29 +01:00
|
|
|
|
|
|
|
export class Role {
|
|
|
|
public static PORTAL_ADMIN = 'PORTAL_ADMINISTRATOR';
|
|
|
|
public static REGISTERED_USER = 'REGISTERED_USER';
|
|
|
|
public static ANONYMOUS_USER = 'ROLE_ANONYMOUS';
|
|
|
|
public static USER_MANAGER = 'USER_MANAGER';
|
|
|
|
public static CURATOR_CLAIM = 'CURATOR_CLAIM';
|
|
|
|
|
|
|
|
|
|
|
|
public static mapType(type: string, communityMap: boolean = true): string {
|
|
|
|
if (type == "ri" && communityMap) {
|
|
|
|
type = "community";
|
|
|
|
} else if (type == "organization") {
|
|
|
|
type = "institution";
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
public static curator(type: string): string {
|
|
|
|
return "CURATOR_" + this.mapType(type, true).toUpperCase();
|
|
|
|
}
|
|
|
|
|
2022-08-05 13:59:08 +02:00
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
public static typeManager(type: string): string {
|
|
|
|
return "MANAGER_" + this.mapType(type, true).toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
public static typeMember(type: string): string {
|
|
|
|
return this.mapType(type, false).toUpperCase();
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:36:29 +01:00
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
|
|
*
|
|
|
|
* Id = EE, EGI, etc
|
|
|
|
* */
|
|
|
|
public static manager(type: string, id: string) {
|
|
|
|
return this.mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
|
|
|
|
*
|
|
|
|
* Id = EE, EGI, etc
|
|
|
|
* */
|
|
|
|
public static member(type: string, id: string) {
|
|
|
|
return this.mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
|
|
|
|
}
|
|
|
|
}
|