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

This commit is contained in:
shamoon 2024-04-04 11:51:15 -07:00 committed by GitHub
parent 00b04c2e86
commit 0f8b2e69c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 16 deletions

View 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}

View 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

View 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():

View 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")

View 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."),
),
)

View 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")

View 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,
)