mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import {
|
|
Input,
|
|
OnInit,
|
|
Directive,
|
|
ViewContainerRef,
|
|
TemplateRef,
|
|
} from '@angular/core'
|
|
import {
|
|
PermissionAction,
|
|
PermissionsService,
|
|
PermissionType,
|
|
} from '../services/permissions.service'
|
|
|
|
@Directive({
|
|
selector: '[ifPermissions]',
|
|
})
|
|
export class IfPermissionsDirective implements OnInit {
|
|
@Input()
|
|
ifPermissions:
|
|
| Array<{ action: PermissionAction; type: PermissionType }>
|
|
| { action: PermissionAction; type: PermissionType }
|
|
|
|
/**
|
|
* @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 (
|
|
[]
|
|
.concat(this.ifPermissions)
|
|
.every((perm: { action: PermissionAction; type: PermissionType }) =>
|
|
this.permissionsService.currentUserCan(perm.action, perm.type)
|
|
)
|
|
) {
|
|
this.viewContainerRef.createEmbeddedView(this.templateRef)
|
|
} else {
|
|
this.viewContainerRef.clear()
|
|
}
|
|
}
|
|
}
|