From 2292e6f2eb28b564f92ea9ab280c4473f2926546 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Wed, 10 Jul 2024 14:10:13 +0800 Subject: [PATCH] perf: save_passwd_change filter user source local and passwords not emtpy --- apps/users/signal_handlers.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/apps/users/signal_handlers.py b/apps/users/signal_handlers.py index ef1006e26..250064182 100644 --- a/apps/users/signal_handlers.py +++ b/apps/users/signal_handlers.py @@ -67,22 +67,15 @@ def user_authenticated_handle(user, created, source, attrs=None, **kwargs): @receiver(post_save, sender=User) 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 passwords = UserPasswordHistory.objects \ .filter(user=instance) \ .order_by('-date_created') \ - .values_list('password', flat=True) - passwords = passwords[:int(settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT)] + .values_list('password', flat=True)[:settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT] - if not passwords: - return - - for p in passwords: - if instance.password == p: - break - else: + if instance.password not in list(passwords): UserPasswordHistory.objects.create( user=instance, password=instance.password, date_created=instance.date_password_last_updated