paperless-ngx/src-ui/src/app/directives/if-permissions.directive.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-11-11 18:33:04 +00:00
import {
Input,
OnInit,
Directive,
ViewContainerRef,
TemplateRef,
} from '@angular/core'
import { SettingsService } from '../services/settings.service'
@Directive({
selector: '[ifPermissions]',
})
export class IfPermissionsDirective implements OnInit {
2022-11-11 18:33:04 +00:00
// The role the user must have
@Input() public ifPermissions: Array<string> | string
2022-11-11 18:33:04 +00:00
/**
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
* @param {TemplateRef<any>} templateRef -- The templateRef to be potentially rendered
* @param {SettignsService} settignsService -- Will give us access to the permissions a user has
*/
constructor(
private viewContainerRef: ViewContainerRef,
private templateRef: TemplateRef<any>,
private settingsService: SettingsService
) {}
public ngOnInit(): void {
if (
[]
.concat(this.ifPermissions)
.every((perm) => this.settingsService.currentUserCan(perm))
) {
this.viewContainerRef.createEmbeddedView(this.templateRef)
} else {
this.viewContainerRef.clear()
}
2022-11-11 18:33:04 +00:00
}
}