Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
is-authorized.directive.ts 783 B
Newer Older
import {Directive,ElementRef,Input} from '@angular/core';
import {SecurityService} from './security.service';

@Directive({
})
export class IsAuthorized {
    @Input('isAuthorized') role:string;
    constructor(private _elementRef:ElementRef, private securityService:SecurityService) {
    }
    ngOnInit():void {
      if(this.role && this.role.trim() !== '') {
        this.securityService.isAuthorized([this.role]).subscribe((isAuthorized:boolean) => {
          if(!isAuthorized) {
            let el : HTMLElement = this._elementRef.nativeElement;
            el.parentNode.removeChild(el);
          }
        },
          (error:any) => {
            console.log("Error in IsAuthorized directive [" + error + "]");
          });
      }
    }
}