From 2e6a721fa26bdc508f064eb8adda58190a95e41b Mon Sep 17 00:00:00 2001 From: nilbacardit26 Date: Wed, 6 Mar 2024 11:50:16 +0100 Subject: [PATCH] adding permissions changed action --- CHANGELOG.rst | 2 +- guillotina_audit/tests/test_audit_basic.py | 12 ++++++++++++ guillotina_audit/utility.py | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2da2ace..283ec8e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,7 @@ 2.0.3 (unreleased) ------------------ -- Nothing changed yet. +- Adding permissions_changed action when permissions are changed 2.0.2 (2024-03-06) diff --git a/guillotina_audit/tests/test_audit_basic.py b/guillotina_audit/tests/test_audit_basic.py index f81f972..a374896 100644 --- a/guillotina_audit/tests/test_audit_basic.py +++ b/guillotina_audit/tests/test_audit_basic.py @@ -264,6 +264,17 @@ async def test_permissions_modified_without_indexing(guillotina_es): assert status == 200 # There should be the same number of documents since indexing_permission_changes is False assert len(resp["hits"]["hits"]) == 2 + response, status = await guillotina_es( + "PATCH", + "/db/guillotina/foo_item", + data=json.dumps({"title": "Another title"}) + ) + assert status == 204 + await asyncio.sleep(2) + # Let's make sure ObjectModifiedEvent adds a document + resp, status = await guillotina_es("GET", "/db/guillotina/@audit") + assert status == 200 + assert len(resp["hits"]["hits"]) == 3 @pytest.mark.app_settings( @@ -319,3 +330,4 @@ async def test_permissions_modified_with_indexing(guillotina_es): assert status == 200 # There should be one more document since indexing_permission_changes is True assert len(resp["hits"]["hits"]) == 3 + assert resp["hits"]["hits"][-1]["_source"]["action"] == "permissions_changed" diff --git a/guillotina_audit/utility.py b/guillotina_audit/utility.py index b7f3765..7dab110 100644 --- a/guillotina_audit/utility.py +++ b/guillotina_audit/utility.py @@ -9,6 +9,7 @@ from guillotina.interfaces import IObjectModifiedEvent from guillotina.interfaces import IObjectMovedEvent from guillotina.interfaces import IObjectRemovedEvent +from guillotina.interfaces import IObjectPermissionsModifiedEvent from guillotina.utils.auth import get_authenticated_user from guillotina.utils.content import get_content_path from guillotina_audit.models import AuditDocument @@ -94,6 +95,9 @@ def log_entry(self, obj, event): elif IObjectMovedEvent.providedBy(event): document["action"] = "moved" document["creation_date"] = datetime.datetime.now(timezone.utc) + elif IObjectPermissionsModifiedEvent.providedBy(event): + document["action"] = "permissions_changed" + document["creation_date"] = datetime.datetime.now(timezone.utc) elif IObjectModifiedEvent.providedBy(event): document["action"] = "modified" document["creation_date"] = obj.modification_date