diff --git a/connect/communityGuard/connectCommunityGuard.guard.ts b/connect/communityGuard/connectCommunityGuard.guard.ts index be2b9e98..e089ab7e 100644 --- a/connect/communityGuard/connectCommunityGuard.guard.ts +++ b/connect/communityGuard/connectCommunityGuard.guard.ts @@ -28,7 +28,8 @@ export class ConnectCommunityGuard implements CanActivate, CanActivateChild { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route.queryParams['communityId'], state.url); + let community = route.params['community']; + return community && this.check(community, state.url); } canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index 7b5ed6a5..77647160 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -1,6 +1,13 @@ import {filter, map, mergeMap, take} from 'rxjs/operators'; import {Injectable} from '@angular/core'; -import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; +import { + ActivatedRouteSnapshot, + CanActivate, + CanActivateChild, + Router, + RouterStateSnapshot, + UrlTree +} from '@angular/router'; import {Observable, of} from 'rxjs'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {properties} from "../../../../environments/environment"; @@ -9,17 +16,19 @@ import {Session} from "../../login/utils/helper.class"; import {UserManagementService} from "../../services/user-management.service"; @Injectable() -export class ConnectSubscriberGuard implements CanActivate { +export class ConnectSubscriberGuard implements CanActivate, CanActivateChild { constructor(private router: Router, private userManagementService: UserManagementService) { } - check(community: string, path: string): Observable { + check(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; - let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); - if (communityDomain) { - community = communityDomain; + let community; + if(properties.isDashboard) { + community = route.params['community']; + } else { + community = ConnectHelper.getCommunityFromDomain(properties.domain); } const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => { if (user) { @@ -35,7 +44,7 @@ export class ConnectSubscriberGuard implements CanActivate { this.router.navigate(['/user-info'], { queryParams: { 'errorCode': errorCode, - 'redirectUrl': path + 'redirectUrl': state.url } }) }); @@ -43,7 +52,10 @@ export class ConnectSubscriberGuard implements CanActivate { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route.queryParams['communityId'], state.url); + return this.check(route, state); } + canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + return this.check(childRoute, state); + } } diff --git a/connect/communityGuard/isCommunity.guard.ts b/connect/communityGuard/isCommunity.guard.ts index a1cae9cc..53dea6a5 100644 --- a/connect/communityGuard/isCommunity.guard.ts +++ b/connect/communityGuard/isCommunity.guard.ts @@ -25,10 +25,7 @@ export class IsCommunity implements CanActivate, CanActivateChild { if(properties.isDashboard) { community = route.params['community']; } else { - community = route.queryParams['communityId']; - if(!community) { - community = ConnectHelper.getCommunityFromDomain(properties.domain); - } + community = ConnectHelper.getCommunityFromDomain(properties.domain); } if (community) { return this.communityService.getCommunity(community).pipe(map(community => { @@ -40,7 +37,7 @@ export class IsCommunity implements CanActivate, CanActivateChild { } })); } else { - this.router.navigate(['error']); + this.router.navigate(['error'], {queryParams: {page: state.url}}); return false; } } diff --git a/connect/connectHelper.ts b/connect/connectHelper.ts index f4638d92..5990419c 100644 --- a/connect/connectHelper.ts +++ b/connect/connectHelper.ts @@ -4,7 +4,7 @@ import {properties} from "../../../environments/environment"; export class ConnectHelper { public static getCommunityFromDomain(domain: string): string{ - // domain = "beta.egi.openaire.eu"; //for testing + domain = "beta.egi.openaire.eu"; //for testing domain = domain.indexOf("//") != -1? domain.split("//")[1]:domain; //remove https:// prefix if (domain.indexOf('openaire.eu') === -1) { return null; diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index 1566b87f..8421265a 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -1,69 +1,27 @@ -import {Observable, Subscription} from 'rxjs'; -import {tap, take} from 'rxjs/operators'; -import { Injectable } from '@angular/core'; -import { - Router, - CanActivate, - ActivatedRouteSnapshot, - RouterStateSnapshot, - Data -} from '@angular/router'; - -import { ConfigurationService } from '../utils/configuration/configuration.service'; +import {Observable} from 'rxjs'; +import {take, tap} from 'rxjs/operators'; +import {Injectable} from '@angular/core'; +import {ActivatedRouteSnapshot, CanActivate, Data, Router, RouterStateSnapshot} from '@angular/router'; +import {ConfigurationService} from '../utils/configuration/configuration.service'; import {ConnectHelper} from '../connect/connectHelper'; import {properties} from "../../../environments/environment"; @Injectable() export class IsRouteEnabled implements CanActivate { - sub: Subscription = null; - + constructor(private router: Router, private config: ConfigurationService) {} - // check(data: Data, community: string, path: string): Observable | boolean { - // const customRedirect = data['redirect']; - // - // const redirect = customRedirect ? customRedirect : '/error'; - // const obs = this.propertiesService.subscribeEnvironment().pipe(map(res => { - // let communityDomain = null; - // //if (!community) { - // communityDomain = ConnectHelper.getCommunityFromDomain(res.domain); - // //} - // if(communityDomain) { - // community = communityDomain; - // } else if (!community && data['community']) { // for openaire or connect - // community = data['community']; - // } - // return res; - // }),mergeMap(prop => { - // if (!community) { - // community = prop.adminToolsCommunity; - // } - // return this.config.isPageEnabled(prop, community, '/' + path); - // }),); - // console.log("check isRouteEnabled.guard : call isPageEnabled"); - // - // obs.pipe(filter(enabled => !enabled)) - // .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}})); - // return obs; - // } - - check(data: Data, community: string, path: string): Observable | boolean { + check(data: Data, path: string): Observable | boolean { const customRedirect = data['redirect']; - const redirect = customRedirect ? customRedirect : '/error'; - let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); - - if(communityDomain) { - community = communityDomain; - } else if (!community && data['community']) { // for openaire or connect + let community = ConnectHelper.getCommunityFromDomain(properties.domain); + if (!community && data['community']) { // for openaire or connect community = data['community']; } - if (!community) { community = properties.adminToolsCommunity; } - return this.config.isPageEnabledByState(properties, community, '/'+path).pipe(take(1),tap((enabled) => { if(!enabled){ this.router.navigate([redirect], {queryParams: {'page': path}}); @@ -73,7 +31,7 @@ export class IsRouteEnabled implements CanActivate { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route.data, route.queryParams['communityId'], state.url); + return this.check(route.data, state.url); } } diff --git a/utils/properties/environment-specific.service.ts b/utils/properties/environment-specific.service.ts index d8b7e0c2..482ef7ff 100644 --- a/utils/properties/environment-specific.service.ts +++ b/utils/properties/environment-specific.service.ts @@ -1,21 +1,16 @@ import {Inject, Injectable, Optional} from '@angular/core'; import {HttpClient} from "@angular/common/http"; -import {BehaviorSubject, of} from 'rxjs'; import {Request} from 'express'; import {REQUEST} from '@nguniversal/express-engine/tokens'; - -import {EnvProperties} from './env-properties'; import {properties} from "../../../../environments/environment"; import {ConnectHelper} from "../../connect/connectHelper"; -@Injectable() +@Injectable({ + providedIn: 'root' +}) export class EnvironmentSpecificService { - public envSpecific: EnvProperties; - public domain: string; - private envSpecificSubject: BehaviorSubject = new BehaviorSubject(null); - constructor(private http: HttpClient, @Optional() @Inject(REQUEST) private request: Request) { if(properties.adminToolsCommunity == "connect") { if(properties.environment == "development"){ @@ -32,21 +27,7 @@ export class EnvironmentSpecificService { } } - public loadEnvironment() { - this.envSpecific = properties; - 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) { - this.envSpecific = properties; - } - // console.log('subscribeEnvironment: already loaded '); - return of(this.envSpecific); - } - - getDomain() { + private getDomain() { var domain = ""; if (typeof document == 'undefined') { domain = this.request.get('host').split(":")[0]; @@ -55,28 +36,4 @@ export class EnvironmentSpecificService { } return domain; } - - public setEnvProperties(es: EnvProperties) { - // This has already been set so bail out. - if (es === null || es === undefined) { - return; - } - this.envSpecific = es; - if (this.envSpecificSubject) { - this.envSpecificSubject.next(this.envSpecific); - } - } - - /* - Call this if you want to know when EnvProperties is set. - */ - public subscribe(caller: any, callback: (caller: any, es: EnvProperties) => void) { - this.envSpecificSubject - .subscribe((es) => { - if (es === null) { - return; - } - callback(caller, es); - }); - } } diff --git a/utils/properties/environmentSpecificResolver.ts b/utils/properties/environmentSpecificResolver.ts deleted file mode 100644 index c11afdfe..00000000 --- a/utils/properties/environmentSpecificResolver.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Router, Resolve, RouterStateSnapshot, - ActivatedRouteSnapshot } from '@angular/router'; - -import { EnvProperties } from './env-properties'; -import { EnvironmentSpecificService } from './environment-specific.service'; - -@Injectable() -export class EnvironmentSpecificResolver implements Resolve { - constructor(private envSpecificSvc: EnvironmentSpecificService, private router: Router) {} - - resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { - return this.envSpecificSvc.loadEnvironment() - .then(es => { - - return es; - }, error => { - console.log(error); - return null; - }); - } -}