2022-12-07 14:55:40 -08:00
|
|
|
import {
|
|
|
|
|
Directive,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
OnInit,
|
|
|
|
|
TemplateRef,
|
|
|
|
|
ViewContainerRef,
|
|
|
|
|
} from '@angular/core'
|
|
|
|
|
import { PaperlessUser } from '../data/paperless-user'
|
2022-12-07 15:46:52 -08:00
|
|
|
import { PermissionsService } from '../services/permissions.service'
|
2022-12-07 14:55:40 -08:00
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
|
selector: '[ifOwner]',
|
|
|
|
|
})
|
|
|
|
|
export class IfOwnerDirective implements OnInit, OnChanges {
|
|
|
|
|
// The role the user must have
|
|
|
|
|
@Input()
|
|
|
|
|
ifOwner: PaperlessUser
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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>,
|
2022-12-07 15:46:52 -08:00
|
|
|
private permissionsService: PermissionsService
|
2022-12-07 14:55:40 -08:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
public ngOnInit(): void {
|
2022-12-07 15:46:52 -08:00
|
|
|
if (
|
|
|
|
|
!this.ifOwner ||
|
|
|
|
|
this.permissionsService.currentUserIsOwner(this.ifOwner)
|
|
|
|
|
) {
|
2022-12-07 14:55:40 -08:00
|
|
|
this.viewContainerRef.createEmbeddedView(this.templateRef)
|
|
|
|
|
} else {
|
|
|
|
|
this.viewContainerRef.clear()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ngOnChanges(): void {
|
|
|
|
|
this.ngOnInit()
|
|
|
|
|
}
|
|
|
|
|
}
|