2018-03-27 10:22:55 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-26 14:57:04 +01:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
|
|
|
CanLoad,
|
|
|
|
Route, ActivatedRoute
|
|
|
|
} from '@angular/router';
|
2018-03-27 10:22:55 +02: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-27 10:22:55 +02:00
|
|
|
import {CommunityService} from '../community/community.service';
|
|
|
|
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
|
|
|
|
|
|
|
|
@Injectable()
|
2019-02-26 14:57:04 +01:00
|
|
|
export class ConnectRIGuard implements CanActivate, CanLoad {
|
2018-03-27 10:22:55 +02:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService, private route:ActivatedRoute) {}
|
|
|
|
|
|
|
|
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()) {
|
2019-02-25 16:28:14 +01:00
|
|
|
if(Session.isPortalAdministrator()) {
|
2018-03-27 10:22:55 +02: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().mergeMap(properties => {
|
2019-02-26 14:57:04 +01:00
|
|
|
return this.communityService.iscommunityRI(properties, properties["communityAPI"] + community)
|
|
|
|
});
|
2018-03-27 10:22:55 +02: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-27 10:22:55 +02:00
|
|
|
return obs;
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
} else{
|
|
|
|
errorCode = LoginErrorCodes.NOT_LOGIN;
|
2019-02-26 14:57:04 +01:00
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": path } });
|
2018-03-27 10:22:55 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2018-03-27 10:22:55 +02: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-27 10:22:55 +02:00
|
|
|
}
|
|
|
|
}
|