[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,
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> | boolean {
check(community: string, path: string): Observable<boolean> | 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> | boolean {
return this.check(route, state.url);
return this.check(route.queryParams['communityId'], state.url);
}
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,
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> | boolean {
check(community: string, path: string): Observable<boolean> | 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> | boolean {
return this.check(route, state.url);
return this.check(route.queryParams['communityId'], state.url);
}
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';
@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> | boolean {
check(community: string, path: string): Observable<boolean> | 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> | boolean {
return this.check(route, state.url);
return this.check(route.queryParams['communityId'], state.url);
}
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';
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> | boolean {
const community = route.queryParams['communityId'];
console.log(community);
check(community: string): Observable<boolean> | 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> | boolean {
return this.check(route);
return this.check(route.queryParams['communityId']);
}
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
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;
}
}
}

View File

@ -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> | 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> | 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> | 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 {
return this.check(null, '/' + route.path);
const path = '/' + route;
return this.check(route.data, ConnectHelper.getCommunityFromPath(path), path);
}
}