Project

General

Profile

1
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
import {Observable} from 'rxjs';
12
import {Session} from './utils/helper.class';
13
import {LoginErrorCodes} from './utils/guardHelper.class';
14

    
15
@Injectable()
16
export class AdminLoginGuard implements CanActivate{
17

    
18
  constructor(private router: Router) {
19
  }
20

    
21
  check(path: string): boolean {
22
    let loggedIn = false;
23
    let isAdmin = false;
24
    let errorCode = LoginErrorCodes.NOT_LOGIN;
25
    if (Session.isLoggedIn()) {
26
      loggedIn = true;
27
      isAdmin = Session.isPortalAdministrator();
28
      if (!isAdmin) {
29
        errorCode = LoginErrorCodes.NOT_ADMIN;
30
      }
31
    }
32
    if (!loggedIn || !isAdmin) {
33
      this.router.navigate(['/user-info'], {
34
        queryParams: {
35
          'errorCode': errorCode,
36
          'redirectUrl': path
37
        }
38
      });
39
    }
40
    return loggedIn && isAdmin;
41
  }
42

    
43
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
44
    return this.check(state.url);
45
  }
46

    
47
}
(1-1/9)