datasurvey/src/main/webapp/app/shared/auth/has-any-authority.directive.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-07-03 21:48:27 +00:00
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
* ```
* <some-element *jhiHasAnyAuthority="'ROLE_ADMIN'">...</some-element>
*
* <some-element *jhiHasAnyAuthority="['ROLE_ADMIN', 'ROLE_USER']">...</some-element>
* ```
*/
@Directive({
selector: '[jhiHasAnyAuthority]',
})
export class HasAnyAuthorityDirective implements OnDestroy {
private authorities!: string | string[];
private readonly destroy$ = new Subject<void>();
constructor(private accountService: AccountService, private templateRef: TemplateRef<any>, 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);
}
}
}