1from django.apps import apps
2from django.utils.translation import gettext_lazy as _
3from dramatiq import actor
4
5from authentik.enterprise.lifecycle.expiration.models import UserExpirationRule
6from authentik.tasks.middleware import CurrentTask
7
8
9@actor(description=_("Apply all enabled user expiration rules."))
10def apply_expiration_rules():
11 if not apps.get_app_config("authentik_enterprise").enabled():
12 return
13 task = CurrentTask.get_task()
14 for rule in UserExpirationRule.objects.filter(enabled=True):
15 # Keep one failing rule from blocking the rest of the sweep.
16 try:
17 rule.apply()
18 except Exception as exc: # noqa: BLE001
19 task.error(exc, rule=rule.name)
20
21
22@actor(description=_("Apply user expiration rule."))
23def apply_expiration_rule(rule_pk: str):
24 if not apps.get_app_config("authentik_enterprise").enabled():
25 return
26 rule = UserExpirationRule.objects.filter(pk=rule_pk).first()
27 if rule:
28 rule.apply()