2018-03-15 10:53:07 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Session} from '../../login/utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2018-03-15 10:53:07 +01:00
|
|
|
import {CommunityService} from '../community/community.service';
|
|
|
|
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
|
|
|
|
import { mergeMap } from 'rxjs/operators';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ConnectAdminLoginGuard implements CanActivate {
|
2018-04-04 18:01:57 +02:00
|
|
|
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
|
2018-03-15 10:53:07 +01:00
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-02-25 16:28:14 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2018-03-15 10:53:07 +01:00
|
|
|
let community = (route.queryParams["communityId"]);
|
|
|
|
if(Session.isLoggedIn()){
|
2019-02-25 16:28:14 +01:00
|
|
|
if(Session.isPortalAdministrator() || Session.isCommunityCurator()) {
|
2018-03-15 10:53:07 +01:00
|
|
|
return true;
|
2019-02-25 16:28:14 +01:00
|
|
|
} else {
|
2018-04-16 13:49:35 +02:00
|
|
|
let obs = this.propertiesService.subscribeEnvironment().map(res=>res).mergeMap(properties => {
|
|
|
|
return this.communityService.iscommunityManager(properties, properties["communityAPI"]+community,Session.getUserEmail())});
|
2018-03-15 10:53:07 +01:00
|
|
|
obs.filter(enabled => !enabled)
|
|
|
|
.subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }));
|
|
|
|
return obs;
|
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
} else{
|
2019-01-25 12:18:38 +01:00
|
|
|
errorCode =LoginErrorCodes.NOT_LOGIN;
|
2018-03-15 10:53:07 +01:00
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-27 10:22:55 +02:00
|
|
|
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
|
|
|
}
|