diff --git a/connect/community/community.service.ts b/connect/community/community.service.ts index e485c8f2..c21c68b1 100644 --- a/connect/community/community.service.ts +++ b/connect/community/community.service.ts @@ -1,132 +1,206 @@ import { Injectable } from '@angular/core'; -import { Http, Headers, RequestOptions } from '@angular/http'; import {HttpClient, HttpHeaders} from "@angular/common/http"; import { CommunityInfo } from './communityInfo'; import {EnvProperties} from '../../utils/properties/env-properties'; import {map} from "rxjs/operators"; -import {COOKIE} from "../../login/utils/helper.class"; +import {BehaviorSubject, from} from "rxjs"; -@Injectable() +@Injectable({ providedIn: 'root' }) export class CommunityService { - constructor(private http: HttpClient) { - } + public community: BehaviorSubject = null; + private promise: Promise = null; - getCommunity(properties: EnvProperties, url: string) { - return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) - //.map(res => res.json()) - .pipe(map(res => this.parseCommunity(res))); + constructor(private http: HttpClient) { + this.community = new BehaviorSubject(null); + } + + getCommunityByService(properties: EnvProperties, url: string) { + console.debug("getCommunityByService"); + this.promise = new Promise(resolve => { + this.getCommunity(properties, url).subscribe(res => { + this.community.next(res); + resolve(); + }, + error => { + this.community.next(null); + resolve(); + }) + }); + } + + async getCommunityByStateAsync(properties: EnvProperties, url: string) { + if(!this.promise) { + this.getCommunityByService(properties, url); } - updateCommunity(url: string, community: any) { - //const headers = new Headers({'Content-Type': 'application/json'}); - //const options = new RequestOptions({headers: headers}); + await this.promise; + return this.community.getValue(); + } - const options = { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - }) - }; + getCommunityByState(properties: EnvProperties, url: string) { + return from(this.getCommunityByStateAsync(properties, url)); + } - const body = JSON.stringify(community); - return this.http.post(url, body, options); - /*.map(res => res.json())*/ - } - - isCommunityManager(properties: EnvProperties, url: string, manager: string) { + /** + * @deprecated + */ + getCommunity(properties: EnvProperties, url: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) - .pipe(map(res => this.parseCommunity(res))) - .pipe(map(community => community.managers.indexOf(manager) !== -1)); + .pipe(map(res => this.parseCommunity(res))); + } + + updateCommunity(url: string, community: any) { + //const headers = new Headers({'Content-Type': 'application/json'}); + //const options = new RequestOptions({headers: headers}); + + const options = { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + }) + }; + + const body = JSON.stringify(community); + return this.http.post(url, body, options); + /*.map(res => res.json())*/ + } + + async isCommunityManagerByStateAsync(properties: EnvProperties, url: string, manager: string) { + if(!this.promise) { + this.getCommunityByService(properties, url); } - isRIType(properties: EnvProperties, url: string) { + await this.promise; + let community: CommunityInfo = this.community.getValue(); + return (community.managers.indexOf(manager) !== -1); + } + + isCommunityManagerByState(properties: EnvProperties, url: string, manager: string) { + return from(this.isCommunityManagerByStateAsync(properties, url, manager)); + } + + /** + * @deprecated + */ + isCommunityManager(properties: EnvProperties, url: string, manager: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + //.map(res => res.json()) + .pipe(map(res => this.parseCommunity(res))) + .pipe(map(community => community.managers.indexOf(manager) !== -1)); + } + + async isTypeByStateAsync(properties: EnvProperties, url: string, type: string) { + if(!this.promise) { + this.getCommunityByService(properties, url); + } + + await this.promise; + let community: CommunityInfo = this.community.getValue(); + return (community && community.type && community.type === type); + } + + isRITypeByState(properties: EnvProperties, url: string) { + return from(this.isTypeByStateAsync(properties, url, "ri")); + } + + isCommunityTypeByState(properties: EnvProperties, url: string) { + return from(this.isTypeByStateAsync(properties, url, "community")); + } + + /** + * @deprecated + */ + isRIType(properties: EnvProperties, url: string) { + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) + //.map(res => res.json()) + .pipe(map(res => this.parseCommunity(res))) + .pipe(map(community => (community && community.type && community.type === 'ri'))); + } + + /** + * @deprecated + */ + isCommunityType(properties: EnvProperties, url: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) .pipe(map(res => this.parseCommunity(res))) - .pipe(map(community => (community && community.type && community.type === 'ri'))); - } + .pipe(map(community => (community && community.type && community.type === 'community'))); + } - isCommunityType(properties: EnvProperties, url: string) { - return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) - //.map(res => res.json()) - .pipe(map(res => this.parseCommunity(res))) - .pipe(map(community => (community && community.type && community.type === 'community'))); - } + /** + * @deprecated + */ + isSubscribedToCommunity(pid: string, email: string, url: string) { + return this.http.get(url + '/community/' + pid + '/subscribers') + //.map(res => ((res === '') ? {} : res.json())) + .pipe(map(res => { + if (res['subscribers'] && res['subscribers'] != null) { - /** - * @deprecated - */ - isSubscribedToCommunity(pid: string, email: string, url: string) { - return this.http.get(url + '/community/' + pid + '/subscribers') - //.map(res => ((res === '') ? {} : res.json())) - .pipe(map(res => { - if (res['subscribers'] && res['subscribers'] != null) { - - for (let i = 0; i < res['subscribers'].length; i++ ) { - if (res['subscribers'][i] != null && res['subscribers'][i].email === email) { - return true; - } + for (let i = 0; i < res['subscribers'].length; i++ ) { + if (res['subscribers'][i] != null && res['subscribers'][i].email === email) { + return true; } } - return false; + } + return false; - })); - } + })); + } - private parseCommunity(data: any): CommunityInfo { + private parseCommunity(data: any): CommunityInfo { - const resData = Array.isArray(data) ? data[0] : data; + const resData = Array.isArray(data) ? data[0] : data; - const community: CommunityInfo = new CommunityInfo(); - community['title'] = resData.name; - community['shortTitle'] = resData.shortName; - community['communityId'] = resData.id; - community['queryId'] = resData.queryId; - community['logoUrl'] = resData.logoUrl; - community['description'] = resData.description; - community['date'] = resData.creationDate; - community['zenodoCommunity'] = resData.zenodoCommunity; - community['status'] = 'all'; - if (resData.hasOwnProperty('status')) { - community['status'] = resData.status; - const status = ['all', 'hidden', 'manager']; - if (status.indexOf(community['status']) === -1) { - community['status'] = 'hidden'; + const community: CommunityInfo = new CommunityInfo(); + community['title'] = resData.name; + community['shortTitle'] = resData.shortName; + community['communityId'] = resData.id; + community['queryId'] = resData.queryId; + community['logoUrl'] = resData.logoUrl; + community['description'] = resData.description; + community['date'] = resData.creationDate; + community['zenodoCommunity'] = resData.zenodoCommunity; + community['status'] = 'all'; + if (resData.hasOwnProperty('status')) { + community['status'] = resData.status; + 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) { + community['managers'] = new Array(); } - } - if (resData.type != null) { - community['type'] = resData.type; - } - if (resData.managers != null) { - if (community['managers'] === undefined) { - community['managers'] = new Array(); - } + const managers = resData.managers; + const length = Array.isArray(managers) ? managers.length : 1; - const managers = resData.managers; - const length = Array.isArray(managers) ? managers.length : 1; + for (let i = 0; i < length; i++) { + const manager = Array.isArray(managers) ? managers[i] : managers; + community.managers[i] = manager; + } + } - for (let i = 0; i < length; i++) { - const manager = Array.isArray(managers) ? managers[i] : managers; - community.managers[i] = manager; - } - } + if (resData.subjects != null) { + if (community['subjects'] === undefined) { + community['subjects'] = new Array(); + } - if (resData.subjects != null) { - if (community['subjects'] === undefined) { - community['subjects'] = new Array(); - } + const subjects = resData.subjects; + const length = Array.isArray(subjects) ? subjects.length : 1; - const subjects = resData.subjects; - const length = Array.isArray(subjects) ? subjects.length : 1; - - for (let i = 0; i < length; i++) { - const subject = Array.isArray(subjects) ? subjects[i] : subjects; - community.subjects[i] = subject; - } - } - return community; - } + for (let i = 0; i < length; i++) { + const subject = Array.isArray(subjects) ? subjects[i] : subjects; + community.subjects[i] = subject; + } + } + return community; + } } diff --git a/connect/communityGuard/connectAdminLoginGuard.guard.ts b/connect/communityGuard/connectAdminLoginGuard.guard.ts index 73fb7cd8..b28ae848 100644 --- a/connect/communityGuard/connectAdminLoginGuard.guard.ts +++ b/connect/communityGuard/connectAdminLoginGuard.guard.ts @@ -7,7 +7,7 @@ import { RouterStateSnapshot, CanLoad, Route, UrlSegment } from '@angular/router'; -import {Observable, of} from 'rxjs'; +import {Observable, of, Subscription} from 'rxjs'; import {Session} from '../../login/utils/helper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {CommunityService} from '../community/community.service'; @@ -15,9 +15,12 @@ import {EnvironmentSpecificService} from '../../utils/properties/environment-spe import {ConnectHelper} from '../connectHelper'; import {StringUtils} from '../../utils/string-utils.class'; import {UserManagementService} from "../../services/user-management.service"; +import {properties} from "../../../../environments/environment"; @Injectable() export class ConnectAdminLoginGuard implements CanActivate, CanLoad { + sub: Subscription = null; + constructor(private router: Router, private communityService: CommunityService, private propertiesService: EnvironmentSpecificService, @@ -27,15 +30,17 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad { check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; let email = null; - const authorized = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => { - return this.userManagementService.getUserInfo(false).pipe(map(user => { + const authorized = + //this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => { + //return + this.userManagementService.getUserInfo(false).pipe(map(user => { if (user) { email = user.email; if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user)) { return of(true); } else { errorCode = LoginErrorCodes.NOT_ADMIN; - return this.communityService.isCommunityManager(properties, properties['communityAPI'] + community, + return this.communityService.isCommunityManagerByState(properties, properties['communityAPI'] + community, email); } } else { @@ -44,8 +49,8 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad { }), mergeMap( authorized => { return authorized; })); - })); - authorized.pipe(filter(authorized => !authorized)).subscribe(() => { + //})); + this.sub = authorized.pipe(filter(authorized => !authorized)).subscribe(() => { this.router.navigate(['/user-info'], { queryParams: { 'errorCode': errorCode, @@ -63,4 +68,11 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad { const path = StringUtils.URLSegmentsToPath(segments) + document.location.search; return this.check(ConnectHelper.getCommunityFromPath(path), path); } + + canDeactivate() { + if(this.sub) { + this.sub.unsubscribe(); + } + return true; + } } diff --git a/connect/communityGuard/connectCommunityGuard.guard.ts b/connect/communityGuard/connectCommunityGuard.guard.ts index 2426ef49..4a84c256 100644 --- a/connect/communityGuard/connectCommunityGuard.guard.ts +++ b/connect/communityGuard/connectCommunityGuard.guard.ts @@ -9,13 +9,15 @@ import { CanLoad, Route } from '@angular/router'; -import {Observable} from 'rxjs'; +import {Observable, Subscription} from 'rxjs'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import {ConnectHelper} from '../connectHelper'; +import {properties} from "../../../../environments/environment"; @Injectable() export class ConnectCommunityGuard implements CanActivate { + sub: Subscription = null; constructor(private router: Router, private communityService: CommunityService, @@ -23,10 +25,9 @@ export class ConnectCommunityGuard implements CanActivate { } check(community: string): Observable | boolean { - const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => { - return this.communityService.isCommunityType(properties, properties['communityAPI'] + community); - })); - obs.pipe(filter(enabled => !enabled)) + const obs = this.communityService.isCommunityTypeByState(properties, properties['communityAPI'] + community); + + this.sub = obs.pipe(filter(enabled => !enabled)) .subscribe(() => this.router.navigate(['errorcommunity'])); return obs; } @@ -34,4 +35,11 @@ export class ConnectCommunityGuard implements CanActivate { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.queryParams['communityId']); } + + canDeactivate() { + if(this.sub) { + this.sub.unsubscribe(); + } + return true; + } } diff --git a/connect/communityGuard/connectRIGuard.guard.ts b/connect/communityGuard/connectRIGuard.guard.ts index ad11f6c5..2f7ec458 100644 --- a/connect/communityGuard/connectRIGuard.guard.ts +++ b/connect/communityGuard/connectRIGuard.guard.ts @@ -9,13 +9,15 @@ import { CanLoad, Route, UrlSegment } from '@angular/router'; -import {Observable} from 'rxjs'; +import {Observable, Subscription} from 'rxjs'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import {ConnectHelper} from '../connectHelper'; +import {properties} from "../../../../environments/environment"; @Injectable() export class ConnectRIGuard implements CanActivate, CanLoad { + sub: Subscription = null; constructor(private router: Router, private communityService: CommunityService, @@ -23,10 +25,9 @@ export class ConnectRIGuard implements CanActivate, CanLoad { } check(community: string): Observable | boolean { - const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => { - return this.communityService.isRIType(properties, properties['communityAPI'] + community); - })); - obs.pipe(filter(enabled => !enabled)) + const obs = this.communityService.isRITypeByState(properties, properties['communityAPI'] + community); + + this.sub = obs.pipe(filter(enabled => !enabled)) .subscribe(() => this.router.navigate(['errorcommunity'])); return obs; } @@ -39,4 +40,11 @@ export class ConnectRIGuard implements CanActivate, CanLoad { const path = '/' + route.path + document.location.search; return this.check(ConnectHelper.getCommunityFromPath(path)); } + + canDeactivate() { + if(this.sub) { + this.sub.unsubscribe(); + } + return true; + } } diff --git a/connect/curators/curator.service.ts b/connect/curators/curator.service.ts index 8b81438d..84a8a29d 100644 --- a/connect/curators/curator.service.ts +++ b/connect/curators/curator.service.ts @@ -13,7 +13,7 @@ export class CuratorService { public getCurators(properties: EnvProperties, emails: string): Observable { let url: string = properties.adminToolsAPIURL + '/curator?emails='+emails; - return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); } public updateCurator(properties: EnvProperties, curator: Curator) { @@ -23,7 +23,7 @@ export class CuratorService { public getCurator(properties: EnvProperties, curatorId: string): Observable { let url: string = properties.adminToolsAPIURL + 'curator/'+curatorId; - return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); + return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); } } diff --git a/error/isRouteEnabled.guard.ts b/error/isRouteEnabled.guard.ts index a64d3e8c..85e21956 100644 --- a/error/isRouteEnabled.guard.ts +++ b/error/isRouteEnabled.guard.ts @@ -1,5 +1,5 @@ -import {of as observableOf, Observable, Subject} from 'rxjs'; +import {of as observableOf, Observable, Subject, Subscription} from 'rxjs'; import {map, filter, mergeMap, tap} from 'rxjs/operators'; import { Injectable } from '@angular/core'; @@ -22,6 +22,7 @@ import {Page} from "../utils/entities/adminTool/page"; @Injectable() export class IsRouteEnabled implements CanActivate { + sub: Subscription = null; constructor(private router: Router, private config: ConfigurationService, @@ -74,7 +75,7 @@ export class IsRouteEnabled implements CanActivate { const obs = //this.config.isPageEnabled(properties, community, '/' + path); this.config.isPageEnabledByState(properties, community, '/'+path); - obs + this.sub = obs //.pipe(tap((enabled) => console.log("aaa: "+enabled))) .pipe(filter(enabled => !enabled)) .subscribe(() => { @@ -87,4 +88,11 @@ export class IsRouteEnabled implements CanActivate { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.data, route.queryParams['communityId'], state.url); } + + canDeactivate() { + if(this.sub) { + this.sub.unsubscribe(); + } + return true; + } }