All files / src/app/core/directives authorization-toggle.directive.ts

100% Statements 18/18
80% Branches 16/20
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5337x 37x 37x 37x   37x                                   37x 141x     141x 141x 141x 141x   141x   282x         252x 106x   146x   252x         141x      
import { ChangeDetectorRef, Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { BehaviorSubject, of } from 'rxjs';
import { distinctUntilChanged, switchMap } from 'rxjs/operators';
 
import { AuthorizationToggleService } from 'ish-core/utils/authorization-toggle/authorization-toggle.service';
 
/**
 * Structural directive.
 * Used on an element, this element will only be rendered if the logged in user has the specified permission or one of the specified permission in case an array is given.
 *
 * @example
 * <div *ishIsAuthorizedTo="APP_B2B_PURCHASE'">
 *   Only visible when the current user is allowed to purchase items.
 * </div>
 * or
 * <div *ishIsAuthorizedTo="['APP_B2B_ORDER_APPROVAL', 'APP_B2B_MANAGE_COSTCENTER']">
 *   Only visible when the current user has at least one of the given permissions.
 * </div>
 */
@Directive({
  selector: '[ishIsAuthorizedTo]',
})
export class AuthorizationToggleDirective {
  private permission$ = new BehaviorSubject<string | string[]>(undefined);
 
  constructor(
    private cdRef: ChangeDetectorRef,
    private templateRef: TemplateRef<unknown>,
    private viewContainer: ViewContainerRef,
    private authorizationToggle: AuthorizationToggleService
  ) {
    this.permission$
      .pipe(
        switchMap(permission => (permission ? this.authorizationToggle.isAuthorizedTo(permission) : of(false))),
        distinctUntilChanged(),
        takeUntilDestroyed()
      )
      .subscribe(enabled => {
        if (enabled) {
          this.viewContainer.createEmbeddedView(this.templateRef);
        } else {
          this.viewContainer.clear();
        }
        this.cdRef.markForCheck();
      });
  }
 
  @Input() set ishIsAuthorizedTo(permission: string | string[]) {
    this.permission$.next(permission);
  }
}