From 3926ee9eb6e39eda40260884fb017e3e69e296ba Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 30 Jan 2021 18:12:14 +0100 Subject: [PATCH] core: clear application cache upon application creation --- authentik/core/apps.py | 5 +++++ authentik/core/signals.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/authentik/core/apps.py b/authentik/core/apps.py index 395737f39..830601a6d 100644 --- a/authentik/core/apps.py +++ b/authentik/core/apps.py @@ -1,4 +1,6 @@ """authentik core app config""" +from importlib import import_module + from django.apps import AppConfig @@ -9,3 +11,6 @@ class AuthentikCoreConfig(AppConfig): label = "authentik_core" verbose_name = "authentik Core" mountpoint = "" + + def ready(self): + import_module("authentik.core.signals") diff --git a/authentik/core/signals.py b/authentik/core/signals.py index ef493518f..3bc4493ec 100644 --- a/authentik/core/signals.py +++ b/authentik/core/signals.py @@ -1,5 +1,24 @@ """authentik core signals""" +from django.core.cache import cache from django.core.signals import Signal +from django.db.models.signals import post_save +from django.dispatch import receiver # Arguments: user: User, password: str password_changed = Signal() + + +@receiver(post_save) +# pylint: disable=unused-argument +def post_save_application(sender, instance, created: bool, **_): + """Clear user's application cache upon application creation""" + from authentik.core.models import Application + from authentik.core.api.applications import user_app_cache_key + + if sender != Application: + return + if not created: + return + # Also delete user application cache + keys = cache.keys(user_app_cache_key("*")) + cache.delete_many(keys)