File

libs/ngx-pfe/services/pfe-routing/route-generator-guards.service.ts

Description

Temporary guard object

Index

Properties

Properties

canActivate
canActivate: CanActivateFn[] | boolean
Type : CanActivateFn[] | boolean
Optional
canActivateChild
canActivateChild: CanActivateChildFn[] | boolean
Type : CanActivateChildFn[] | boolean
Optional
canDeactivate
canDeactivate: CanDeactivateFn<>[] | boolean
Type : CanDeactivateFn<>[] | boolean
Optional
canLoad
canLoad: CanLoadFn[] | boolean
Type : CanLoadFn[] | boolean
Optional
canMatch
canMatch: CanMatchFn[] | boolean
Type : CanMatchFn[] | boolean
Optional
import { NgxLoggerService } from '@allianz/ngx-logger';
import { Injectable, Injector } from '@angular/core';
import { CanActivateChildFn, CanActivateFn, CanDeactivateFn, CanLoadFn, CanMatchFn } from '@angular/router';
import { GuardOptions } from '../../models/ngx-pfe-page-config.model';
import { PFE_GUARDS_CONFIG } from './route-generator-guards.definition';

/**
 * Temporary guard object
 */
export interface GuardResult {
  canActivate?: CanActivateFn[] | boolean;
  canActivateChild?: CanActivateChildFn[] | boolean;
  canDeactivate?: CanDeactivateFn<unknown>[] | boolean;
  canMatch?: CanMatchFn[] | boolean;
  canLoad?: CanLoadFn[] | boolean;
}

/**
 * Guards manager class to handle the management of guards
 */
@Injectable()
export class PfeRouteGuardsService {
  constructor(
    protected injector: Injector,
    private logger: NgxLoggerService
  ) {}

  /**
   * Generate valid guards list to be added
   * @param guards list of guards define in the pfe config page
   */
  public generateGuards<T extends string | number | symbol>(guards: GuardOptions<T> | undefined): GuardResult | undefined {
    const noGuardsConfig = !guards || Object.values(guards).every((option) => !option?.length);
    if (noGuardsConfig) {
      return undefined;
    }

    let guardsObject: Record<T, () => boolean>;
    try {
      guardsObject = this.injector.get(PFE_GUARDS_CONFIG);
    } catch (err) {
      this.logger.error(`There are no guards defined in the PFE_GUARDS_CONFIG provider`);
      return undefined;
    }

    // Filter valid guards to be added
    return this.mapGuards(guards, guardsObject);
  }

  /**
   * For each item (arrays - canMatch, canActivate, etc) retrieve the ones that implement
   * the interfaces and are define in the providers of the app.module
   * @param guards
   * @param guardsObject
   */
  private mapGuards<T extends string | number | symbol>(
    guards: GuardOptions<T>,
    guardsObject: Record<T, () => boolean>
  ): GuardResult | undefined {
    if (!guards || !guardsObject) {
      return undefined;
    }

    const guardOptions: GuardResult = {};
    (Object.keys(guards) as (keyof GuardOptions)[]).forEach((guardKey) => {
      // Iterate through the define elements in GuardOptions for this page
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
      const elements = guards[guardKey]!.map((key) => guardsObject[key]).filter((value) => value !== undefined);
      guardOptions[guardKey] = elements && elements.length > 0 && elements; // Add valid guards
    });
    return guardOptions;
  }
}

results matching ""

    No results matching ""