]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Change: enable auditlog by default, fix import / export (#6267)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Thu, 4 Apr 2024 18:51:15 +0000 (11:51 -0700)
committerGitHub <noreply@github.com>
Thu, 4 Apr 2024 18:51:15 +0000 (18:51 +0000)
docs/configuration.md
src/documents/management/commands/document_exporter.py
src/documents/management/commands/document_importer.py
src/documents/tests/test_management_exporter.py
src/paperless/checks.py
src/paperless/settings.py
src/paperless/tests/test_checks.py

index 981d27b27bfd332644d5047adf7e27e49b294d25..1b5cc510dc444235021faa2d01e9f5f80afab57a 100644 (file)
@@ -1311,11 +1311,9 @@ assigns or creates tags if a properly formatted barcode is detected.
 
 #### [`PAPERLESS_AUDIT_LOG_ENABLED=<bool>`](#PAPERLESS_AUDIT_LOG_ENABLED) {#PAPERLESS_AUDIT_LOG_ENABLED}
 
-: Enables an audit trail for documents, document types, correspondents, and tags. Log entries can be viewed in the Django backend only.
+: Enables the audit trail for documents, document types, correspondents, and tags.
 
-    !!! warning
-
-        Once enabled cannot be disabled
+    Defaults to true.
 
 ## Collate Double-Sided Documents {#collate}
 
index faa42ba31d06e94432b278e060edc746179a7044..b02fd686bbc9725fb7b5b60dce75c2f9c8443383 100644 (file)
@@ -9,6 +9,9 @@ from typing import Optional
 
 import tqdm
 from django.conf import settings
+
+if settings.AUDIT_LOG_ENABLED:
+    from auditlog.models import LogEntry
 from django.contrib.auth.models import Group
 from django.contrib.auth.models import Permission
 from django.contrib.auth.models import User
@@ -307,6 +310,11 @@ class Command(BaseCommand):
                 serializers.serialize("json", ApplicationConfiguration.objects.all()),
             )
 
+            if settings.AUDIT_LOG_ENABLED:
+                manifest += json.loads(
+                    serializers.serialize("json", LogEntry.objects.all()),
+                )
+
             # These are treated specially and included in the per-document manifest
             # if that setting is enabled.  Otherwise, they are just exported to the bulk
             # manifest
index 88ac2b91a4095b27929447dca912973d4f6d7893..dc0ac36fd23cf4efdb7f8df81a3b2c1b35ef0f1a 100644 (file)
@@ -21,7 +21,13 @@ from django.db.models.signals import post_save
 from filelock import FileLock
 
 from documents.file_handling import create_source_path_directory
+from documents.models import Correspondent
+from documents.models import CustomField
+from documents.models import CustomFieldInstance
 from documents.models import Document
+from documents.models import DocumentType
+from documents.models import Note
+from documents.models import Tag
 from documents.parsers import run_convert
 from documents.settings import EXPORTER_ARCHIVE_NAME
 from documents.settings import EXPORTER_FILE_NAME
@@ -30,6 +36,9 @@ from documents.signals.handlers import update_filename_and_move_files
 from documents.utils import copy_file_with_basic_stats
 from paperless import version
 
+if settings.AUDIT_LOG_ENABLED:
+    from auditlog.registry import auditlog
+
 
 @contextmanager
 def disable_signal(sig, receiver, sender):
@@ -151,6 +160,15 @@ class Command(BaseCommand):
             receiver=update_filename_and_move_files,
             sender=Document.tags.through,
         ):
+            if settings.AUDIT_LOG_ENABLED:
+                auditlog.unregister(Document)
+                auditlog.unregister(Correspondent)
+                auditlog.unregister(Tag)
+                auditlog.unregister(DocumentType)
+                auditlog.unregister(Note)
+                auditlog.unregister(CustomField)
+                auditlog.unregister(CustomFieldInstance)
+
             # Fill up the database with whatever is in the manifest
             try:
                 with transaction.atomic():
index 226b89694e8cd87d798061777876fa3f6eeb644b..b95d07deceb2e5cc8cc0f3055492680e9836c7b7 100644 (file)
@@ -780,3 +780,17 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
 
             self.assertEqual(ContentType.objects.count(), num_content_type_objects)
             self.assertEqual(Permission.objects.count(), num_permission_objects + 1)
+
+    def test_exporter_with_auditlog_disabled(self):
+        shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))
+        shutil.copytree(
+            os.path.join(os.path.dirname(__file__), "samples", "documents"),
+            os.path.join(self.dirs.media_dir, "documents"),
+        )
+
+        with override_settings(
+            AUDIT_LOG_ENABLED=False,
+        ):
+            manifest = self._do_export(use_filename_format=True)
+            for obj in manifest:
+                self.assertNotEqual(obj["model"], "auditlog.logentry")
index 0bfdce2cd8e1f874785fc1c9aa42efc97988a8ba..cbc8da5cf64e4539cf1926083512a3d4f4355aea 100644 (file)
@@ -5,7 +5,6 @@ import shutil
 import stat
 
 from django.conf import settings
-from django.core.checks import Critical
 from django.core.checks import Error
 from django.core.checks import Warning
 from django.core.checks import register
@@ -205,13 +204,10 @@ def audit_log_check(app_configs, **kwargs):
     all_tables = db_conn.introspection.table_names()
     result = []
 
-    if ("auditlog_logentry" in all_tables) and not (settings.AUDIT_LOG_ENABLED):
+    if ("auditlog_logentry" in all_tables) and not settings.AUDIT_LOG_ENABLED:
         result.append(
-            Critical(
-                (
-                    "auditlog table was found but PAPERLESS_AUDIT_LOG_ENABLED"
-                    " is not active.  This setting cannot be disabled after enabling"
-                ),
+            Warning(
+                ("auditlog table was found but audit log is disabled."),
             ),
         )
 
index a583ef40675e7bdc5b2b8d1095093299fa6311a5..64af7c9b7dd49459e6573e870d40309597849fd3 100644 (file)
@@ -1045,7 +1045,7 @@ TIKA_GOTENBERG_ENDPOINT = os.getenv(
 if TIKA_ENABLED:
     INSTALLED_APPS.append("paperless_tika.apps.PaperlessTikaConfig")
 
-AUDIT_LOG_ENABLED = __get_boolean("PAPERLESS_AUDIT_LOG_ENABLED", "NO")
+AUDIT_LOG_ENABLED = __get_boolean("PAPERLESS_AUDIT_LOG_ENABLED", "true")
 if AUDIT_LOG_ENABLED:
     INSTALLED_APPS.append("auditlog")
     MIDDLEWARE.append("auditlog.middleware.AuditlogMiddleware")
index a6879cdbf07f8c7f02e9cf0e17164b7ef3a3a194..d2ea9102bd9578df2a7d5edb6331e312203a3eaf 100644 (file)
@@ -259,9 +259,6 @@ class TestAuditLogChecks(TestCase):
                 msg = msgs[0]
 
                 self.assertIn(
-                    (
-                        "auditlog table was found but PAPERLESS_AUDIT_LOG_ENABLED"
-                        " is not active."
-                    ),
+                    ("auditlog table was found but audit log is disabled."),
                     msg.msg,
                 )