2020-12-05 21:08:42 +00:00
|
|
|
"""authentik policy signals"""
|
2020-05-24 00:06:54 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2021-01-01 14:39:43 +00:00
|
|
|
from structlog.stdlib import get_logger
|
2020-05-24 00:06:54 +00:00
|
|
|
|
2021-01-16 21:27:36 +00:00
|
|
|
from authentik.core.api.applications import user_app_cache_key
|
|
|
|
|
2020-05-24 00:06:54 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def invalidate_policy_cache(sender, instance, **_):
|
|
|
|
"""Invalidate Policy cache when policy is updated"""
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.policies.models import Policy, PolicyBinding
|
2020-05-24 00:06:54 +00:00
|
|
|
|
|
|
|
if isinstance(instance, Policy):
|
|
|
|
total = 0
|
|
|
|
for binding in PolicyBinding.objects.filter(policy=instance):
|
2020-05-23 20:01:38 +00:00
|
|
|
prefix = (
|
|
|
|
f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*"
|
|
|
|
)
|
2020-05-24 00:06:54 +00:00
|
|
|
keys = cache.keys(prefix)
|
|
|
|
total += len(keys)
|
|
|
|
cache.delete_many(keys)
|
2020-07-07 15:05:31 +00:00
|
|
|
LOGGER.debug("Invalidating policy cache", policy=instance, keys=total)
|
2021-01-16 21:27:36 +00:00
|
|
|
# Also delete user application cache
|
2021-03-13 20:10:13 +00:00
|
|
|
keys = cache.keys(user_app_cache_key("*")) or []
|
2021-01-16 21:27:36 +00:00
|
|
|
cache.delete_many(keys)
|