import { Directive, Input, TemplateRef, ViewContainerRef, OnDestroy } from '@angular/core'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { AccountService } from 'app/core/auth/account.service'; /** * @whatItDoes Conditionally includes an HTML element if current user has any * of the authorities passed as the `expression`. * * @howToUse * ``` * ... * * ... * ``` */ @Directive({ selector: '[jhiHasAnyAuthority]', }) export class HasAnyAuthorityDirective implements OnDestroy { private authorities!: string | string[]; private readonly destroy$ = new Subject(); constructor(private accountService: AccountService, private templateRef: TemplateRef, private viewContainerRef: ViewContainerRef) {} @Input() set jhiHasAnyAuthority(value: string | string[]) { this.authorities = value; this.updateView(); // Get notified each time authentication state changes. this.accountService .getAuthenticationState() .pipe(takeUntil(this.destroy$)) .subscribe(() => { this.updateView(); }); } ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); } private updateView(): void { const hasAnyAuthority = this.accountService.hasAnyAuthority(this.authorities); this.viewContainerRef.clear(); if (hasAnyAuthority) { this.viewContainerRef.createEmbeddedView(this.templateRef); } } }