mirror of https://github.com/Ombi-app/Ombi
parent
5e8f06cd24
commit
26bcf1e4e1
@ -0,0 +1,36 @@
|
||||
import { Directive, Input, OnInit, TemplateRef, ViewContainerRef } from "@angular/core";
|
||||
import { AuthService } from "../../auth/auth.service";
|
||||
|
||||
@Directive({
|
||||
selector: '[role]',
|
||||
})
|
||||
export class RoleDirective implements OnInit {
|
||||
private roleName: string;
|
||||
|
||||
private isHidden = true;
|
||||
|
||||
@Input() public set role(val: string) {
|
||||
if (val) {
|
||||
this.roleName = val;
|
||||
this.updateView();
|
||||
}
|
||||
}
|
||||
|
||||
public constructor(private templateRef: TemplateRef<unknown>, private viewContainer: ViewContainerRef, private auth: AuthService) {}
|
||||
|
||||
public ngOnInit(): void {
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
private updateView(): void {
|
||||
if (this.auth.hasRole(this.roleName)) {
|
||||
if (this.isHidden) {
|
||||
this.viewContainer.createEmbeddedView(this.templateRef);
|
||||
this.isHidden = false;
|
||||
}
|
||||
} else {
|
||||
this.viewContainer.clear();
|
||||
this.isHidden = true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { RoleDirective } from './role-directive';
|
||||
|
||||
@NgModule({
|
||||
declarations: [RoleDirective],
|
||||
exports: [RoleDirective],
|
||||
})
|
||||
export class RoleModule {}
|
Loading…
Reference in new issue