openaire-library/connect/communityGuard/connectAdminLoginGuard.guar...

76 lines
2.8 KiB
TypeScript
Raw Normal View History

import {map, filter, mergeMap, take} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad, Route, UrlSegment
} from '@angular/router';
[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
2020-07-17 01:08:10 +02:00
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';
import {EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
import {StringUtils} from '../../utils/string-utils.class';
import {UserManagementService} from "../../services/user-management.service";
[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
2020-07-17 01:08:10 +02:00
import {properties} from "../../../../environments/environment";
@Injectable()
export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
[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
2020-07-17 01:08:10 +02:00
sub: Subscription = null;
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService,
private userManagementService: UserManagementService) {
}
check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
let email = null;
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1),map(user => {
if (user) {
email = user.email;
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
return of(true);
} else {
errorCode = LoginErrorCodes.NOT_ADMIN;
[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
2020-07-17 01:08:10 +02:00
return this.communityService.isCommunityManagerByState(properties, properties['communityAPI'] + community,
email).pipe(take(1));
}
} else {
return of(false);
}
}), mergeMap( authorized => {
return authorized;
}));
[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
2020-07-17 01:08:10 +02:00
//}));
this.sub = authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path
}
})});
return authorized;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url);
}
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
const path = StringUtils.URLSegmentsToPath(segments) + document.location.search;
return this.check(ConnectHelper.getCommunityFromPath(path), path);
}
[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
2020-07-17 01:08:10 +02:00
canDeactivate() {
if(this.sub) {
this.sub.unsubscribe();
}
return true;
}
}