1from django.utils.translation import gettext_lazy as _
2from dramatiq import actor
3
4from authentik.core.models import User
5from authentik.enterprise.lifecycle.review.models import LifecycleRule
6from authentik.events.models import Event, Notification, NotificationTransport
7from authentik.tasks.middleware import CurrentTask
8
9
10@actor(description=_("Dispatch tasks to apply lifecycle rules."))
11def apply_lifecycle_rules():
12 task = CurrentTask.get_task()
13 for rule in LifecycleRule.objects.all():
14 apply_lifecycle_rule.send_with_options(
15 args=(rule.id,),
16 rel_obj=task.rel_obj,
17 )
18
19
20@actor(description=_("Apply lifecycle rule."))
21def apply_lifecycle_rule(rule_id: str):
22 rule = LifecycleRule.objects.filter(pk=rule_id).first()
23 if rule:
24 rule.apply()
25
26
27@actor(description=_("Send lifecycle rule notification."))
28def send_notification(transport_pk: int | None, event_pk: str, user_pk: int, severity: str):
29 event = Event.objects.filter(pk=event_pk).first()
30 if not event:
31 return
32 user = User.objects.filter(pk=user_pk).first()
33 if not user:
34 return
35
36 notification = Notification(
37 severity=severity,
38 body=event.summary,
39 event=event,
40 user=user,
41 hyperlink=event.hyperlink,
42 hyperlink_label=event.hyperlink_label,
43 )
44 if transport_pk is None:
45 NotificationTransport().send_local(notification)
46 return
47 transport = NotificationTransport.objects.filter(pk=transport_pk).first()
48 if not transport:
49 return
50 transport.send(notification)