[Trunk | Library]:

1. community.service.ts: 
	a. Add BehaviorSubject for community.
	b. Method "isCommunityManager" replaced by "isCommunityManagerByState" (filtering applied in community stored in BehaviorSubject).
        c. Method "isRIType" replaced by "isRITypeByState" (filtering applied in community stored in BehaviorSubject).
        d. Method "isCommunityType" replaced by "isCommunityTypeByState" (filtering applied in community stored in BehaviorSubject).
2. connectAdminLoginGuard.guard.ts: 
	a. Get properties from environment (no service needed).
	b. Call method "isCommunityManagerByState" of CommunityService.
	c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules).
3. connectCommunityGuard.guard.ts: 
        a. Get properties from environment (no service needed).
        b. Call method "isCommunityTypeByState" of CommunityService.
        c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules).
4. connectRIGuard.guard.ts:
        a. Get properties from environment (no service needed).
        b. Call method "isRITypeByState" of CommunityService.
        c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules).
5. isRouteEnabled.guard.ts: Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules).
6. curator.service.ts: [Undo change of r59073] Use "useCache" property, not "useLongCache" for curators.


git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59140 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
konstantina.galouni 2020-07-16 23:08:10 +00:00
parent 04badde256
commit 0a1576d618
6 changed files with 226 additions and 116 deletions

View File

@ -1,17 +1,50 @@
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 {
public community: BehaviorSubject<CommunityInfo> = null;
private promise: Promise<boolean> = null;
constructor(private http: HttpClient) {
this.community = new BehaviorSubject(null);
}
getCommunityByService(properties: EnvProperties, url: string) {
console.debug("getCommunityByService");
this.promise = new Promise<any>(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);
}
await this.promise;
return this.community.getValue();
}
getCommunityByState(properties: EnvProperties, url: string) {
return from(this.getCommunityByStateAsync(properties, url));
}
/**
* @deprecated
*/
getCommunity(properties: EnvProperties, url: string) {
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => <any> res.json())
@ -33,6 +66,23 @@ export class CommunityService {
/*.map(res => res.json())*/
}
async isCommunityManagerByStateAsync(properties: EnvProperties, url: string, manager: string) {
if(!this.promise) {
this.getCommunityByService(properties, url);
}
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 => <any> res.json())
@ -40,6 +90,27 @@ export class CommunityService {
.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 => <any> res.json())
@ -47,6 +118,9 @@ export class CommunityService {
.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 => <any> res.json())

View File

@ -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> | 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;
}
}

View File

@ -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> | 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> | boolean {
return this.check(route.queryParams['communityId']);
}
canDeactivate() {
if(this.sub) {
this.sub.unsubscribe();
}
return true;
}
}

View File

@ -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> | 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;
}
}

View File

@ -13,7 +13,7 @@ export class CuratorService {
public getCurators(properties: EnvProperties, emails: string): Observable<Curator[]> {
let url: string = properties.adminToolsAPIURL + '/curator?emails='+emails;
return this.http.get<Curator[]>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
return this.http.get<Curator[]>((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<Curator> {
let url: string = properties.adminToolsAPIURL + 'curator/'+curatorId;
return this.http.get<Curator>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
return this.http.get<Curator>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
}
}

View File

@ -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> | boolean {
return this.check(route.data, route.queryParams['communityId'], state.url);
}
canDeactivate() {
if(this.sub) {
this.sub.unsubscribe();
}
return true;
}
}