summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/feature-toggles-guard.service.ts
blob: ad94f2689341f09d3d35c2103ab822a2e73f67bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild } from '@angular/router';

import { map } from 'rxjs/operators';

import { DashboardNotFoundError } from '~/app/core/error/error';
import { FeatureTogglesMap, FeatureTogglesService } from './feature-toggles.service';

@Injectable({
  providedIn: 'root'
})
export class FeatureTogglesGuardService implements CanActivate, CanActivateChild {
  constructor(private featureToggles: FeatureTogglesService) {}

  canActivate(route: ActivatedRouteSnapshot) {
    return this.featureToggles.get().pipe(
      map((enabledFeatures: FeatureTogglesMap) => {
        if (enabledFeatures[route.routeConfig.path] === false) {
          throw new DashboardNotFoundError();
          return false;
        }
        return true;
      })
    );
  }

  canActivateChild(route: ActivatedRouteSnapshot) {
    return this.canActivate(route.parent);
  }
}