perf: save_passwd_change filter user source local and passwords not emtpy

This commit is contained in:
feng 2024-07-10 14:10:13 +08:00 committed by Bryan
parent bf82a1c721
commit 2292e6f2eb

View File

@ -67,22 +67,15 @@ def user_authenticated_handle(user, created, source, attrs=None, **kwargs):
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def save_passwd_change(sender, instance: User, **kwargs): def save_passwd_change(sender, instance: User, **kwargs):
if instance.source != User.Source.local.value: if instance.source != User.Source.local.value or not instance.password:
return return
passwords = UserPasswordHistory.objects \ passwords = UserPasswordHistory.objects \
.filter(user=instance) \ .filter(user=instance) \
.order_by('-date_created') \ .order_by('-date_created') \
.values_list('password', flat=True) .values_list('password', flat=True)[:settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT]
passwords = passwords[:int(settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT)]
if not passwords: if instance.password not in list(passwords):
return
for p in passwords:
if instance.password == p:
break
else:
UserPasswordHistory.objects.create( UserPasswordHistory.objects.create(
user=instance, password=instance.password, user=instance, password=instance.password,
date_created=instance.date_password_last_updated date_created=instance.date_password_last_updated