Project

General

Profile

1 55650 k.triantaf
import {Injectable} from '@angular/core';
2
import {
3
  Router,
4
  CanActivate,
5
  ActivatedRouteSnapshot,
6
  RouterStateSnapshot,
7
  CanLoad,
8
  Route,
9
  UrlSegment
10
} from '@angular/router';
11 55573 konstantin
import {Observable} from 'rxjs';
12 50169 argiro.kok
import {Session} from './utils/helper.class';
13 53612 konstantin
import {LoginErrorCodes} from './utils/guardHelper.class';
14 50586 argiro.kok
15 50169 argiro.kok
@Injectable()
16 55650 k.triantaf
export class AdminLoginGuard implements CanActivate{
17 50169 argiro.kok
18 54912 k.triantaf
  constructor(private router: Router) {
19
  }
20 50169 argiro.kok
21 54912 k.triantaf
  check(path: string): boolean {
22 54649 k.triantaf
    let loggedIn = false;
23
    let isAdmin = false;
24
    let errorCode = LoginErrorCodes.NOT_LOGIN;
25 54912 k.triantaf
    if (Session.isLoggedIn()) {
26 50169 argiro.kok
      loggedIn = true;
27 54649 k.triantaf
      isAdmin = Session.isPortalAdministrator();
28 54912 k.triantaf
      if (!isAdmin) {
29 54649 k.triantaf
        errorCode = LoginErrorCodes.NOT_ADMIN;
30 50169 argiro.kok
      }
31
    }
32 54912 k.triantaf
    if (!loggedIn || !isAdmin) {
33 55650 k.triantaf
      this.router.navigate(['/user-info'], {
34
        queryParams: {
35
          'errorCode': errorCode,
36
          'redirectUrl': path
37
        }
38
      });
39 50169 argiro.kok
    }
40 54649 k.triantaf
    return loggedIn && isAdmin;
41 50169 argiro.kok
  }
42 54649 k.triantaf
43
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
44 54665 k.triantaf
    return this.check(state.url);
45 54649 k.triantaf
  }
46
47 50169 argiro.kok
}