mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-12-18 04:26:35 +01:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
|
|
import {
|
||
|
|
Directive,
|
||
|
|
Input,
|
||
|
|
OnChanges,
|
||
|
|
OnInit,
|
||
|
|
TemplateRef,
|
||
|
|
ViewContainerRef,
|
||
|
|
} from '@angular/core'
|
||
|
|
import { ObjectWithPermissions } from '../data/object-with-permissions'
|
||
|
|
import {
|
||
|
|
PermissionAction,
|
||
|
|
PermissionsService,
|
||
|
|
} from '../services/permissions.service'
|
||
|
|
|
||
|
|
@Directive({
|
||
|
|
selector: '[ifObjectPermissions]',
|
||
|
|
})
|
||
|
|
export class IfObjectPermissionsDirective implements OnInit, OnChanges {
|
||
|
|
// The role the user must have
|
||
|
|
@Input()
|
||
|
|
ifObjectPermissions: ObjectWithPermissions
|
||
|
|
|
||
|
|
@Input()
|
||
|
|
action: PermissionAction
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
|
||
|
|
* @param {TemplateRef<any>} templateRef -- The templateRef to be potentially rendered
|
||
|
|
* @param {PermissionsService} permissionsService -- Will give us access to the permissions a user has
|
||
|
|
*/
|
||
|
|
constructor(
|
||
|
|
private viewContainerRef: ViewContainerRef,
|
||
|
|
private templateRef: TemplateRef<any>,
|
||
|
|
private permissionsService: PermissionsService
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public ngOnInit(): void {
|
||
|
|
if (
|
||
|
|
!this.ifObjectPermissions ||
|
||
|
|
this.permissionsService.currentUserHasObjectPermissions(
|
||
|
|
this.action,
|
||
|
|
this.ifObjectPermissions
|
||
|
|
)
|
||
|
|
) {
|
||
|
|
this.viewContainerRef.createEmbeddedView(this.templateRef)
|
||
|
|
} else {
|
||
|
|
this.viewContainerRef.clear()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public ngOnChanges(): void {
|
||
|
|
this.ngOnInit()
|
||
|
|
}
|
||
|
|
}
|