[Trunk|Library]: 1. Add static function to find community from url path. 2. Community guards can now be used as CanLoad guards using functionality of getCommunityFromPath(look at 1).

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@54915 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2019-02-27 10:39:13 +00:00
parent bc9801aeb0
commit 5d5f39be19
6 changed files with 52 additions and 42 deletions

View File

@ -4,7 +4,6 @@ import {
CanActivate, CanActivate,
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
RouterStateSnapshot, RouterStateSnapshot,
ActivatedRoute,
CanLoad, Route CanLoad, Route
} from '@angular/router'; } from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
@ -12,16 +11,16 @@ import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service'; import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@Injectable() @Injectable()
export class ConnectAdminLoginGuard implements CanActivate, CanLoad{ export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
constructor(private router: Router, constructor(private router: Router,
private communityService: CommunityService, private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {} private propertiesService: EnvironmentSpecificService) {}
check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
const community = route.queryParams['communityId'];
if (Session.isLoggedIn()) { if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator() || Session.isCommunityCurator()) { if (Session.isPortalAdministrator() || Session.isCommunityCurator()) {
return true; return true;
@ -47,10 +46,11 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad{
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route, state.url); return this.check(route.queryParams['communityId'], state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path); const path = '/' + route.path;
return this.check(ConnectHelper.getCommunityFromPath(path), path);
} }
} }

View File

@ -5,13 +5,14 @@ import {
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
RouterStateSnapshot, RouterStateSnapshot,
CanLoad, CanLoad,
Route, ActivatedRoute Route
} from '@angular/router'; } from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {Session} from '../../login/utils/helper.class'; import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service'; import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@Injectable() @Injectable()
export class ConnectRIGuard implements CanActivate, CanLoad { export class ConnectRIGuard implements CanActivate, CanLoad {
@ -21,10 +22,8 @@ export class ConnectRIGuard implements CanActivate, CanLoad {
private propertiesService: EnvironmentSpecificService) { private propertiesService: EnvironmentSpecificService) {
} }
check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
const community = (route.queryParams['communityId']);
if (Session.isLoggedIn()) { if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator()) { if (Session.isPortalAdministrator()) {
return true; return true;
@ -44,16 +43,16 @@ export class ConnectRIGuard implements CanActivate, CanLoad {
} else { } else {
errorCode = LoginErrorCodes.NOT_LOGIN; errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}}); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
return false; return false;
} }
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route, state.url); return this.check(route.queryParams['communityId'], state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path); const path = '/' + route.path;
return this.check(ConnectHelper.getCommunityFromPath(path), path);
} }
} }

View File

@ -8,14 +8,13 @@ import { EnvironmentSpecificService} from '../../utils/properties/environment-sp
import {ConnectHelper} from '../connectHelper'; import {ConnectHelper} from '../connectHelper';
@Injectable() @Injectable()
export class ConnectSubscriberGuard implements CanActivate, CanLoad{ export class ConnectSubscriberGuard implements CanActivate, CanLoad {
constructor(private router: Router, constructor(private router: Router,
private communityService: CommunityService, private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {} private propertiesService: EnvironmentSpecificService) {}
check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
let community = route.queryParams['communityId'];
if (!community && (typeof document !== 'undefined')) { if (!community && (typeof document !== 'undefined')) {
community = ConnectHelper.getCommunityFromDomain(document.location.hostname); community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
} }
@ -39,11 +38,12 @@ export class ConnectSubscriberGuard implements CanActivate, CanLoad{
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route, state.url); return this.check(route.queryParams['communityId'], state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path); const path = '/' + route.path;
return this.check(ConnectHelper.getCommunityFromPath(path), path);
} }
} }

View File

@ -8,6 +8,7 @@ import {
} from '@angular/router'; } from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/filter';
import {ConnectHelper} from '../connectHelper';
@Injectable() @Injectable()
export class IsCommunity implements CanActivate, CanLoad { export class IsCommunity implements CanActivate, CanLoad {
@ -15,9 +16,7 @@ export class IsCommunity implements CanActivate, CanLoad {
constructor(private router: Router) { constructor(private router: Router) {
} }
check(route: ActivatedRouteSnapshot): Observable<boolean> | boolean { check(community: string): Observable<boolean> | boolean {
const community = route.queryParams['communityId'];
console.log(community);
if (community && community !== 'undefined') { if (community && community !== 'undefined') {
return true; return true;
} else { } else {
@ -27,10 +26,11 @@ export class IsCommunity implements CanActivate, CanLoad {
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route); return this.check(route.queryParams['communityId']);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null); const path = '/' + route.path;
return this.check(ConnectHelper.getCommunityFromPath(path));
} }
} }

View File

@ -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 // domain = "beta.egi.openaire.eu"; for testing
if(domain.indexOf("openaire.eu") == -1){ if (domain.indexOf('openaire.eu') === -1) {
return null; return null;
} }
if(domain.indexOf("beta")!=-1){ if ( domain.indexOf('beta') !== -1) {
domain = domain.substr(domain.indexOf(".")+1,domain.length); domain = domain.substr(domain.indexOf('.') + 1, domain.length);
domain = domain.substr(0,domain.indexOf(".")); domain = domain.substr(0, domain.indexOf('.'));
}else if(domain.indexOf("test.")!=-1){ } else if (domain.indexOf('test.') !== -1) {
return null; return null;
}else{ } else {
domain = domain.substr(0,domain.indexOf(".")); domain = domain.substr(0, domain.indexOf('.'));
} }
if(domain == "connect" || domain == "explore"){ if (domain === 'connect' || domain === 'explore') {
return null; return null;
} }
return domain; 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;
}
}
} }

View File

@ -5,7 +5,7 @@ import {
CanLoad, CanLoad,
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
RouterStateSnapshot, RouterStateSnapshot,
Route Route, Data
} from '@angular/router'; } from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/filter';
@ -18,13 +18,12 @@ export class IsRouteEnabled implements CanActivate, CanLoad {
constructor(private router: Router, constructor(private router: Router,
private config: ConfigurationService, private config: ConfigurationService,
private propertiesService: EnvironmentSpecificService ) {} private propertiesService: EnvironmentSpecificService) {}
check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean { check(data: Data, community: string, path: string): Observable<boolean> | boolean {
const customRedirect = route.data['redirect']; const customRedirect = data['redirect'];
let community = route.queryParams['communityId']; if (!community && data['community']) { // for openaire
if (!community && route.data['community']) { // for openaire community = data['community'];
community = route.data['community'];
} }
if (!community && typeof document !== 'undefined') { if (!community && typeof document !== 'undefined') {
community = ConnectHelper.getCommunityFromDomain(document.location.hostname); community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
@ -42,10 +41,11 @@ export class IsRouteEnabled implements CanActivate, CanLoad {
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route, state.url); return this.check(route.data, route.queryParams['communityId'], state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path); const path = '/' + route;
return this.check(route.data, ConnectHelper.getCommunityFromPath(path), path);
} }
} }