2017-12-19 13:53:46 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Session} from './utils/helper.class';
|
|
|
|
import {ErrorCodes} from './utils/guardHelper.class';
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
|
|
|
export class AdminLoginGuard implements CanActivate {
|
|
|
|
|
|
|
|
constructor(private router: Router) {}
|
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
|
|
var user;
|
|
|
|
var loggedIn = false;
|
|
|
|
var isAdmin = false;
|
|
|
|
var errorCode = ErrorCodes.NOT_LOGGIN;
|
|
|
|
if(Session.isLoggedIn()){
|
|
|
|
loggedIn = true;
|
2018-06-12 13:15:42 +02:00
|
|
|
if(!Session.isLoggedIn()){
|
2017-12-19 13:53:46 +01:00
|
|
|
loggedIn = false;
|
|
|
|
errorCode = ErrorCodes.NOT_VALID;
|
|
|
|
}else {
|
2018-03-27 10:22:55 +02:00
|
|
|
isAdmin = Session.isPortalAdministrator();
|
2017-12-19 13:53:46 +01:00
|
|
|
if(!isAdmin){
|
|
|
|
errorCode = ErrorCodes.NOT_ADMIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
errorCode =ErrorCodes.NOT_LOGGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!loggedIn){
|
|
|
|
// this.guardHelper.redirect("/user-info",errorCode,state.url);
|
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}else if(!isAdmin){
|
|
|
|
// this.guardHelper.redirect("/user-info",errorCode,state.url);
|
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|