Fix bulk setting null owner / permissions

This commit is contained in:
Michael Shamoon 2022-12-13 23:26:23 -08:00
parent e6804dad2f
commit 737f00df3a
8 changed files with 35 additions and 32 deletions

View File

@ -18,7 +18,7 @@
</div> </div>
<div class="form-check form-switch form-check-inline"> <div class="form-check form-switch form-check-inline">
<input type="checkbox" class="form-check-input" id="is_superuser" formControlName="is_superuser" (change)="onToggleSuperUser()"> <input type="checkbox" class="form-check-input" id="is_superuser" formControlName="is_superuser" (change)="onToggleSuperUser()">
<label class="form-check-label" for="is_superuser"><ng-container i18n>Superuser</ng-container> <small class="form-text text-muted ms-1" i18n>(Grants all permissions and objects)</small></label> <label class="form-check-label" for="is_superuser"><ng-container i18n>Superuser</ng-container> <small class="form-text text-muted ms-1" i18n>(Grants all permissions and can view objects)</small></label>
</div> </div>
</div> </div>

View File

@ -1,7 +1,6 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
import { FormControl, FormGroup } from '@angular/forms' import { FormControl, FormGroup } from '@angular/forms'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { PaperlessGroup } from 'src/app/data/paperless-group'
import { PaperlessUser } from 'src/app/data/paperless-user' import { PaperlessUser } from 'src/app/data/paperless-user'
import { UserService } from 'src/app/services/rest/user.service' import { UserService } from 'src/app/services/rest/user.service'
@ -31,12 +30,10 @@ export class PermissionsDialogComponent implements OnInit {
}) })
get permissions() { get permissions() {
console.log(this.form.get('permissions_form'))
return { return {
owner: this.form.get('permissions_form')?.value['owner'], owner: this.form.get('permissions_form').value?.owner ?? null,
set_permissions: set_permissions:
this.form.get('permissions_form')?.value['set_permissions'], this.form.get('permissions_form').value?.set_permissions ?? null,
} }
} }

View File

@ -80,6 +80,7 @@ export class SavedViewWidgetComponent
clickTag(tag: PaperlessTag, event: MouseEvent) { clickTag(tag: PaperlessTag, event: MouseEvent) {
event.preventDefault() event.preventDefault()
event.stopImmediatePropagation()
this.list.quickFilter([ this.list.quickFilter([
{ rule_type: FILTER_HAS_TAGS_ALL, value: tag.id.toString() }, { rule_type: FILTER_HAS_TAGS_ALL, value: tag.id.toString() },

View File

@ -494,8 +494,6 @@ export class BulkEditorComponent extends ComponentWithPermissions {
backdrop: 'static', backdrop: 'static',
}) })
modal.componentInstance.confirmClicked.subscribe((permissions) => { modal.componentInstance.confirmClicked.subscribe((permissions) => {
console.log(permissions)
modal.componentInstance.buttonsEnabled = false modal.componentInstance.buttonsEnabled = false
this.executeBulkOperation(modal, 'set_permissions', permissions) this.executeBulkOperation(modal, 'set_permissions', permissions)
}) })

View File

@ -136,12 +136,10 @@ def set_permissions(doc_ids, set_permissions, owner=None):
qs = Document.objects.filter(id__in=doc_ids) qs = Document.objects.filter(id__in=doc_ids)
if owner is not None: update_owner_for_object.delay(document_ids=doc_ids, owner=owner)
update_owner_for_object.delay(document_ids=doc_ids, owner=owner)
for doc in qs: for doc in qs:
if set_permissions is not None: set_permissions_for_object(set_permissions, doc)
set_permissions_for_object(set_permissions, doc)
affected_docs = [doc.id for doc in qs] affected_docs = [doc.id for doc in qs]

View File

@ -103,7 +103,7 @@ class SetPermissionsMixin:
) )
return groups return groups
def validate_set_permissions(self, set_permissions): def validate_set_permissions(self, set_permissions=None):
permissions_dict = { permissions_dict = {
"view": { "view": {
"users": User.objects.none(), "users": User.objects.none(),
@ -612,12 +612,11 @@ class BulkEditSerializer(DocumentListSerializer, SetPermissionsMixin):
return ownerUser return ownerUser
def _validate_parameters_set_permissions(self, parameters): def _validate_parameters_set_permissions(self, parameters):
if "permissions" in parameters: parameters["set_permissions"] = self.validate_set_permissions(
self.validate_set_permissions(parameters["permissions"]) parameters["set_permissions"],
if "owner" in parameters: )
if "owner" in parameters and parameters["owner"] is not None:
self._validate_owner(parameters["owner"]) self._validate_owner(parameters["owner"])
if "permissions" not in parameters and "owner" not in parameters:
raise serializers.ValidationError("permissions not specified")
def validate(self, attrs): def validate(self, attrs):

View File

@ -294,7 +294,7 @@ def update_document_archive_file(document_id):
@shared_task @shared_task
def update_owner_for_object(document_ids, owner): def update_owner_for_object(document_ids, owner):
documents = Document.objects.filter(id__in=document_ids) documents = Document.objects.filter(id__in=document_ids)
ownerUser = User.objects.get(pk=owner) ownerUser = User.objects.get(pk=owner) if owner is not None else None
for document in documents: for document in documents:
document.owner = ownerUser document.owner = ownerUser if owner is not None else None
document.save() document.save()

View File

@ -2331,30 +2331,40 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
], ],
) )
def test_set_permissions(self): @mock.patch("documents.serialisers.bulk_edit.set_permissions")
def test_set_permissions(self, m):
m.return_value = "OK"
user1 = User.objects.create(username="user1") user1 = User.objects.create(username="user1")
user2 = User.objects.create(username="user2") user2 = User.objects.create(username="user2")
permissions = { permissions = {
"view": { "view": {
"users": User.objects.filter(id__in=[user1.id, user2.id]), "users": [user1.id, user2.id],
"groups": Group.objects.none(), "groups": None,
}, },
"change": { "change": {
"users": User.objects.filter(id__in=[user1.id]), "users": [user1.id],
"groups": Group.objects.none(), "groups": None,
}, },
} }
bulk_edit.set_permissions( response = self.client.post(
[self.doc2.id, self.doc3.id], "/api/documents/bulk_edit/",
set_permissions=permissions, json.dumps(
{
"documents": [self.doc2.id, self.doc3.id],
"method": "set_permissions",
"parameters": {"set_permissions": permissions},
},
),
content_type="application/json",
) )
self.assertEqual(get_users_with_perms(self.doc2).count(), 2) self.assertEqual(response.status_code, 200)
self.async_task.assert_called_once() m.assert_called_once()
args, kwargs = self.async_task.call_args args, kwargs = m.call_args
self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id]) self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id])
self.assertEqual(len(kwargs["set_permissions"]["view"]["users"]), 2)
class TestBulkDownload(DirectoriesMixin, APITestCase): class TestBulkDownload(DirectoriesMixin, APITestCase):