File

src/app/auth/directives/roles.directive.ts

Description

This directive will show or hide the element depending whether any of the specified roles matches the roles assigned to "securityInfo" in AuthService.

Implements

AfterViewInit OnInit

Example

Metadata

selector [dataflowAppRoles]

Index

Properties
Methods
Inputs

Constructor

constructor(authService: AuthService, sharedAboutService: SharedAboutService, elem: ElementRef, renderer: Renderer2)
Parameters :
Name Type Optional Description
authService AuthService
sharedAboutService SharedAboutService
elem ElementRef
renderer Renderer2

Inputs

appFeature

Type: string

dataflowAppRoles

Type: string[]

Methods

Private checkRoleAccess
checkRoleAccess(featureEnabled: boolean)
Parameters :
Name Type Optional Description
featureEnabled boolean
Returns : void
Private checkRoles
checkRoles()
Returns : void
ngAfterViewInit
ngAfterViewInit()

Initializes the state element and calls checkRoles().

Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

Private existingDisplayPropertyValue
existingDisplayPropertyValue: string
Type : string
import {
  AfterViewInit, Directive, ElementRef, Input, Output, EventEmitter, HostListener,
  Renderer2, OnInit
} from '@angular/core';
import { AuthService } from '../auth.service';
import { SharedAboutService } from '../../shared/services/shared-about.service';

/**
 * This directive will show or hide the element depending whether
 * any of the specified roles matches the roles assigned to "securityInfo"
 * in {@link AuthService}.
 *
 * @author Gunnar Hillert
 */
@Directive({
    selector: '[dataflowAppRoles]'
})
export class RolesDirective implements AfterViewInit, OnInit {

  @Input()
  public dataflowAppRoles: string[];

  @Input()
  public appFeature: string;

  private existingDisplayPropertyValue: string;
  constructor(
    private authService: AuthService,
    private sharedAboutService: SharedAboutService,
    private elem: ElementRef, private renderer: Renderer2) {
  }

  private checkRoles() {
    if (this.appFeature) {
      this.sharedAboutService.getFeatureInfo().subscribe(featureInfo => {
        const featureEnabled = this.appFeature ? featureInfo.isFeatureEnabled(this.appFeature) : true;
        this.checkRoleAccess(featureEnabled);
      });
    } else {
      this.checkRoleAccess(true);
    }
  }

  private checkRoleAccess(featureEnabled: boolean) {
    const hasRoleAccess = this.authService.securityInfo.canAccess(this.dataflowAppRoles);
    if (!featureEnabled || !hasRoleAccess) {
      this.renderer.setStyle(this.elem.nativeElement, 'display', 'none');
    } else {
      if (this.existingDisplayPropertyValue) {
        this.renderer.setStyle(this.elem.nativeElement, 'display', this.existingDisplayPropertyValue);
      } else {
        this.renderer.removeStyle(this.elem.nativeElement, 'display');
      }
    }
  }

  /**
   * Initializes the state element and calls checkRoles().
   */
  ngAfterViewInit() {
    this.existingDisplayPropertyValue = this.elem.nativeElement.style.display;
    this.checkRoles();
  }

  ngOnInit() {
    this.authService.securityInfoSubject.forEach(event => {
      this.checkRoles();
    });
    this.sharedAboutService.featureInfoSubject.forEach(event => {
      this.checkRoles();
    });
  }
}

results matching ""

    No results matching ""