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]',
|
|
|
|
|
})
|
2022-11-11 14:32:18 -08:00
|
|
|
export class IfPermissionsDirective implements OnInit {
|
2022-11-11 18:33:04 +00:00
|
|
|
// The role the user must have
|
2022-11-11 14:32:18 -08:00
|
|
|
@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 {
|
2022-11-11 14:32:18 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|