Browse Source

[Update] 修改校验用户有效性逻辑

pull/3554/head
BaiJiangJie 5 years ago
parent
commit
b90960e6e1
  1. 24
      apps/authentication/utils.py

24
apps/authentication/utils.py

@ -1,31 +1,20 @@
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext as _
from django.contrib.auth import authenticate
from common.utils import (
get_ip_city, get_object_or_none, validate_ip
)
from users.models import User
from . import errors
def check_user_valid(**kwargs):
password = kwargs.pop('password', None)
public_key = kwargs.pop('public_key', None)
email = kwargs.pop('email', None)
username = kwargs.pop('username', None)
request = kwargs.get('request')
if username:
user = get_object_or_none(User, username=username)
elif email:
user = get_object_or_none(User, email=email)
else:
user = None
if user is None:
return None, errors.reason_user_not_exist
user = authenticate(request, username=username,
password=password, public_key=public_key)
if not user:
return None, errors.reason_password_failed
elif user.is_expired:
return None, errors.reason_user_inactive
elif not user.is_active:
@ -33,9 +22,4 @@ def check_user_valid(**kwargs):
elif user.password_has_expired:
return None, errors.reason_password_expired
if password or public_key:
user = authenticate(request, username=username,
password=password, public_key=public_key)
if user:
return user, ''
return None, errors.reason_password_failed

Loading…
Cancel
Save