mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-20 04:26:53 +00:00
Fix: let template viewers preview zones
The zone preview is a POST, so it required permission to add templates. Both preview endpoints now require viewing templates and documents.
This commit is contained in:
1 parent
0e7cbda3d9
commit
f401267c4b
3 files changed
+79
-5
No files matched your search
@@ -699,6 +699,21 @@ class ViewDocumentsPermissions(GlobalPermissionsByMethod):
|
||||
}
|
||||
|
||||
|
||||
class ViewOcrTemplatesPermissions(GlobalPermissionsByMethod):
|
||||
"""
|
||||
Requires the global permission to view OCR templates for OPTIONS, GET and
|
||||
POST, for the template editor's read-only preview endpoints.
|
||||
"""
|
||||
|
||||
perms_map = {
|
||||
"OPTIONS": ["documents.view_ocrtemplate"],
|
||||
"GET": ["documents.view_ocrtemplate"],
|
||||
# The zone preview is a POST only to carry the zone in the body; it
|
||||
# creates and changes nothing, so viewing templates is enough.
|
||||
"POST": ["documents.view_ocrtemplate"],
|
||||
}
|
||||
|
||||
|
||||
class TrashPermissions(GlobalPermissionsByMethod):
|
||||
"""Check the global document permission for each trash operation."""
|
||||
|
||||
|
||||
@@ -2201,6 +2201,60 @@ class TestOcrTemplatePreviewPermissions:
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
mock_preview.assert_not_called()
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"action",
|
||||
[
|
||||
pytest.param("document-page-image", id="document-page-image"),
|
||||
pytest.param("test-zone", id="test-zone"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("template_perms", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
("view_ocrtemplate",),
|
||||
status.HTTP_200_OK,
|
||||
id="view-template-is-enough",
|
||||
),
|
||||
pytest.param(
|
||||
("add_ocrtemplate", "change_ocrtemplate"),
|
||||
status.HTTP_403_FORBIDDEN,
|
||||
id="no-view-template",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_previews_require_global_template_view_permission(
|
||||
self,
|
||||
regular_user: User,
|
||||
user_client: APIClient,
|
||||
mock_preview: MagicMock,
|
||||
action: str,
|
||||
template_perms: tuple[str, ...],
|
||||
expected: int,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A user with global view document permission and either only view
|
||||
template permission, or add and change but not view
|
||||
- An unowned image document
|
||||
WHEN:
|
||||
- The user requests a page image or a zone preview
|
||||
THEN:
|
||||
- Viewing templates is enough for both, and without it both are
|
||||
refused
|
||||
"""
|
||||
grant_global(regular_user, "view_document", *template_perms)
|
||||
document = DocumentFactory(owner=None, mime_type="image/png")
|
||||
document.source_path.write_bytes(b"document contents")
|
||||
mock_preview.return_value = {"raw_text": None, "value": None}
|
||||
|
||||
if action == "document-page-image":
|
||||
response = user_client.get(preview_url(document))
|
||||
else:
|
||||
response = preview_zone(user_client, document)
|
||||
|
||||
assert response.status_code == expected
|
||||
|
||||
def test_versions_are_authorized_through_the_root_document(
|
||||
self,
|
||||
preview_client: APIClient,
|
||||
@@ -2281,7 +2335,7 @@ class TestOcrTemplatePreviewPermissions:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A user with global add template and view document permissions
|
||||
- A user with global view template and view document permissions
|
||||
WHEN:
|
||||
- The user previews a zone on the document
|
||||
THEN:
|
||||
@@ -2290,7 +2344,7 @@ class TestOcrTemplatePreviewPermissions:
|
||||
- No preview extraction runs
|
||||
"""
|
||||
owner = UserFactory()
|
||||
grant_global(regular_user, "add_ocrtemplate", "view_document")
|
||||
grant_global(regular_user, "view_ocrtemplate", "view_document")
|
||||
document = DocumentFactory(owner=owner)
|
||||
response = preview_zone(user_client, document, x=0, y=0, height=100, page=1)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
@@ -183,6 +183,7 @@ from documents.permissions import PaperlessNotePermissions
|
||||
from documents.permissions import PaperlessObjectPermissions
|
||||
from documents.permissions import TrashPermissions
|
||||
from documents.permissions import ViewDocumentsPermissions
|
||||
from documents.permissions import ViewOcrTemplatesPermissions
|
||||
from documents.permissions import annotate_document_count_for_related_queryset
|
||||
from documents.permissions import get_document_count_filter_for_user
|
||||
from documents.permissions import get_permission_grantees
|
||||
@@ -5836,10 +5837,14 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
|
||||
ordering_fields = ("name",)
|
||||
|
||||
def get_permissions(self):
|
||||
permissions = super().get_permissions()
|
||||
if self.action in {"document_page_image", "test_zone"}:
|
||||
permissions.append(ViewDocumentsPermissions())
|
||||
return permissions
|
||||
# Read-only previews: viewing templates is enough, even for the POST.
|
||||
return [
|
||||
IsAuthenticated(),
|
||||
ViewOcrTemplatesPermissions(),
|
||||
ViewDocumentsPermissions(),
|
||||
]
|
||||
return super().get_permissions()
|
||||
|
||||
@action(
|
||||
detail=False,
|
||||
|
||||
Reference in new issue
Block a user