-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
has-any-authority.directive.ts
41 lines (36 loc) · 1.34 KB
/
has-any-authority.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import { Principal } from './principal.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 {
private authorities: string[];
constructor(private principal: Principal, private templateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef) {
}
@Input()
set jhiHasAnyAuthority(value: string|string[]) {
this.authorities = typeof value === 'string' ? [ <string> value ] : <string[]> value;
this.updateView();
// Get notified each time authentication state changes.
this.principal.getAuthenticationState().subscribe((identity) => this.updateView());
}
private updateView(): void {
this.principal.hasAnyAuthority(this.authorities).then((result) => {
this.viewContainerRef.clear();
if (result) {
this.viewContainerRef.createEmbeddedView(this.templateRef);
}
});
}
}