mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-05 22:39:23 +00:00
Fix: check descendant tag permissions on delete (#14687)
This commit is contained in:
1 parent
22746aa202
commit
fa83a4c5dc
2 files changed
+82
No files matched your search
@@ -576,6 +576,15 @@ class CorrespondentViewSet(
|
||||
return super().retrieve(request, *args, **kwargs)
|
||||
|
||||
|
||||
def _with_tag_descendants(tags: QuerySet[Tag]) -> QuerySet[Tag]:
|
||||
"""Include every tag that deleting the selected tags will affect."""
|
||||
tag_ids = set()
|
||||
for tag in tags:
|
||||
tag_ids.add(tag.pk)
|
||||
tag_ids.update(tag.get_descendants_pks())
|
||||
return Tag.objects.filter(pk__in=tag_ids)
|
||||
|
||||
|
||||
@extend_schema_view(**generate_object_with_permissions_schema(TagSerializer))
|
||||
class TagViewSet(PermissionsAwareDocumentCountMixin, ModelViewSet[Tag]):
|
||||
model = Tag
|
||||
@@ -597,6 +606,14 @@ class TagViewSet(PermissionsAwareDocumentCountMixin, ModelViewSet[Tag]):
|
||||
filterset_class = TagFilterSet
|
||||
ordering_fields = ("color", "name", "matching_algorithm", "match", "document_count")
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
tags = _with_tag_descendants(Tag.objects.filter(pk=instance.pk))
|
||||
if tags.exclude(
|
||||
pk__in=permitted_object_ids(self.request.user, Tag, "delete_tag"),
|
||||
).exists():
|
||||
raise PermissionDenied(_("Insufficient permissions."))
|
||||
super().perform_destroy(instance)
|
||||
|
||||
def get_serializer_context(self):
|
||||
context = super().get_serializer_context()
|
||||
context["document_count_filter"] = self.get_document_count_filter()
|
||||
@@ -5075,6 +5092,9 @@ class BulkEditObjectsView(PassUserMixin):
|
||||
pk__in=object_ids,
|
||||
)
|
||||
|
||||
if object_type == "tags" and operation == "delete":
|
||||
objs = _with_tag_descendants(objs)
|
||||
|
||||
if not user.is_superuser:
|
||||
perm = f"documents.{perm_codename}"
|
||||
# Limited to the owner (or unowned), same as documents, see BulkEditView
|
||||
|
||||
Reference in new issue
Block a user