diff --git a/connect/approvedByCommunity/approved.component.ts b/connect/approvedByCommunity/approved.component.ts index d895332d..71adfaf3 100644 --- a/connect/approvedByCommunity/approved.component.ts +++ b/connect/approvedByCommunity/approved.component.ts @@ -33,8 +33,8 @@ export class ApprovedByCommunityComponent { communityId => { this.communityId = communityId['communityId']; - if(!this.communityId && (typeof document !== 'undefined')){ - this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname); + if(!this.communityId){ + this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); } if(this.communityId && this.communityId != "openaire") { diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index bdb44997..4ef49280 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -15,16 +15,16 @@ export class ConnectSubscriberGuard implements CanActivate, CanLoad { check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; - if (!community && (typeof document !== 'undefined')) { - community = ConnectHelper.getCommunityFromDomain(document.location.hostname); - } if (!Session.isLoggedIn()) { errorCode = LoginErrorCodes.NOT_LOGIN; this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}}); return false; } else { const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { - return this.communityService.isSubscribedToCommunity(community, Session.getUserEmail(), properties['adminToolsAPIURL']); + if(!community){ + community = ConnectHelper.getCommunityFromDomain(properties.domain); + } + return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"]) }); obs.filter(enabled => !enabled) .subscribe(() => this.router.navigate(['/user-info'], { @@ -40,10 +40,10 @@ export class ConnectSubscriberGuard implements CanActivate, CanLoad { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.queryParams['communityId'], state.url); } - canLoad(route: Route): Observable | Promise | boolean { const path = '/' + route.path; return this.check(ConnectHelper.getCommunityFromPath(path), path); + } } diff --git a/connect/connectHelper.ts b/connect/connectHelper.ts index 79e5e445..30285c31 100644 --- a/connect/connectHelper.ts +++ b/connect/connectHelper.ts @@ -3,7 +3,7 @@ import {HttpParams} from '@angular/common/http'; export class ConnectHelper { public static getCommunityFromDomain(domain: string): string{ - // domain = "beta.egi.openaire.eu"; for testing + // domain = "beta.egi.openaire.eu"; //for testing if (domain.indexOf('openaire.eu') === -1) { return null; } @@ -15,7 +15,7 @@ export class ConnectHelper { } else { domain = domain.substr(0, domain.indexOf('.')); } - if (domain === 'connect' || domain === 'explore') { + if(domain == "connect" || domain == "explore" || domain == "monitor"){ return null; } return domain; diff --git a/connect/userEmailPreferences/mailPrefs.component.ts b/connect/userEmailPreferences/mailPrefs.component.ts index 03fac6ae..db7ae031 100644 --- a/connect/userEmailPreferences/mailPrefs.component.ts +++ b/connect/userEmailPreferences/mailPrefs.component.ts @@ -52,20 +52,20 @@ export class MailPrefsComponent { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; + this.sub = this.route.queryParams.subscribe(params => { + this.communityId = params['communityId']; + + if(!this.communityId){ + this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); + } + + this.fetchId = Session.getUserEmail(); + + this.getEmailPreferences(); + + }); }); - this.sub = this.route.queryParams.subscribe(params => { - this.communityId = params['communityId']; - - if(!this.communityId && (typeof document !== 'undefined')){ - this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname); - } - - this.fetchId = Session.getUserEmail(); - - this.getEmailPreferences(); - - }); } getEmailPreferences() { diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index 700427a2..07bf0d94 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -25,25 +25,26 @@ export class IsRouteEnabled implements CanActivate, CanLoad { if (!community && data['community']) { // for openaire community = data['community']; } - if (!community && typeof document !== 'undefined') { - community = ConnectHelper.getCommunityFromDomain(document.location.hostname); - } - if (community) { const redirect = customRedirect ? customRedirect : '/error'; - const obs = this.propertiesService.subscribeEnvironment().map(res => res['adminToolsAPIURL']).mergeMap(url => { - return this.config.isPageEnabled(url, community, '/' + path.split('?')[0].substring(1)); + const obs = this.propertiesService.subscribeEnvironment().map(res => { + if(!community){ + community = ConnectHelper.getCommunityFromDomain(res.domain); + } + return res["adminToolsAPIURL"] + }).mergeMap(url => { + if(!community){ // no community to check - return true + return Observable.of(true); + } + return this.config.isPageEnabled(url, community,"/"+path.split("?")[0].substring(1)) }); obs.filter(enabled => !enabled) .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}})); return obs; - } - return true; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.data, route.queryParams['communityId'], state.url); } - canLoad(route: Route): Observable | Promise | boolean { const path = '/' + route; return this.check(route.data, ConnectHelper.getCommunityFromPath(path), path); diff --git a/utils/helper/helper.component.ts b/utils/helper/helper.component.ts index 58131f80..b0e5767f 100644 --- a/utils/helper/helper.component.ts +++ b/utils/helper/helper.component.ts @@ -41,9 +41,9 @@ export class HelperComponent { this.route.queryParams.subscribe( params => { this.communityId = params['communityId']; - if(!this.communityId && (typeof document !== 'undefined')){ - this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname); - } + if(!this.communityId){ + this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); + } if(!this.communityId){ this.communityId = this.properties.adminToolsCommunity; } diff --git a/utils/properties/env-properties.ts b/utils/properties/env-properties.ts index 62cd538c..1cde6b5b 100644 --- a/utils/properties/env-properties.ts +++ b/utils/properties/env-properties.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; export class EnvProperties { environment:string; //{beta,test,production, development} + domain:string; enablePiwikTrack:boolean ; enableHelper:boolean; useCache:boolean ; diff --git a/utils/properties/environment-specific.service.ts b/utils/properties/environment-specific.service.ts index 51ca6cb4..06dc48dd 100644 --- a/utils/properties/environment-specific.service.ts +++ b/utils/properties/environment-specific.service.ts @@ -1,4 +1,4 @@ -import { Injectable, OnInit, PLATFORM_ID, Inject,InjectionToken , Optional } from '@angular/core'; +import { Injectable, OnInit, PLATFORM_ID, Inject,InjectionToken , Optional, Injector } from '@angular/core'; import { isPlatformBrowser} from '@angular/common'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Observable, Subscription, BehaviorSubject } from 'rxjs/Rx'; @@ -12,20 +12,13 @@ import { DOCUMENT } from '@angular/platform-browser'; export class EnvironmentSpecificService { public envSpecific: EnvProperties; + public domain: string; public envSpecificNull: EnvProperties = null; testBrowser: boolean; private envSpecificSubject: BehaviorSubject = new BehaviorSubject(null); private propertiesUrl = "/assets/env-properties.json"; - - private serverUrl = "http://scoobydoo.di.uoa.gr:4000"; - // private serverUrl = "https://beta.explore.openaire.eu"; - // private serverUrl = "https://beta.connect.openaire.eu"; - // private serverUrl = "https://beta.admin.connect.openaire.eu"; - // private serverUrl = "https://explore.openaire.eu"; - // private serverUrl = "https://connect.openaire.eu"; - // private serverUrl = "https://test.explore.openaire.eu"; - constructor(private http: Http, @Inject(PLATFORM_ID) private platformId: string) { + constructor(private http: Http, @Inject(PLATFORM_ID) private platformId: string, private injector: Injector) { this.testBrowser = isPlatformBrowser(platformId); if (this.testBrowser) { //this is only executed on the browser @@ -33,35 +26,65 @@ export class EnvironmentSpecificService { } public loadEnvironment() { // Only want to do this once - if root page is revisited, it calls this again. - if (this.envSpecific === null || this.envSpecific === undefined) { - var location = this.propertiesUrl; - if(typeof document == 'undefined'){ - location = this.serverUrl + this.propertiesUrl; - }else{ - location = document.location.protocol +"//" + document.location.host+this.propertiesUrl; - } - //console.log('Loading '+ location); + if (this.envSpecific === null || this.envSpecific === undefined) { + var domain = this.getDomain(); + var location = this.getFullUrl(); + // console.log('loadEnvironment: Loading '+ location); return this.http.get(location) - .map((data) => data.json()) + .map((data) => { + var properties:EnvProperties=data.json(); + properties.domain = domain; + this.envSpecific = properties; + return properties; + }) .toPromise(); } - - return Promise.resolve(this.envSpecificNull); + // console.log('loadEnvironment: already loaded '); + return Promise.resolve(this.envSpecific); } + public subscribeEnvironment() { // Only want to do this once - if root page is revisited, it calls this again. if (this.envSpecific === null || this.envSpecific === undefined) { - //console.log('Loading env-properties.json'); - - return this.http.get(this.propertiesUrl) - .map((data) => data.json()); + var domain = this.getDomain(); + var location = this.getFullUrl(); + // console.log('subscribeEnvironment: Loading '+ location); + return this.http.get(location) + .map((data) => { + var properties:EnvProperties=data.json(); + properties.domain = domain; + this.envSpecific = properties; + return properties; + }) } - + // console.log('subscribeEnvironment: already loaded '); return Observable.of(this.envSpecific); } - + getFullUrl(){ + var location =""; + var domain = ""; + if (typeof document == 'undefined') { + let req = this.injector.get('request'); + domain = req.get('host').split(":")[0]; + location = (domain.indexOf(".openaire.eu")!=-1?"https://":"http://")+ req.get('host') + this.propertiesUrl; + }else{ + location = document.location.protocol +"//" + document.location.host+this.propertiesUrl; + domain = document.location.hostname; + } + return location; + } + getDomain(){ + var domain = ""; + if (typeof document == 'undefined') { + let req = this.injector.get('request'); + domain = req.get('host').split(":")[0]; + }else{ + domain = document.location.hostname; + } + return domain; + } public setEnvProperties(es: EnvProperties) { // This has already been set so bail out. diff --git a/utils/properties/environmentSpecificResolver.ts b/utils/properties/environmentSpecificResolver.ts index 8bdc9658..c11afdfe 100644 --- a/utils/properties/environmentSpecificResolver.ts +++ b/utils/properties/environmentSpecificResolver.ts @@ -12,8 +12,8 @@ export class EnvironmentSpecificResolver implements Resolve { resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { return this.envSpecificSvc.loadEnvironment() .then(es => { - this.envSpecificSvc.setEnvProperties(es); - return this.envSpecificSvc.envSpecific; + + return es; }, error => { console.log(error); return null;