[Library | Trunk]: Add new session methods on guards

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59232 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2020-08-06 20:01:37 +00:00
parent ae76e56dab
commit 59ff1cc209
2 changed files with 41 additions and 41 deletions

View File

@ -30,13 +30,10 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
check(community: string, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
let email = null; let email = null;
const authorized = const authorized = this.userManagementService.getUserInfo(false).pipe(map(user => {
//this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
//return
this.userManagementService.getUserInfo(false).pipe(map(user => {
if (user) { if (user) {
email = user.email; email = user.email;
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user)) { if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
return of(true); return of(true);
} else { } else {
errorCode = LoginErrorCodes.NOT_ADMIN; errorCode = LoginErrorCodes.NOT_ADMIN;

View File

@ -1,15 +1,15 @@
import {filter, map, mergeMap} from 'rxjs/operators'; import {filter, map, mergeMap} from 'rxjs/operators';
import { Injectable } from '@angular/core'; import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router'; import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
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'; import {ConnectHelper} from '../connectHelper';
import {UserManagementService} from "../../services/user-management.service"; import {UserManagementService} from "../../services/user-management.service";
import {SubscribeService} from "../../utils/subscribe/subscribe.service"; import {SubscribeService} from "../../utils/subscribe/subscribe.service";
import {properties} from "../../../../environments/environment";
@Injectable() @Injectable()
export class ConnectSubscriberGuard implements CanActivate { export class ConnectSubscriberGuard implements CanActivate {
@ -17,37 +17,40 @@ export class ConnectSubscriberGuard implements CanActivate {
private communityService: CommunityService, private communityService: CommunityService,
private subscribeService: SubscribeService, private subscribeService: SubscribeService,
private userManagementService: UserManagementService, private userManagementService: UserManagementService,
private propertiesService: EnvironmentSpecificService) {} private propertiesService: EnvironmentSpecificService) {
}
check(community: string, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
let email = null; let email = null;
const subscribed = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => { const subscribed = this.userManagementService.getUserInfo(false).pipe(map(user => {
return this.userManagementService.getUserInfo(false).pipe(map(user => { if (user) {
if (user) { errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
errorCode = LoginErrorCodes.NOT_SUBSCRIBER; email = user.email;
email = user.email; let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain);
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); if (communityDomain) {
if(communityDomain) { community = communityDomain;
community = communityDomain; }
} if (Session.isSubscriber('community', community, user)) {
return this.subscribeService.isSubscribedToCommunity(properties, community) return of(true);
} else { }
return of(false); return this.subscribeService.isSubscribedToCommunity(properties, community)
} } else {
}), mergeMap( authorized => { return of(false);
return authorized; }
})); }), mergeMap(authorized => {
})); return authorized;
subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => { }));
this.router.navigate(['/user-info'], { subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => {
queryParams: { this.router.navigate(['/user-info'], {
'errorCode': errorCode, queryParams: {
'redirectUrl': path 'errorCode': errorCode,
} 'redirectUrl': path
})}); }
return subscribed; })
} });
return subscribed;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url); return this.check(route.queryParams['communityId'], state.url);