mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Refactor permissions check code
Directly check permissions and no subscription (uisettings is always initialized on frontend startup) update permission directive to accept single string add explicit management permission name
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
import { IfPermissionsDirective } from './if-permissions.directive'
|
||||
|
||||
// TODO - Must be implemented
|
||||
describe('IfPermissionsDirective', () => {
|
||||
it('should create an instance', () => {
|
||||
const directive = new IfPermissionsDirective()
|
||||
expect(directive).toBeTruthy()
|
||||
})
|
||||
})
|
@@ -4,18 +4,15 @@ import {
|
||||
Directive,
|
||||
ViewContainerRef,
|
||||
TemplateRef,
|
||||
OnDestroy,
|
||||
} from '@angular/core'
|
||||
import { Subscription } from 'rxjs'
|
||||
import { SettingsService } from '../services/settings.service'
|
||||
|
||||
@Directive({
|
||||
selector: '[ifPermissions]',
|
||||
})
|
||||
export class IfPermissionsDirective implements OnInit, OnDestroy {
|
||||
private subscription: Subscription[] = []
|
||||
export class IfPermissionsDirective implements OnInit {
|
||||
// The role the user must have
|
||||
@Input() public ifPermissions: Array<string>
|
||||
@Input() public ifPermissions: Array<string> | string
|
||||
|
||||
/**
|
||||
* @param {ViewContainerRef} viewContainerRef -- The location where we need to render the templateRef
|
||||
@@ -29,32 +26,14 @@ export class IfPermissionsDirective implements OnInit, OnDestroy {
|
||||
) {}
|
||||
|
||||
public ngOnInit(): void {
|
||||
this.subscription.push(
|
||||
this.settingsService.permissions().subscribe((permission) => {
|
||||
if (!permission) {
|
||||
// Remove element from DOM
|
||||
this.viewContainerRef.clear()
|
||||
}
|
||||
// User permissions are checked by a permission mention in DOM
|
||||
const idx = permission.findIndex(
|
||||
(element) => this.ifPermissions.indexOf(element) !== -1
|
||||
)
|
||||
if (idx < 0) {
|
||||
this.viewContainerRef.clear()
|
||||
} else {
|
||||
// Appends the ref element to DOM
|
||||
this.viewContainerRef.createEmbeddedView(this.templateRef)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* On destroy cancels the API if its fetching.
|
||||
*/
|
||||
public ngOnDestroy(): void {
|
||||
this.subscription.forEach((subscription: Subscription) =>
|
||||
subscription.unsubscribe()
|
||||
)
|
||||
if (
|
||||
[]
|
||||
.concat(this.ifPermissions)
|
||||
.every((perm) => this.settingsService.currentUserCan(perm))
|
||||
) {
|
||||
this.viewContainerRef.createEmbeddedView(this.templateRef)
|
||||
} else {
|
||||
this.viewContainerRef.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +0,0 @@
|
||||
import { SortableDirective } from './sortable.directive'
|
||||
|
||||
describe('SortableDirective', () => {
|
||||
it('should create an instance', () => {
|
||||
const directive = new SortableDirective()
|
||||
expect(directive).toBeTruthy()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user