Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Change: enable auditlog by default, fix import / export (#6267)

shamoon0f8b2e69c930
diff --git a/docs/configuration.md b/docs/configuration.md
index 981d27b27..1b5cc510d 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -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}
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py
index faa42ba31..b02fd686b 100644
--- a/src/documents/management/commands/document_exporter.py
+++ b/src/documents/management/commands/document_exporter.py
@@ -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
diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.py
index 88ac2b91a..dc0ac36fd 100644
--- a/src/documents/management/commands/document_importer.py
+++ b/src/documents/management/commands/document_importer.py
@@ -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():
diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py
index 226b89694..b95d07dec 100644
--- a/src/documents/tests/test_management_exporter.py
+++ b/src/documents/tests/test_management_exporter.py
@@ -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")
diff --git a/src/paperless/checks.py b/src/paperless/checks.py
index 0bfdce2cd..cbc8da5cf 100644
--- a/src/paperless/checks.py
+++ b/src/paperless/checks.py
@@ -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."),
),
)
diff --git a/src/paperless/settings.py b/src/paperless/settings.py
index a583ef406..64af7c9b7 100644
--- a/src/paperless/settings.py
+++ b/src/paperless/settings.py
@@ -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")
diff --git a/src/paperless/tests/test_checks.py b/src/paperless/tests/test_checks.py
index a6879cdbf..d2ea9102b 100644
--- a/src/paperless/tests/test_checks.py
+++ b/src/paperless/tests/test_checks.py
@@ -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,
)