From b1daa4d3573968bd513d192633309a144e689b2c Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:39:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=B8=8D=E5=B8=B8?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=94=A8=E6=88=B7=E9=94=81=E5=AE=9A=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20(#11576)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- apps/users/tasks.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/apps/users/tasks.py b/apps/users/tasks.py index c363940fe..add7a9ed7 100644 --- a/apps/users/tasks.py +++ b/apps/users/tasks.py @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- # +from datetime import timedelta from celery import shared_task from django.conf import settings -from django.db.models import Max from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from audits.models import UserLoginLog from common.const.crontab import CRONTAB_AT_AM_TEN, CRONTAB_AT_PM_TWO from common.utils import get_logger from common.utils.timezone import utc_now @@ -84,17 +83,7 @@ def check_user_expired_periodic(): @register_as_period_task(crontab=CRONTAB_AT_PM_TWO) @tmp_to_root_org() def check_unused_users(): - now = utc_now() - unused_usernames = [] - usernames_max_datetime = UserLoginLog.objects.values('username').annotate(max_datetime=Max('datetime')) - for i in usernames_max_datetime: - username = i['username'] - max_datetime = i['max_datetime'] - uncommon_users_ttl = settings.SECURITY_UNCOMMON_USERS_TTL - if (now - max_datetime).seconds > uncommon_users_ttl * 24 * 60 * 60: - unused_usernames.append(username) - - if not unused_usernames: - return - - User.objects.filter(username__in=unused_usernames).update(is_active=False) + uncommon_users_ttl = settings.SECURITY_UNCOMMON_USERS_TTL + seconds_to_subtract = uncommon_users_ttl * 24 * 60 * 60 + t = utc_now() - timedelta(seconds=seconds_to_subtract) + User.objects.filter(last_login__lte=t).update(is_active=False)