diff --git a/connect/communityGuard/connectAdminLoginGuard.guard.ts b/connect/communityGuard/connectAdminLoginGuard.guard.ts index db9991ca..bcbc6ee0 100644 --- a/connect/communityGuard/connectAdminLoginGuard.guard.ts +++ b/connect/communityGuard/connectAdminLoginGuard.guard.ts @@ -4,7 +4,6 @@ import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, - ActivatedRoute, CanLoad, Route } from '@angular/router'; import {Observable} from 'rxjs/Observable'; @@ -12,16 +11,16 @@ import {Session} from '../../login/utils/helper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; +import {ConnectHelper} from '../connectHelper'; @Injectable() -export class ConnectAdminLoginGuard implements CanActivate, CanLoad{ +export class ConnectAdminLoginGuard implements CanActivate, CanLoad { constructor(private router: Router, private communityService: CommunityService, private propertiesService: EnvironmentSpecificService) {} - check(route: ActivatedRouteSnapshot, path: string): Observable | boolean { + check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - const community = route.queryParams['communityId']; if (Session.isLoggedIn()) { if (Session.isPortalAdministrator() || Session.isCommunityCurator()) { return true; @@ -47,10 +46,11 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad{ } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route, state.url); + return this.check(route.queryParams['communityId'], state.url); } canLoad(route: Route): Observable | Promise | boolean { - return this.check(null, '/' + route.path); + const path = '/' + route.path; + return this.check(ConnectHelper.getCommunityFromPath(path), path); } } diff --git a/connect/communityGuard/connectRIGuard.guard.ts b/connect/communityGuard/connectRIGuard.guard.ts index b5a86ad5..8b30a603 100644 --- a/connect/communityGuard/connectRIGuard.guard.ts +++ b/connect/communityGuard/connectRIGuard.guard.ts @@ -5,13 +5,14 @@ import { ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, - Route, ActivatedRoute + Route } from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Session} from '../../login/utils/helper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; +import {ConnectHelper} from '../connectHelper'; @Injectable() export class ConnectRIGuard implements CanActivate, CanLoad { @@ -21,10 +22,8 @@ export class ConnectRIGuard implements CanActivate, CanLoad { private propertiesService: EnvironmentSpecificService) { } - check(route: ActivatedRouteSnapshot, path: string): Observable | boolean { + check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - const community = (route.queryParams['communityId']); - if (Session.isLoggedIn()) { if (Session.isPortalAdministrator()) { return true; @@ -44,16 +43,16 @@ export class ConnectRIGuard implements CanActivate, CanLoad { } else { errorCode = LoginErrorCodes.NOT_LOGIN; this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}}); - return false; } } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route, state.url); + return this.check(route.queryParams['communityId'], state.url); } canLoad(route: Route): Observable | Promise | boolean { - return this.check(null, '/' + route.path); + const path = '/' + route.path; + return this.check(ConnectHelper.getCommunityFromPath(path), path); } } diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index bd6c0299..bdb44997 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -8,14 +8,13 @@ import { EnvironmentSpecificService} from '../../utils/properties/environment-sp import {ConnectHelper} from '../connectHelper'; @Injectable() -export class ConnectSubscriberGuard implements CanActivate, CanLoad{ +export class ConnectSubscriberGuard implements CanActivate, CanLoad { constructor(private router: Router, private communityService: CommunityService, private propertiesService: EnvironmentSpecificService) {} - check(route: ActivatedRouteSnapshot, path: string): Observable | boolean { + check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; - let community = route.queryParams['communityId']; if (!community && (typeof document !== 'undefined')) { community = ConnectHelper.getCommunityFromDomain(document.location.hostname); } @@ -39,11 +38,12 @@ export class ConnectSubscriberGuard implements CanActivate, CanLoad{ } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route, state.url); + return this.check(route.queryParams['communityId'], state.url); } canLoad(route: Route): Observable | Promise | boolean { - return this.check(null, '/' + route.path); + const path = '/' + route.path; + return this.check(ConnectHelper.getCommunityFromPath(path), path); } } diff --git a/connect/communityGuard/isCommunity.guard.ts b/connect/communityGuard/isCommunity.guard.ts index 9687aebe..160fd07a 100644 --- a/connect/communityGuard/isCommunity.guard.ts +++ b/connect/communityGuard/isCommunity.guard.ts @@ -8,6 +8,7 @@ import { } from '@angular/router'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/filter'; +import {ConnectHelper} from '../connectHelper'; @Injectable() export class IsCommunity implements CanActivate, CanLoad { @@ -15,9 +16,7 @@ export class IsCommunity implements CanActivate, CanLoad { constructor(private router: Router) { } - check(route: ActivatedRouteSnapshot): Observable | boolean { - const community = route.queryParams['communityId']; - console.log(community); + check(community: string): Observable | boolean { if (community && community !== 'undefined') { return true; } else { @@ -27,10 +26,11 @@ export class IsCommunity implements CanActivate, CanLoad { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route); + return this.check(route.queryParams['communityId']); } canLoad(route: Route): Observable | Promise | boolean { - return this.check(null); + const path = '/' + route.path; + return this.check(ConnectHelper.getCommunityFromPath(path)); } } diff --git a/connect/connectHelper.ts b/connect/connectHelper.ts index cc619da8..79e5e445 100644 --- a/connect/connectHelper.ts +++ b/connect/connectHelper.ts @@ -1,21 +1,32 @@ -export class ConnectHelper{ +import {HttpParams} from '@angular/common/http'; - public static getCommunityFromDomain(domain:string):string{ +export class ConnectHelper { + + public static getCommunityFromDomain(domain: string): string{ // domain = "beta.egi.openaire.eu"; for testing - if(domain.indexOf("openaire.eu") == -1){ + if (domain.indexOf('openaire.eu') === -1) { return null; } - if(domain.indexOf("beta")!=-1){ - domain = domain.substr(domain.indexOf(".")+1,domain.length); - domain = domain.substr(0,domain.indexOf(".")); - }else if(domain.indexOf("test.")!=-1){ + if ( domain.indexOf('beta') !== -1) { + domain = domain.substr(domain.indexOf('.') + 1, domain.length); + domain = domain.substr(0, domain.indexOf('.')); + } else if (domain.indexOf('test.') !== -1) { return null; - }else{ - domain = domain.substr(0,domain.indexOf(".")); + } else { + domain = domain.substr(0, domain.indexOf('.')); } - if(domain == "connect" || domain == "explore"){ + if (domain === 'connect' || domain === 'explore') { return null; } return domain; } + + public static getCommunityFromPath(path: string): string { + if (path.includes('?')) { + const httpParams = new HttpParams({ fromString: path.split('?')[1] }); + return httpParams.get('communityId'); + } else { + return null; + } + } } diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index b31c9bbc..700427a2 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -5,7 +5,7 @@ import { CanLoad, ActivatedRouteSnapshot, RouterStateSnapshot, - Route + Route, Data } from '@angular/router'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/filter'; @@ -18,13 +18,12 @@ export class IsRouteEnabled implements CanActivate, CanLoad { constructor(private router: Router, private config: ConfigurationService, - private propertiesService: EnvironmentSpecificService ) {} + private propertiesService: EnvironmentSpecificService) {} - check(route: ActivatedRouteSnapshot, path: string): Observable | boolean { - const customRedirect = route.data['redirect']; - let community = route.queryParams['communityId']; - if (!community && route.data['community']) { // for openaire - community = route.data['community']; + check(data: Data, community: string, path: string): Observable | boolean { + const customRedirect = data['redirect']; + if (!community && data['community']) { // for openaire + community = data['community']; } if (!community && typeof document !== 'undefined') { community = ConnectHelper.getCommunityFromDomain(document.location.hostname); @@ -42,10 +41,11 @@ export class IsRouteEnabled implements CanActivate, CanLoad { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route, state.url); + return this.check(route.data, route.queryParams['communityId'], state.url); } canLoad(route: Route): Observable | Promise | boolean { - return this.check(null, '/' + route.path); + const path = '/' + route; + return this.check(route.data, ConnectHelper.getCommunityFromPath(path), path); } }