File

projects/isy-angular-widgets/src/lib/security/security-guard.ts

Description

An AuthGuard that can check whether certain routes can be loaded or activated with current permissions. Can e.g. be used in app-routing-module:

{ path: 'personen', canLoad: [AuthGuard] }

Index

Methods

Methods

canActivate
canActivate(route: ActivatedRouteSnapshot)

Determines if a given route can be accessed

Parameters :
Name Type Optional Description
route ActivatedRouteSnapshot No

The route to check

Returns : Observable<boolean>

An observable that eventually outputs a boolean whether the given route can be accessed

import {inject, Injectable} from '@angular/core';
import {ActivatedRouteSnapshot} from '@angular/router';
import {Observable} from 'rxjs';
import {SecurityService} from './security-service';

/**
 * An AuthGuard that can check whether certain routes can be loaded or activated with current permissions.
 * Can e.g. be used in app-routing-module:
 *
 *
 *   {
 *     path: 'personen',
 *     canLoad: [AuthGuard]
 *   }
 */
@Injectable({providedIn: 'root'})
export class AuthGuard {
  /**
   * A service that can be configured with permission configuration and return permissions for certain elements.
   */
  private readonly securityService = inject(SecurityService);

  /**
   * Determines if a given route can be accessed
   * @param route The route to check
   * @returns An observable that eventually outputs a boolean whether the given route can be accessed
   */
  canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
    return this.securityService.checkRoutePermission(route);
  }
}

results matching ""

    No results matching ""