From 10051a303e7e4481dfbfaad6faa06f4d5118789b Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Thu, 7 Mar 2019 15:43:54 +0000 Subject: [PATCH] [Trunk|Library]: connectCommunityGuard has been added it for check if type is community. Change community error page message git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@54965 d315682c-612b-4755-9ff5-7f18f6832af3 --- connect/community/community.service.ts | 94 ++++++++++--------- .../communityErrorPage.component.ts | 18 ++-- .../connectAdminLoginGuard.guard.ts | 2 +- .../connectCommunityGuard.guard.ts | 40 ++++++++ .../communityGuard/connectRIGuard.guard.ts | 36 ++----- connect/communityGuard/isCommunity.guard.ts | 1 - connect/connectHelper.ts | 2 +- error/isRouteEnabled.guard.ts | 8 +- login/utils/guardHelper.class.ts | 24 ++--- 9 files changed, 119 insertions(+), 106 deletions(-) create mode 100644 connect/communityGuard/connectCommunityGuard.guard.ts diff --git a/connect/community/community.service.ts b/connect/community/community.service.ts index 213b6bab..aa718b67 100644 --- a/connect/community/community.service.ts +++ b/connect/community/community.service.ts @@ -1,49 +1,55 @@ import { Injectable } from '@angular/core'; import { Http, Headers, RequestOptions } from '@angular/http'; import { CommunityInfo } from './communityInfo'; -import{EnvProperties} from '../../utils/properties/env-properties'; +import {EnvProperties} from '../../utils/properties/env-properties'; @Injectable() export class CommunityService { - constructor(private http:Http) { + constructor(private http: Http) { } - getCommunity(properties:EnvProperties, url: string) { - return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url) + getCommunity(properties: EnvProperties, url: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) .map(res => res.json()).map(res => this.parseCommunity(res)); } - updateCommunity(url: string, community:any) { - let headers = new Headers({'Content-Type': 'application/json'}); - let options = new RequestOptions({headers: headers}); - - let body = JSON.stringify(community); - - - return this.http.post(url, body, options) + updateCommunity(url: string, community: any) { + const headers = new Headers({'Content-Type': 'application/json'}); + const options = new RequestOptions({headers: headers}); + const body = JSON.stringify(community); + return this.http.post(url, body, options); /*.map(res => res.json())*/ } - iscommunityManager(properties:EnvProperties, url: string, manager:string){ - return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url) - .map(res => res.json()).map(res => this.parseCommunity(res)).map(community => community.managers.indexOf(manager)!=-1); + isCommunityManager(properties: EnvProperties, url: string, manager: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + .map(res => res.json()).map(res => + this.parseCommunity(res)).map(community => community.managers.indexOf(manager) !== -1); } - iscommunityRI(properties:EnvProperties, url: string){ - return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url) - .map(res => res.json()).map(res => this.parseCommunity(res)).map(community => (community && community.type && community.type !="community")); + isRIType(properties: EnvProperties, url: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + .map(res => res.json()).map(res => + this.parseCommunity(res)).map(community => + (community && community.type && community.type === 'ri')); } - isSubscribedToCommunity(pid:string, email:string, url:string){ - return this.http.get(url+"/community/"+pid+"/subscribers") - .map(res => ((res =="")?{}: res.json())) + isCommunityType(properties: EnvProperties, url: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + .map(res => res.json()).map(res => + this.parseCommunity(res)).map(community => + (community && community.type && community.type === 'community')); + } + isSubscribedToCommunity(pid: string, email: string, url: string) { + return this.http.get(url + '/community/' + pid + '/subscribers') + .map(res => ((res === '') ? {} : res.json())) .map(res => { - if(res.subscribers && res.subscribers != null){ + if (res.subscribers && res.subscribers != null) { - for(var i =0; i< res.subscribers.length; i++ ){ - if(res.subscribers[i]!=null && res.subscribers[i].email == email){ + for (let i = 0; i < res.subscribers.length; i++ ) { + if (res.subscribers[i] != null && res.subscribers[i].email === email) { return true; } } @@ -53,11 +59,11 @@ export class CommunityService { }); } - private parseCommunity(data:any): CommunityInfo { + private parseCommunity(data: any): CommunityInfo { - let resData = Array.isArray(data) ? data[0] : data; + const resData = Array.isArray(data) ? data[0] : data; - let community: CommunityInfo = new CommunityInfo(); + const community: CommunityInfo = new CommunityInfo(); community['title'] = resData.name; community['shortTitle'] = resData.shortName; community['communityId'] = resData.id; @@ -66,42 +72,42 @@ export class CommunityService { community['description'] = resData.description; community['date'] = resData.creationDate; community['zenodoCommunity'] = resData.zenodoCommunity; - community['status'] = "all"; - if(resData.hasOwnProperty('status')){ + community['status'] = 'all'; + if (resData.hasOwnProperty('status')) { community['status'] = resData.status; - let status = ["all","hidden","manager"]; - if(status.indexOf(community['status']) ==-1){ - community['status'] = "hidden"; + const status = ['all', 'hidden', 'manager']; + if (status.indexOf(community['status']) === -1) { + community['status'] = 'hidden'; } } if (resData.type != null) { community['type'] = resData.type; } - if(resData.managers != null) { - if(community['managers'] == undefined) { + if (resData.managers != null) { + if (community['managers'] === undefined) { community['managers'] = new Array(); } - let managers = resData.managers; - let length = Array.isArray(managers) ? managers.length : 1; + const managers = resData.managers; + const length = Array.isArray(managers) ? managers.length : 1; - for(let i=0; i(); } - let subjects = resData.subjects; - let length = Array.isArray(subjects) ? subjects.length : 1; + const subjects = resData.subjects; + const length = Array.isArray(subjects) ? subjects.length : 1; - for(let i=0; i

- There is no community selected! + There is no community selected or the requested page is not available at this type of community!


@@ -25,19 +25,17 @@ import {Title, Meta} from '@angular/platform-browser'; ` }) -export class CommunityErrorPageComponent { +export class CommunityErrorPageComponent implements OnInit { public page: string; constructor (private _location: Location, private _meta: Meta, private _title: Title, private route: ActivatedRoute) { - var title = "OpenAIRE | Error page"; + const title = 'OpenAIRE | Error page'; - this._meta.updateTag({content:title},"property='og:title'"); + this._meta.updateTag({content: title}, "property='og:title'"); this._title.setTitle(title); this.page = _location.path(true); - //this.page = _router.url; - //this.page = location.href; } ngOnInit() { diff --git a/connect/communityGuard/connectAdminLoginGuard.guard.ts b/connect/communityGuard/connectAdminLoginGuard.guard.ts index 3dca0944..332a8ea2 100644 --- a/connect/communityGuard/connectAdminLoginGuard.guard.ts +++ b/connect/communityGuard/connectAdminLoginGuard.guard.ts @@ -26,7 +26,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad { return true; } else { const obs = this.propertiesService.subscribeEnvironment().map(res => res).mergeMap(properties => { - return this.communityService.iscommunityManager(properties, properties['communityAPI'] + community, Session.getUserEmail()); + return this.communityService.isCommunityManager(properties, properties['communityAPI'] + community, Session.getUserEmail()); }); obs.filter(enabled => !enabled) .subscribe(() => this.router.navigate(['/user-info'], { diff --git a/connect/communityGuard/connectCommunityGuard.guard.ts b/connect/communityGuard/connectCommunityGuard.guard.ts new file mode 100644 index 00000000..9f1e47c2 --- /dev/null +++ b/connect/communityGuard/connectCommunityGuard.guard.ts @@ -0,0 +1,40 @@ +import { Injectable } from '@angular/core'; +import { + Router, + CanActivate, + ActivatedRouteSnapshot, + RouterStateSnapshot, + CanLoad, + Route +} from '@angular/router'; +import {Observable} from 'rxjs/Observable'; +import {CommunityService} from '../community/community.service'; +import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; +import {ConnectHelper} from '../connectHelper'; + +@Injectable() +export class ConnectCommunityGuard implements CanActivate, CanLoad { + + constructor(private router: Router, + private communityService: CommunityService, + private propertiesService: EnvironmentSpecificService) { + } + + check(community: string): Observable | boolean { + const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { + return this.communityService.isCommunityType(properties, properties['communityAPI'] + community); + }); + obs.filter(enabled => !enabled) + .subscribe(() => this.router.navigate(['errorcommunity'])); + return obs; + } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { + return this.check(route.queryParams['communityId']); + } + + canLoad(route: Route): Observable | Promise | boolean { + const path = '/' + route.path + document.location.search; + return this.check(ConnectHelper.getCommunityFromPath(path)); + } +} diff --git a/connect/communityGuard/connectRIGuard.guard.ts b/connect/communityGuard/connectRIGuard.guard.ts index 29c9dc85..ddad2bcf 100644 --- a/connect/communityGuard/connectRIGuard.guard.ts +++ b/connect/communityGuard/connectRIGuard.guard.ts @@ -8,8 +8,6 @@ import { 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'; @@ -22,37 +20,21 @@ export class ConnectRIGuard implements CanActivate, CanLoad { private propertiesService: EnvironmentSpecificService) { } - check(community: string, path: string): Observable | boolean { - let errorCode = LoginErrorCodes.NOT_LOGIN; - if (Session.isLoggedIn()) { - if (Session.isPortalAdministrator()) { - return true; - } else { - const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { - return this.communityService.iscommunityRI(properties, properties['communityAPI'] + community); - }); - obs.filter(enabled => !enabled) - .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': path}}); - return false; - } + check(community: string): Observable | boolean { + const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { + return this.communityService.isRIType(properties, properties['communityAPI'] + community); + }); + obs.filter(enabled => !enabled) + .subscribe(() => this.router.navigate(['errorcommunity'])); + return obs; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { - return this.check(route.queryParams['communityId'], state.url); + return this.check(route.queryParams['communityId']); } canLoad(route: Route): Observable | Promise | boolean { const path = '/' + route.path + document.location.search; - return this.check(ConnectHelper.getCommunityFromPath(path), path); + return this.check(ConnectHelper.getCommunityFromPath(path)); } } diff --git a/connect/communityGuard/isCommunity.guard.ts b/connect/communityGuard/isCommunity.guard.ts index a7edc364..566714c2 100644 --- a/connect/communityGuard/isCommunity.guard.ts +++ b/connect/communityGuard/isCommunity.guard.ts @@ -31,7 +31,6 @@ export class IsCommunity implements CanActivate, CanLoad { canLoad(route: Route): Observable | Promise | boolean { const path = '/' + route.path + document.location.search; - console.log(path) return this.check(ConnectHelper.getCommunityFromPath(path)); } } diff --git a/connect/connectHelper.ts b/connect/connectHelper.ts index 30285c31..e9bb4924 100644 --- a/connect/connectHelper.ts +++ b/connect/connectHelper.ts @@ -15,7 +15,7 @@ export class ConnectHelper { } else { domain = domain.substr(0, domain.indexOf('.')); } - if(domain == "connect" || domain == "explore" || domain == "monitor"){ + if (domain === 'connect' || domain === 'explore' || domain === 'monitor'){ return null; } return domain; diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index 3c108a23..7e3fc80c 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -27,15 +27,15 @@ export class IsRouteEnabled implements CanActivate, CanLoad { } const redirect = customRedirect ? customRedirect : '/error'; const obs = this.propertiesService.subscribeEnvironment().map(res => { - if(!community){ + if (!community) { community = ConnectHelper.getCommunityFromDomain(res.domain); } - return res["adminToolsAPIURL"] + return res['adminToolsAPIURL']; }).mergeMap(url => { - if(!community){ // no community to check - return true + if (!community) { // no community to check - return true return Observable.of(true); } - return this.config.isPageEnabled(url, community,"/"+path.split("?")[0].substring(1)) + return this.config.isPageEnabled(url, community, '/' + path.split('?')[0].substring(1)); }); obs.filter(enabled => !enabled) .subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}})); diff --git a/login/utils/guardHelper.class.ts b/login/utils/guardHelper.class.ts index e6e8e30d..9d1d3f11 100644 --- a/login/utils/guardHelper.class.ts +++ b/login/utils/guardHelper.class.ts @@ -1,21 +1,9 @@ - -import { Router} from '@angular/router'; - -// export class GuardHelper{ -// constructor(private router: Router) {} -// -// redirect(url:string, errorCode:number, redirectUrl:string){ -// this.router.navigate([url], { queryParams: { "errorCode": errorCode, "redirectUrl": redirectUrl } }); -// -// } -// -// } export class LoginErrorCodes { - public static NOT_LOGIN:number =1; - public static NOT_ADMIN:number =2; - public static NOT_VALID:number =3; - public static NOT_CONNECT_ADMIN:number =4; - public static NO_COMMUNITY:number =5; - public static NOT_SUBSCRIBER:number =6; + public static NOT_LOGIN = 1; + public static NOT_ADMIN = 2; + public static NOT_VALID = 3; + public static NOT_CONNECT_ADMIN = 4; + public static NO_COMMUNITY = 5; + public static NOT_SUBSCRIBER = 6; }