Refactor permissions API endpoints, UI group permissions

This commit is contained in:
Michael Shamoon
2022-12-07 21:11:47 -08:00
parent 4016649a18
commit 692f43f43e
29 changed files with 353 additions and 139 deletions

View File

@@ -1,5 +1,6 @@
import {
Directive,
EmbeddedViewRef,
Input,
OnChanges,
OnInit,
@@ -18,10 +19,12 @@ import {
export class IfObjectPermissionsDirective implements OnInit, OnChanges {
// The role the user must have
@Input()
ifObjectPermissions: ObjectWithPermissions
ifObjectPermissions: {
object: ObjectWithPermissions
action: PermissionAction
}
@Input()
action: PermissionAction
createdView: EmbeddedViewRef<any>
/**
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
@@ -36,13 +39,16 @@ export class IfObjectPermissionsDirective implements OnInit, OnChanges {
public ngOnInit(): void {
if (
!this.ifObjectPermissions ||
!this.ifObjectPermissions?.object ||
this.permissionsService.currentUserHasObjectPermissions(
this.action,
this.ifObjectPermissions
this.ifObjectPermissions.action,
this.ifObjectPermissions.object
)
) {
this.viewContainerRef.createEmbeddedView(this.templateRef)
if (!this.createdView)
this.createdView = this.viewContainerRef.createEmbeddedView(
this.templateRef
)
} else {
this.viewContainerRef.clear()
}

View File

@@ -1,12 +1,13 @@
import {
Directive,
EmbeddedViewRef,
Input,
OnChanges,
OnInit,
TemplateRef,
ViewContainerRef,
} from '@angular/core'
import { PaperlessUser } from '../data/paperless-user'
import { ObjectWithPermissions } from '../data/object-with-permissions'
import { PermissionsService } from '../services/permissions.service'
@Directive({
@@ -15,7 +16,9 @@ import { PermissionsService } from '../services/permissions.service'
export class IfOwnerDirective implements OnInit, OnChanges {
// The role the user must have
@Input()
ifOwner: PaperlessUser
ifOwner: ObjectWithPermissions
createdView: EmbeddedViewRef<any>
/**
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
@@ -29,11 +32,11 @@ export class IfOwnerDirective implements OnInit, OnChanges {
) {}
public ngOnInit(): void {
if (
!this.ifOwner ||
this.permissionsService.currentUserIsOwner(this.ifOwner)
) {
this.viewContainerRef.createEmbeddedView(this.templateRef)
if (this.permissionsService.currentUserOwnsObject(this.ifOwner)) {
if (!this.createdView)
this.createdView = this.viewContainerRef.createEmbeddedView(
this.templateRef
)
} else {
this.viewContainerRef.clear()
}

View File

@@ -6,17 +6,19 @@ import {
TemplateRef,
} from '@angular/core'
import {
PaperlessPermission,
PermissionAction,
PermissionsService,
PermissionType,
} from '../services/permissions.service'
@Directive({
selector: '[ifPermissions]',
})
export class IfPermissionsDirective implements OnInit {
// The role the user must have
@Input()
ifPermissions: Array<PaperlessPermission> | PaperlessPermission
ifPermissions:
| Array<{ action: PermissionAction; type: PermissionType }>
| { action: PermissionAction; type: PermissionType }
/**
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
@@ -33,8 +35,8 @@ export class IfPermissionsDirective implements OnInit {
if (
[]
.concat(this.ifPermissions)
.every((perm: PaperlessPermission) =>
this.permissionsService.currentUserCan(perm)
.every((perm: { action: PermissionAction; type: PermissionType }) =>
this.permissionsService.currentUserCan(perm.action, perm.type)
)
) {
this.viewContainerRef.createEmbeddedView(this.templateRef)