From e8ec8d994f8a00154879d987642d4a1cc29ef191 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 16 Jan 2024 11:43:09 +0100 Subject: [PATCH] delegate enabled check to apps Signed-off-by: Jens Langhammer --- authentik/enterprise/apps.py | 11 +++++++++++ authentik/enterprise/middleware.py | 4 ++-- authentik/enterprise/signals.py | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/authentik/enterprise/apps.py b/authentik/enterprise/apps.py index b2b06306f..8f486c666 100644 --- a/authentik/enterprise/apps.py +++ b/authentik/enterprise/apps.py @@ -1,4 +1,6 @@ """Enterprise app config""" +from functools import lru_cache + from django.conf import settings from authentik.blueprints.apps import ManagedAppConfig @@ -25,3 +27,12 @@ class AuthentikEnterpriseConfig(EnterpriseConfig): orig_import = "authentik.events.middleware.AuditMiddleware" new_import = "authentik.enterprise.middleware.EnterpriseAuditMiddleware" settings.MIDDLEWARE = [new_import if x == orig_import else x for x in settings.MIDDLEWARE] + + def enabled(self): + return self.check_enabled() + + @lru_cache() + def check_enabled(self): + from authentik.enterprise.models import LicenseKey + + return LicenseKey.get_total().is_valid() diff --git a/authentik/enterprise/middleware.py b/authentik/enterprise/middleware.py index d4f012dcc..dba5095ca 100644 --- a/authentik/enterprise/middleware.py +++ b/authentik/enterprise/middleware.py @@ -4,6 +4,7 @@ from functools import partial from typing import Callable from deepdiff import DeepDiff +from django.apps.registry import apps from django.core.files import File from django.db import connection from django.db.models import Model @@ -12,7 +13,6 @@ from django.db.models.signals import post_init from django.http import HttpRequest, HttpResponse from authentik.core.models import User -from authentik.enterprise.models import LicenseKey from authentik.events.middleware import AuditMiddleware, should_log_model @@ -23,7 +23,7 @@ class EnterpriseAuditMiddleware(AuditMiddleware): def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]): super().__init__(get_response) - self._enabled = LicenseKey.get_total().is_valid() + self._enabled = apps.get_app_config("authentik_enterprise").enabled() def connect(self, request: HttpRequest): super().connect(request) diff --git a/authentik/enterprise/signals.py b/authentik/enterprise/signals.py index 67e7c5745..a967e8d1d 100644 --- a/authentik/enterprise/signals.py +++ b/authentik/enterprise/signals.py @@ -1,6 +1,7 @@ """Enterprise signals""" from datetime import datetime +from django.apps.registry import apps from django.db.models.signals import pre_save from django.dispatch import receiver from django.utils.timezone import get_current_timezone @@ -16,3 +17,4 @@ def pre_save_license(sender: type[License], instance: License, **_): instance.internal_users = status.internal_users instance.external_users = status.external_users instance.expiry = datetime.fromtimestamp(status.exp, tz=get_current_timezone()) + apps.get_app_config("authentik_enterprise").check_enabled.cache_clear()