authentik.enterprise.lifecycle.expiration.signals
1from django.contrib.auth.signals import user_logged_in 2from django.db import transaction 3from django.db.models.signals import post_save, pre_delete 4from django.dispatch import receiver 5from django.http import HttpRequest 6 7from authentik.core.models import User 8from authentik.enterprise.lifecycle.expiration.models import UserExpirationRule 9from authentik.enterprise.lifecycle.offboarding.models import OffboardingStatus 10from authentik.tasks.schedules.models import Schedule 11 12 13@receiver(user_logged_in) 14def expiration_on_user_logged_in(sender, request: HttpRequest, user: User, **_): 15 UserExpirationRule.withdraw_pending(user) 16 17 18@receiver(post_save, sender=User) 19def expiration_on_user_save(sender, instance: User, created: bool, update_fields, **_): 20 """Fallback for explicit `last_login` writes that bypass `login()`. Full saves are 21 skipped, execution rechecks `last_login` anyway.""" 22 if created or instance.last_login is None: 23 return 24 if update_fields is None or "last_login" not in update_fields: 25 return 26 UserExpirationRule.withdraw_pending(instance, logged_in_at=instance.last_login) 27 28 29@receiver(post_save, sender=UserExpirationRule) 30def expiration_post_rule_save(sender, instance: UserExpirationRule, **_): 31 from authentik.enterprise.lifecycle.expiration.tasks import ( 32 apply_expiration_rule, 33 apply_expiration_rules, 34 ) 35 36 # The schedule may not exist yet when blueprints are applied before schedules. 37 schedule = Schedule.objects.filter(actor_name=apply_expiration_rules.actor_name).first() 38 # Queue after commit, so the sweep sees the transports saved with the rule. 39 transaction.on_commit( 40 lambda: apply_expiration_rule.send_with_options(args=(str(instance.pk),), rel_obj=schedule) 41 ) 42 43 44@receiver(pre_delete, sender=UserExpirationRule) 45def expiration_pre_rule_delete(sender, instance: UserExpirationRule, **_): 46 """Pending rows would otherwise lose their rule and run as manual offboardings.""" 47 instance.offboardings.filter(status=OffboardingStatus.PENDING).delete()
@receiver(user_logged_in)
def
expiration_on_user_logged_in( sender, request: django.http.request.HttpRequest, user: authentik.core.models.User, **_):
@receiver(post_save, sender=User)
def
expiration_on_user_save( sender, instance: authentik.core.models.User, created: bool, update_fields, **_):
19@receiver(post_save, sender=User) 20def expiration_on_user_save(sender, instance: User, created: bool, update_fields, **_): 21 """Fallback for explicit `last_login` writes that bypass `login()`. Full saves are 22 skipped, execution rechecks `last_login` anyway.""" 23 if created or instance.last_login is None: 24 return 25 if update_fields is None or "last_login" not in update_fields: 26 return 27 UserExpirationRule.withdraw_pending(instance, logged_in_at=instance.last_login)
Fallback for explicit last_login writes that bypass login(). Full saves are
skipped, execution rechecks last_login anyway.
@receiver(post_save, sender=UserExpirationRule)
def
expiration_post_rule_save( sender, instance: authentik.enterprise.lifecycle.expiration.models.UserExpirationRule, **_):
30@receiver(post_save, sender=UserExpirationRule) 31def expiration_post_rule_save(sender, instance: UserExpirationRule, **_): 32 from authentik.enterprise.lifecycle.expiration.tasks import ( 33 apply_expiration_rule, 34 apply_expiration_rules, 35 ) 36 37 # The schedule may not exist yet when blueprints are applied before schedules. 38 schedule = Schedule.objects.filter(actor_name=apply_expiration_rules.actor_name).first() 39 # Queue after commit, so the sweep sees the transports saved with the rule. 40 transaction.on_commit( 41 lambda: apply_expiration_rule.send_with_options(args=(str(instance.pk),), rel_obj=schedule) 42 )
@receiver(pre_delete, sender=UserExpirationRule)
def
expiration_pre_rule_delete( sender, instance: authentik.enterprise.lifecycle.expiration.models.UserExpirationRule, **_):
45@receiver(pre_delete, sender=UserExpirationRule) 46def expiration_pre_rule_delete(sender, instance: UserExpirationRule, **_): 47 """Pending rows would otherwise lose their rule and run as manual offboardings.""" 48 instance.offboardings.filter(status=OffboardingStatus.PENDING).delete()
Pending rows would otherwise lose their rule and run as manual offboardings.