File

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

Description

A service that can be configured with permission configuration and return permissions for certain elements.

Index

Methods

Methods

checkElementPermission
checkElementPermission(element: string)

Checks permission for a given element

Parameters :
Name Type Optional Description
element string No

The element to check permission for

Returns : boolean

A boolean that contains whether permission for the given element is present

checkLoadRoutePermission
checkLoadRoutePermission(route: Route)

Checks loading permission for a given route, e.g. for lazy loading.

Parameters :
Name Type Optional Description
route Route No

The route to check permission for

Returns : Observable<boolean>

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

checkRoutePermission
checkRoutePermission(route: ActivatedRouteSnapshot)

Checks permission for a given route

Parameters :
Name Type Optional Description
route ActivatedRouteSnapshot No

The route to check permission for

Returns : Observable<boolean>

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

setPermissions
setPermissions(permissionJson: PermissionMaps)

Sets the permission map the service will use

Parameters :
Name Type Optional Description
permissionJson PermissionMaps No

The permission map to use

Returns : void
setRoles
setRoles(userInfo: UserInfo)

Sets the user the service will use

Parameters :
Name Type Optional Description
userInfo UserInfo No

The user to use

Returns : void
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Route} from '@angular/router';
import {UserInfo} from '../api/userinfo';
import {Observable, of} from 'rxjs';
import {PermissionMaps} from './permission-maps';

/**
 * A service that can be configured with permission configuration and return permissions for certain elements.
 */
@Injectable({
  providedIn: 'root'
})
export class SecurityService {
  private roles?: string[];
  private routeMap = new Map<string, string[]>();
  private elementMap = new Map<string, string[]>();

  /**
   * Sets the user the service will use
   * @param userInfo The user to use
   */
  setRoles(userInfo: UserInfo): void {
    this.roles = userInfo.roles;
  }

  /**
   * Sets the permission map the service will use
   * @param permissionJson The permission map to use
   */
  setPermissions(permissionJson: PermissionMaps): void {
    this.routeMap = new Map(Object.entries(permissionJson.routes));
    this.elementMap = new Map(Object.entries(permissionJson.elements));
  }

  /**
   * Checks permission for a given element
   * @param element The element to check permission for
   * @returns A boolean that contains whether permission for the given element is present
   */
  checkElementPermission(element: string): boolean {
    if (!this.roles) return false;

    const elementValue = this.elementMap.get(element);

    return elementValue?.some((role) => this.roles!.includes(role)) ?? false;
  }

  /**
   * Checks permission for a given route
   * @param route The route to check permission for
   * @returns An observable that eventually outputs a boolean whether the given route can be accessed
   */
  checkRoutePermission(route: ActivatedRouteSnapshot): Observable<boolean> {
    if (!this.roles) return of(false);

    const spacer = route.url[0].path;
    const routeValue = this.routeMap.get(spacer);
    const hasAccess = routeValue?.some((role) => this.roles!.includes(role)) ?? false;

    return of(hasAccess);
  }

  /**
   * Checks loading permission for a given route, e.g. for lazy loading.
   * @param route The route to check permission for
   * @returns An observable that eventually outputs a boolean whether the given route can be loaded
   */
  checkLoadRoutePermission(route: Route): Observable<boolean> {
    if (!this.roles || !route.path) {
      return of(false);
    }

    const routeValue = this.routeMap.get(route.path);
    let hasPermission = false;

    if (routeValue) {
      hasPermission = routeValue.some((role) => this.roles!.includes(role));
    }

    return of(hasPermission);
  }
}

results matching ""

    No results matching ""