2018-03-15 10:53:07 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-26 14:57:04 +01:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
|
|
|
ActivatedRoute,
|
|
|
|
CanLoad, Route
|
|
|
|
} from '@angular/router';
|
2018-03-15 10:53:07 +01:00
|
|
|
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()
|
2019-02-26 14:57:04 +01:00
|
|
|
export class ConnectAdminLoginGuard implements CanActivate, CanLoad{
|
|
|
|
constructor(private router: Router,
|
|
|
|
private communityService: CommunityService,
|
|
|
|
private propertiesService:EnvironmentSpecificService,
|
|
|
|
private route: ActivatedRoute) {}
|
2018-03-15 10:53:07 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
check(path: string): Observable<boolean> | boolean {
|
2019-02-25 16:28:14 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2019-02-26 14:57:04 +01:00
|
|
|
let community = this.route.queryParams["communityId"];
|
|
|
|
if (Session.isLoggedIn()) {
|
|
|
|
if (Session.isPortalAdministrator() || Session.isCommunityCurator()) {
|
2018-03-15 10:53:07 +01:00
|
|
|
return true;
|
2019-02-25 16:28:14 +01:00
|
|
|
} else {
|
2019-02-26 14:57:04 +01: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)
|
2019-02-26 14:57:04 +01:00
|
|
|
.subscribe(() => this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": errorCode,
|
|
|
|
"redirectUrl": path
|
|
|
|
}
|
|
|
|
}));
|
2018-03-15 10:53:07 +01:00
|
|
|
return obs;
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errorCode = LoginErrorCodes.NOT_LOGIN;
|
|
|
|
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}});
|
2018-03-15 10:53:07 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
|
|
return this.check(state.url);
|
|
|
|
}
|
2018-03-27 10:22:55 +02:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
|
|
|
return this.check('/' + route.path);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
|
|
|
}
|