feat: create has-role directive for simple ACL
This commit is contained in:
parent
e9735a4e99
commit
95165a0940
34
src/directives/has-role.directive.ts
Normal file
34
src/directives/has-role.directive.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core';
|
||||||
|
import AuthApiService from "@api/v1/authApiService";
|
||||||
|
import {AuthRoles} from "@model/AuthRoles";
|
||||||
|
import {catchError, of} from "rxjs";
|
||||||
|
|
||||||
|
@Directive({
|
||||||
|
selector: '[appHasRole]',
|
||||||
|
standalone: true,
|
||||||
|
providers: [AuthApiService]
|
||||||
|
})
|
||||||
|
export class HasRoleDirective {
|
||||||
|
constructor(
|
||||||
|
private templateRef: TemplateRef<any>,
|
||||||
|
private viewContainer: ViewContainerRef,
|
||||||
|
private authService: AuthApiService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Input() set appHasRole(role: AuthRoles) {
|
||||||
|
this.viewContainer.clear();
|
||||||
|
|
||||||
|
this.authService
|
||||||
|
.getRole()
|
||||||
|
.pipe(catchError(error => {
|
||||||
|
this.viewContainer.clear();
|
||||||
|
return of(null);
|
||||||
|
}))
|
||||||
|
.subscribe(data => {
|
||||||
|
if (data === role)
|
||||||
|
this.viewContainer.createEmbeddedView(this.templateRef);
|
||||||
|
else
|
||||||
|
this.viewContainer.clear();
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user