54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|