Skip to content
Snippets Groups Projects
app-route-guards.ts 5.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • // eslint-disable-next-line max-classes-per-file
    
    rhenck's avatar
    rhenck committed
    import { Injectable } from '@angular/core';
    
    import {
      ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot
    } from '@angular/router';
    
    rhenck's avatar
    rhenck committed
    import { Observable } from 'rxjs';
    
    import { map } from 'rxjs/operators';
    import { MainDataService } from './maindata.service';
    
    rhenck's avatar
    rhenck committed
    import { AuthAccessKeyType, AuthData, AuthFlagType } from './app.interfaces';
    import { BackendService } from './backend.service';
    
    mechtelm's avatar
    mechtelm committed
    export class RouteDispatcherActivateGuard implements CanActivate {
    
    mechtelm's avatar
    mechtelm committed
        private router: Router
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
    
        if (authData) {
          if (authData.token) {
    
    paflov's avatar
    paflov committed
            if (authData.access[AuthAccessKeyType.WORKSPACE_ADMIN] || authData.access[AuthAccessKeyType.SUPER_ADMIN]) {
    
              this.router.navigate(['/r/admin-starter']);
            } else if (authData.flags.indexOf(AuthFlagType.CODE_REQUIRED) >= 0) {
              this.router.navigate(['/r/code-input']);
    
            } else if (authData.access[AuthAccessKeyType.TEST_GROUP_MONITOR]) {
    
              this.router.navigate(['/r/monitor-starter']);
    
            } else if (authData.access[AuthAccessKeyType.TEST]) {
              this.router.navigate(['/r/test-starter']);
    
            } else {
              this.router.navigate(['/r/login', '']);
            }
          } else {
            this.router.navigate(['/r/login', '']);
          }
    
    mechtelm's avatar
    mechtelm committed
        } else {
          this.router.navigate(['/r/login', '']);
        }
    
    mechtelm's avatar
    mechtelm committed
        return false;
    
    mechtelm's avatar
    mechtelm committed
    
    
    mechtelm's avatar
    mechtelm committed
    @Injectable()
    export class DirectLoginActivateGuard implements CanActivate {
      constructor(
        private mds: MainDataService,
        private bs: BackendService,
        private router: Router
      ) {
      }
    
      canActivate(
        next: ActivatedRouteSnapshot,
    
        state: RouterStateSnapshot
      ): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
    
    mechtelm's avatar
    mechtelm committed
        if (!authData) {
          const directLoginName = state.url.substr(1);
          if (directLoginName.length > 0 && directLoginName.indexOf('/') < 0) {
    
            return this.bs.nameOnlyLogin(directLoginName).pipe(
              map((authDataResponse: AuthData|number) => {
                if (typeof authDataResponse !== 'number') {
                  this.mds.setAuthData(authDataResponse as AuthData);
                  this.router.navigate(['/r']);
                  return false;
                }
    
    paf's avatar
    paf committed
                return true;
    
    mechtelm's avatar
    mechtelm committed
          }
        }
    
    mechtelm's avatar
    mechtelm committed
      }
    }
    
    
    @Injectable({
      providedIn: 'root'
    })
    export class CodeInputComponentActivateGuard implements CanActivate {
    
      constructor(private router: Router) { }
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
        if (authData) {
    
    mechtelm's avatar
    mechtelm committed
          if (authData.flags) {
            if (authData.flags.indexOf(AuthFlagType.CODE_REQUIRED) >= 0) {
    
    paf's avatar
    paf committed
              return true;
    
    mechtelm's avatar
    mechtelm committed
            }
            this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
            return false;
    
    mechtelm's avatar
    mechtelm committed
          }
          this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
          return false;
    
        this.router.navigate(['/r']);
        return false;
    
    
    @Injectable({
      providedIn: 'root'
    })
    export class AdminComponentActivateGuard implements CanActivate {
    
      constructor(private router: Router) { }
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
        if (authData) {
          if (authData.access) {
            if (authData.access[AuthAccessKeyType.WORKSPACE_ADMIN]) {
              return true;
            }
    
    mechtelm's avatar
    mechtelm committed
            this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
            return false;
    
    mechtelm's avatar
    mechtelm committed
          this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
          return false;
    
        this.router.navigate(['/r']);
        return false;
    
    @Injectable({
      providedIn: 'root'
    })
    export class AdminOrSuperAdminComponentActivateGuard implements CanActivate {
    
      constructor(private router: Router) { }
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
        if (authData) {
          if (authData.access) {
    
    paflov's avatar
    paflov committed
            if (authData.access[AuthAccessKeyType.WORKSPACE_ADMIN] || authData.access[AuthAccessKeyType.SUPER_ADMIN]) {
    
              return true;
            }
            this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
            return false;
    
          }
          this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
          return false;
    
        this.router.navigate(['/r']);
        return false;
    
    @Injectable({
      providedIn: 'root'
    })
    export class SuperAdminComponentActivateGuard implements CanActivate {
    
      constructor(private router: Router) { }
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
        if (authData) {
          if (authData.access) {
            if (authData.access[AuthAccessKeyType.SUPER_ADMIN]) {
              return true;
            }
    
    mechtelm's avatar
    mechtelm committed
            this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
            return false;
    
    mechtelm's avatar
    mechtelm committed
          this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
          return false;
    
        this.router.navigate(['/r']);
        return false;
    
      }
    }
    
    @Injectable({
      providedIn: 'root'
    })
    export class TestComponentActivateGuard implements CanActivate {
    
      constructor(private router: Router) { }
    
      canActivate(): Observable<boolean> | Promise<boolean> | boolean {
    
        const authData = MainDataService.getAuthData();
        if (authData) {
          if (authData.access) {
            if (authData.access[AuthAccessKeyType.TEST]) {
              return true;
            }
    
    mechtelm's avatar
    mechtelm committed
            this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
            return false;
    
    mechtelm's avatar
    mechtelm committed
          this.router.navigate(['/r']);
    
    paf's avatar
    paf committed
          return false;
    
        this.router.navigate(['/r']);
        return false;
    
    
    @Injectable({
      providedIn: 'root'
    })
    export class GroupMonitorActivateGuard implements CanActivate {
      constructor(
    
      canActivate(): boolean {
        const authData = MainDataService.getAuthData();
    
    paf's avatar
    paf committed
        if (authData && authData.access && authData.access[AuthAccessKeyType.TEST_GROUP_MONITOR]) {
    
    paflov's avatar
    paflov committed
          return true;
    
        }
        this.router.navigate(['/r']);
        return false;