openaire-library/connect/communityGuard/connectSubscriber.guard.ts

57 lines
2.4 KiB
TypeScript
Raw Normal View History

import {filter, map, mergeMap} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
import {Observable, of} from 'rxjs';
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';
import {UserManagementService} from "../../services/user-management.service";
[Trunk | Library]: 1. community.service.ts: Mark unused method "isSubscribedToCommunity()" as "deprecated" (This service calls community API - made in Italy, not uoa-admin-tools - made in Greece). 2. connectSubscriber.guard.ts: Call "isSubscribedToCommunity()" method from "subscribe.service.ts" (not "community.service.ts"). 3. curator.service.ts: getCurators(properties: EnvProperties, url: string) --> getCurators(properties: EnvProperties, emails: string): Build path for request in service. 4. email.service.ts: Build request path in service a. sendEmail(url: string, email: Email) --> sendEmail(properties: EnvProperties, email: Email) b. contact(url: string, email: Email, recaptcha: string = null) --> contact(properties: EnvProperties, email: Email, recaptcha: string = null) c. Create method "notifyForNewManagers(properties: EnvProperties, communityId: string, email: Email)" (sendEmail was used for everything). 5. subscribe.service.ts: a. subscribeToCommunity(pid: string, email: string, url: string) --> subscribeToCommunity(properties: EnvProperties, pid: string, email: string) b. unSubscribeToCommunity(pid: string, email: string, url: string) --> unSubscribeToCommunity(properties: EnvProperties, pid: string, email: string) 6. layout.service.ts: getLayout(communityId: string, url: string) --> getLayout(properties: EnvProperties, communityId: string): Build path for request in service. 7. feedback.component.ts: In method "contact" from "emailService", build request path in service. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@58420 d315682c-612b-4755-9ff5-7f18f6832af3
2020-04-07 16:57:26 +02:00
import {SubscribeService} from "../../utils/subscribe/subscribe.service";
@Injectable()
export class ConnectSubscriberGuard implements CanActivate {
constructor(private router: Router,
private communityService: CommunityService,
[Trunk | Library]: 1. community.service.ts: Mark unused method "isSubscribedToCommunity()" as "deprecated" (This service calls community API - made in Italy, not uoa-admin-tools - made in Greece). 2. connectSubscriber.guard.ts: Call "isSubscribedToCommunity()" method from "subscribe.service.ts" (not "community.service.ts"). 3. curator.service.ts: getCurators(properties: EnvProperties, url: string) --> getCurators(properties: EnvProperties, emails: string): Build path for request in service. 4. email.service.ts: Build request path in service a. sendEmail(url: string, email: Email) --> sendEmail(properties: EnvProperties, email: Email) b. contact(url: string, email: Email, recaptcha: string = null) --> contact(properties: EnvProperties, email: Email, recaptcha: string = null) c. Create method "notifyForNewManagers(properties: EnvProperties, communityId: string, email: Email)" (sendEmail was used for everything). 5. subscribe.service.ts: a. subscribeToCommunity(pid: string, email: string, url: string) --> subscribeToCommunity(properties: EnvProperties, pid: string, email: string) b. unSubscribeToCommunity(pid: string, email: string, url: string) --> unSubscribeToCommunity(properties: EnvProperties, pid: string, email: string) 6. layout.service.ts: getLayout(communityId: string, url: string) --> getLayout(properties: EnvProperties, communityId: string): Build path for request in service. 7. feedback.component.ts: In method "contact" from "emailService", build request path in service. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@58420 d315682c-612b-4755-9ff5-7f18f6832af3
2020-04-07 16:57:26 +02:00
private subscribeService: SubscribeService,
private userManagementService: UserManagementService,
private propertiesService: EnvironmentSpecificService) {}
check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
let email = null;
const subscribed = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => {
return this.userManagementService.getUserInfo(false).pipe(map(user => {
if (user) {
errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
email = user.email;
let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain);
if(communityDomain) {
community = communityDomain;
}
return this.subscribeService.isSubscribedToCommunity(properties, community)
} else {
return of(false);
}
}), mergeMap( authorized => {
return authorized;
}));
}));
subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => {
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path
}
})});
return subscribed;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url);
}
}