2017-12-19 13:53:46 +01:00
|
|
|
import {StringUtils} from '../../utils/string-utils.class';
|
2018-02-12 11:21:42 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
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() {
|
2018-06-12 13:15:42 +02:00
|
|
|
COOKIE.deleteCookie(COOKIE.cookieName_id)
|
|
|
|
COOKIE.deleteCookie(COOKIE.cookieName_u);
|
2018-06-29 13:58:37 +02:00
|
|
|
COOKIE.deleteCookie("openAIRESession");
|
2018-06-12 13:15:42 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
public static getUser():User {
|
2018-04-03 14:33:27 +02:00
|
|
|
return this.getUserFromCookie();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
public static isLoggedIn(): boolean {
|
2019-09-13 11:38:21 +02:00
|
|
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_id);
|
2019-09-13 12:02:30 +02:00
|
|
|
return (cookie != null && cookie != "");
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-09-13 12:02:30 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static getUserFromCookie():User{
|
|
|
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
2018-06-15 14:58:58 +02:00
|
|
|
if(cookie != null && cookie!=""){
|
2018-06-12 13:15:42 +02:00
|
|
|
var user:User = this.parseUserInfo(cookie);
|
2017-12-19 13:53:46 +01:00
|
|
|
if(user && user.email){
|
|
|
|
return user;
|
|
|
|
}else{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 13:15:42 +02:00
|
|
|
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();
|
2018-06-29 13:58:37 +02:00
|
|
|
var sub = (info.sub)?StringUtils.URIDecode(info.sub):"";
|
|
|
|
user.id = (sub && sub.indexOf('@'))?sub.substring(0,sub.indexOf('@')):sub;
|
2018-06-12 13:15:42 +02:00
|
|
|
|
|
|
|
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;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static getUserEmail():string {
|
2018-04-03 14:33:27 +02:00
|
|
|
var user:User = this.getUserFromCookie();
|
|
|
|
if(user!=null){
|
|
|
|
return user.email;
|
|
|
|
}
|
|
|
|
return null;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-11-29 13:38:59 +01:00
|
|
|
|
|
|
|
public static getUserFullName():string {
|
|
|
|
var user:User = this.getUserFromCookie();
|
|
|
|
if(user!=null){
|
|
|
|
return user.fullname;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2018-05-23 16:47:27 +02:00
|
|
|
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;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-05-23 16:47:27 +02:00
|
|
|
public static getParamsObj(params:string) {
|
2018-07-02 15:42:20 +02:00
|
|
|
var object = null;
|
2017-12-19 13:53:46 +01:00
|
|
|
if(params.split("&").length > 0){
|
|
|
|
object = {};
|
|
|
|
}
|
2018-07-02 15:42:20 +02:00
|
|
|
params =(params && params.split("?").length > 1 )?params.split("?")[1]:params;
|
2017-12-19 13:53:46 +01:00
|
|
|
for(var i=0; i<params.split("&").length; i++){
|
|
|
|
object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
|
|
|
|
}
|
2018-05-23 16:47:27 +02:00
|
|
|
|
|
|
|
return object;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
|
|
|
|
//Methods to check roles
|
2019-09-13 09:01:19 +02:00
|
|
|
// OLD
|
|
|
|
/* public static isClaimsCurator():boolean {
|
2018-03-27 10:22:55 +02:00
|
|
|
var isAuthorized = false;
|
2019-02-28 16:26:20 +01:00
|
|
|
if(Session.isLoggedIn()){
|
|
|
|
var roles = ["urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu"]
|
|
|
|
for (var i = 0; i < roles.length; i++) {
|
|
|
|
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
|
2018-03-27 10:22:55 +02:00
|
|
|
isAuthorized = true;
|
2017-12-19 13:53:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2019-02-28 16:26:20 +01:00
|
|
|
}
|
|
|
|
return isAuthorized;
|
2018-03-27 10:22:55 +02:00
|
|
|
}
|
|
|
|
public static isCommunityCurator():boolean {
|
|
|
|
var isAuthorized = false;
|
|
|
|
if(Session.isLoggedIn()){
|
2018-04-03 10:45:46 +02:00
|
|
|
var roles = ["urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu"]
|
2018-03-27 10:22:55 +02:00
|
|
|
for (var i = 0; i < roles.length; i++) {
|
|
|
|
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
|
|
|
|
isAuthorized = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2018-03-27 10:22:55 +02:00
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
public static isPortalAdministrator():boolean {
|
2018-03-27 10:22:55 +02:00
|
|
|
var isAuthorized = false;
|
2018-02-16 11:24:00 +01:00
|
|
|
if(Session.isLoggedIn()){
|
2018-04-03 10:45:46 +02:00
|
|
|
var roles = ["urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu"]
|
2018-02-16 11:24:00 +01:00
|
|
|
for (var i = 0; i < roles.length; i++) {
|
|
|
|
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
|
2018-03-27 10:22:55 +02:00
|
|
|
isAuthorized = true;
|
2018-02-16 11:24:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2018-02-16 11:24:00 +01:00
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2018-02-16 11:24:00 +01:00
|
|
|
}
|
2019-06-12 13:50:48 +02:00
|
|
|
public static isUserManager():boolean {
|
|
|
|
var isAuthorized = false;
|
|
|
|
if(Session.isLoggedIn()){
|
|
|
|
var roles = ["urn:geant:openaire.eu:group:User+Manager#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;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
public static isRegisteredUser():boolean {
|
2018-06-12 13:15:42 +02:00
|
|
|
var isAuthorized = false;
|
2017-12-19 13:53:46 +01:00
|
|
|
if(Session.isLoggedIn()){
|
2018-04-03 10:45:46 +02:00
|
|
|
var claimRoles = ["urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu"];
|
2017-12-19 13:53:46 +01:00
|
|
|
for (var i = 0; i < claimRoles.length; i++) {
|
|
|
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
2018-06-12 13:15:42 +02:00
|
|
|
isAuthorized = true;
|
2017-12-19 13:53:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
return isAuthorized;
|
2019-09-13 09:01:19 +02:00
|
|
|
}*/
|
|
|
|
|
|
|
|
// NEW
|
|
|
|
|
|
|
|
public static isClaimsCurator(user: User): boolean {
|
|
|
|
return user !== null &&
|
|
|
|
user.role.indexOf('urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu') !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isCommunityCurator(user: User): boolean {
|
|
|
|
return user !== null &&
|
|
|
|
user.role.indexOf('urn:geant:openaire.eu:group:Curator+-+Community#aai.openaire.eu') !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isPortalAdministrator(user: User): boolean {
|
|
|
|
return user !== null &&
|
|
|
|
user.role.indexOf('urn:geant:openaire.eu:group:Portal+Administrator#aai.openaire.eu') !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isUserManager(user: User): boolean {
|
|
|
|
return user !== null &&
|
|
|
|
user.role.indexOf('urn:geant:openaire.eu:group:User+Manager#aai.openaire.eu') !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isRegisteredUser(user: User): boolean {
|
|
|
|
return user !== null &&
|
|
|
|
user.role.indexOf('urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu') !== -1;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-06-12 13:15:42 +02:00
|
|
|
export class COOKIE{
|
|
|
|
public static cookieName_u:string="openAIREUser"//"XCsrfToken";
|
2017-12-19 13:53:46 +01:00
|
|
|
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 = '/') {
|
2019-05-22 17:04:10 +02:00
|
|
|
//TODO fix domain?
|
2017-12-19 13:53:46 +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}` : '';
|
2018-02-12 11:21:42 +01:00
|
|
|
let domain = "";
|
2019-05-22 17:04:10 +02:00
|
|
|
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+';';
|
2018-02-12 11:21:42 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|