paperless-ngx/src-ui/src/app/directives/if-permissions.directive.ts
Michael Shamoon 5c5486d2ea Refactor permissions check code
Directly check permissions and no subscription (uisettings is always initialized on frontend startup)
update permission directive to accept single string
add explicit management permission name
2022-11-11 15:45:37 -08:00

40 lines
1.1 KiB
TypeScript

import {
Input,
OnInit,
Directive,
ViewContainerRef,
TemplateRef,
} from '@angular/core'
import { SettingsService } from '../services/settings.service'
@Directive({
selector: '[ifPermissions]',
})
export class IfPermissionsDirective implements OnInit {
// The role the user must have
@Input() public ifPermissions: Array<string> | string
/**
* @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()
}
}
}