diff --git a/connect/communityGuard/connectAdminLoginGuard.guard.ts b/connect/communityGuard/connectAdminLoginGuard.guard.ts index 6329212e..d00b8fce 100644 --- a/connect/communityGuard/connectAdminLoginGuard.guard.ts +++ b/connect/communityGuard/connectAdminLoginGuard.guard.ts @@ -1,5 +1,12 @@ import { Injectable } from '@angular/core'; -import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import { + Router, + CanActivate, + ActivatedRouteSnapshot, + RouterStateSnapshot, + ActivatedRoute, + CanLoad, Route +} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Session} from '../../login/utils/helper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; @@ -8,29 +15,44 @@ import { EnvironmentSpecificService} from '../../utils/properties/environment-sp import { mergeMap } from 'rxjs/operators'; @Injectable() -export class ConnectAdminLoginGuard implements CanActivate { - constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {} +export class ConnectAdminLoginGuard implements CanActivate, CanLoad{ + constructor(private router: Router, + private communityService: CommunityService, + private propertiesService:EnvironmentSpecificService, + private route: ActivatedRoute) {} - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { + check(path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - let community = (route.queryParams["communityId"]); - if(Session.isLoggedIn()){ - if(Session.isPortalAdministrator() || Session.isCommunityCurator()) { + let community = this.route.queryParams["communityId"]; + if (Session.isLoggedIn()) { + if (Session.isPortalAdministrator() || Session.isCommunityCurator()) { return true; } else { - let obs = this.propertiesService.subscribeEnvironment().map(res=>res).mergeMap(properties => { - return this.communityService.iscommunityManager(properties, properties["communityAPI"]+community,Session.getUserEmail())}); + let obs = this.propertiesService.subscribeEnvironment().map(res => res).mergeMap(properties => { + return this.communityService.iscommunityManager(properties, properties["communityAPI"] + community, Session.getUserEmail()) + }); obs.filter(enabled => !enabled) - .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } })); + .subscribe(() => this.router.navigate(['/user-info'], { + queryParams: { + "errorCode": errorCode, + "redirectUrl": path + } + })); return obs; - } - } else{ - errorCode =LoginErrorCodes.NOT_LOGIN; - this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }); + } + } 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(state.url); + } + canLoad(route: Route): Observable | Promise | boolean { + return this.check('/' + route.path); } } diff --git a/connect/communityGuard/connectRIGuard.guard.ts b/connect/communityGuard/connectRIGuard.guard.ts index 9044f1d7..3ed8f6ee 100644 --- a/connect/communityGuard/connectRIGuard.guard.ts +++ b/connect/communityGuard/connectRIGuard.guard.ts @@ -1,36 +1,56 @@ import { Injectable } from '@angular/core'; -import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import { + Router, + CanActivate, + ActivatedRouteSnapshot, + RouterStateSnapshot, + CanLoad, + Route, ActivatedRoute +} 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 { mergeMap } from 'rxjs/operators'; @Injectable() -export class ConnectRIGuard implements CanActivate { - constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {} +export class ConnectRIGuard implements CanActivate, CanLoad { - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { + constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService, private route:ActivatedRoute) {} + + check(path: string) : Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - let community = (route.queryParams["communityId"]); - if(Session.isLoggedIn()){ + let community = (this.route.queryParams["communityId"]); + + if(Session.isLoggedIn()) { if(Session.isPortalAdministrator()) { return true; } else { let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { - return this.communityService.iscommunityRI(properties, properties["communityAPI"]+community)}); + return this.communityService.iscommunityRI(properties, properties["communityAPI"] + community) + }); obs.filter(enabled => !enabled) - .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } })); + .subscribe(() => this.router.navigate(['/user-info'], { + queryParams: { + "errorCode": errorCode, + "redirectUrl": path + } + })); return obs; - } + } } else{ errorCode = LoginErrorCodes.NOT_LOGIN; - this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }); + this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": path } }); return false; } + } + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { + return this.check(state.url); + } + canLoad(route: Route): Observable | Promise | boolean { + return this.check('/' + route.path); } } diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index b68f3fbb..cbf2acbf 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -1,24 +1,22 @@ import { Injectable } from '@angular/core'; -import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute} 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 { mergeMap } from 'rxjs/operators'; import {ConnectHelper} from '../connectHelper'; @Injectable() export class ConnectSubscriberGuard implements CanActivate { - constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {} + constructor(private router: Router, + private communityService: CommunityService, + private propertiesService:EnvironmentSpecificService, + private route: ActivatedRoute) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - //console.log(state.url); - var user; - var authorized = false; - var errorCode = LoginErrorCodes.NOT_SUBSCRIBER; - - let community = (route.queryParams["communityId"]); + let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; + let community = this.route.queryParams["communityId"]; if(!community && (typeof document !== 'undefined')){ community = ConnectHelper.getCommunityFromDomain(document.location.hostname); } @@ -27,19 +25,19 @@ export class ConnectSubscriberGuard implements CanActivate { this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }); return false; } - // if(Session.isPortalAdministrator() || Session.isCommunityCurator() || Session.isClaimsCurator()){ - // authorized = true; - // return true; - // - // }else { - - let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { - return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"])}); - obs.filter(enabled => !enabled) - .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } })); - return obs; - // } - + else { + let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { + return this.communityService.isSubscribedToCommunity(community, Session.getUserEmail(), properties["adminToolsAPIURL"]) + }); + obs.filter(enabled => !enabled) + .subscribe(() => this.router.navigate(['/user-info'], { + queryParams: { + "errorCode": errorCode, + "redirectUrl": state.url + } + })); + return obs; + } } } diff --git a/connect/communityGuard/isCommunity.guard.ts b/connect/communityGuard/isCommunity.guard.ts index fe6aa6a5..8966c95a 100644 --- a/connect/communityGuard/isCommunity.guard.ts +++ b/connect/communityGuard/isCommunity.guard.ts @@ -1,20 +1,36 @@ import { Injectable } from '@angular/core'; -import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import { + ActivatedRoute, + Router, + CanActivate, + ActivatedRouteSnapshot, + RouterStateSnapshot, + CanLoad, Route +} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/filter'; @Injectable() -export class IsCommunity implements CanActivate { +export class IsCommunity implements CanActivate, CanLoad { - constructor(private route: ActivatedRoute,private router: Router) {} + constructor(private router: Router, + private route: ActivatedRoute) {} + + check(): Observable | boolean { + let community = this.route.queryParams["communityId"]; + if(community && community!="undefined"){ + return true; + }else{ + this.router.navigate(['errorcommunity']); + } + return false; + } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - let community = (route.queryParams["communityId"]); - if(community && community!="undefined"){ - return true; - }else{ - this.router.navigate(['errorcommunity']); - } - return false; - } + return this.check(); } + + canLoad(route: Route): Observable | Promise | boolean { + return this.check(); + } +} diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index e916776a..b230016d 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -1,5 +1,13 @@ import { Injectable } from '@angular/core'; -import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import { + ActivatedRoute, + Router, + CanActivate, + CanLoad, + ActivatedRouteSnapshot, + RouterStateSnapshot, + Route +} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/filter'; import { ConfigurationService } from '../utils/configuration/configuration.service'; @@ -7,29 +15,35 @@ import { EnvironmentSpecificService} from '../utils/properties/environment-speci import {ConnectHelper} from '../connect/connectHelper'; @Injectable() -export class IsRouteEnabled implements CanActivate { +export class IsRouteEnabled implements CanActivate, CanLoad { constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService, private propertiesService:EnvironmentSpecificService ) {} + check(path: string): Observable | boolean { + let customRedirect = this.route.data['redirect']; + let community = this.route.queryParams["communityId"]; + if(!community && this.route.data['community']){ // for openaire + community = this.route.data['community']; + } + if(!community && typeof document != 'undefined'){ + community = ConnectHelper.getCommunityFromDomain(document.location.hostname); + } + if(community){ + let redirect = customRedirect ? customRedirect : '/error'; + let obs = this.propertiesService.subscribeEnvironment().map(res=>res["adminToolsAPIURL"]).mergeMap(url => { + 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(state.url); + } - let customRedirect = route.data['redirect']; - let community = route.queryParams["communityId"]; - if(!community && route.data['community']){ // for openaire - let community = route.data['community'] - } - if(!community && typeof document != 'undefined'){ - community = ConnectHelper.getCommunityFromDomain(document.location.hostname); - } - - if(community){ - let redirect = !!customRedirect ? customRedirect : '/error'; - let obs = this.propertiesService.subscribeEnvironment().map(res=>res["adminToolsAPIURL"]).mergeMap(url => { - return this.config.isPageEnabled(url, community,"/"+state.url.split("?")[0].substring(1))}); - obs.filter(enabled => !enabled) - .subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } })); - return obs; - } - return true; - } + canLoad(route: Route): Observable | Promise | boolean { + return this.check('/' + route.path); + } } diff --git a/utils/configuration/configuration.service.ts b/utils/configuration/configuration.service.ts index 6e406efc..601879a3 100644 --- a/utils/configuration/configuration.service.ts +++ b/utils/configuration/configuration.service.ts @@ -32,7 +32,7 @@ export class ConfigurationService { isPageEnabled(APIUrl:string, community:string,router: string){ return this.http.get(APIUrl + "/community/" + community+"/pages?page_route="+router) .map(res => res.json()).map(res => { - var result = false; + let result = false; if(res.length >0 && res[0].route == router){ result = res[0].isEnabled; }