Skip to content

Commit

Permalink
Update msg tips of user login (#6708)
Browse files Browse the repository at this point in the history
* Update serializers.py

* Update forms.py

* Update serializers.py

* update-uni-test
  • Loading branch information
r350178982 authored Sep 4, 2024
1 parent 4e7456b commit df5e7be
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
16 changes: 12 additions & 4 deletions seahub/api2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ def validate(self, attrs):
raise serializers.ValidationError('invalid params')

if login_id and password:
user = authenticate(username=login_id, password=password)
if user:
# First check the user is active or not
username = Profile.objects.convert_login_str_to_username(login_id)
if username is None:
username = login_id
try:
user = User.objects.get(username)
if not user.is_active:
raise serializers.ValidationError('User account is disabled.')
else:
except User.DoesNotExist:
pass

# Second check the password correct or not
user = authenticate(username=login_id, password=password)
if not user:
"""try login id/contact email"""
# convert login id or contact email to username if any
username = Profile.objects.convert_login_str_to_username(login_id)
user = authenticate(username=username, password=password)
# After local user authentication process is completed, authenticate LDAP user
if user is None and ENABLE_LDAP:
Expand Down
11 changes: 11 additions & 0 deletions seahub/auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def clean(self):
password = self.cleaned_data.get('password')

if username and password:
# First check user account active or not
email = Profile.objects.convert_login_str_to_username(username)
try:
user = User.objects.get(email=email)
if not user.is_active:
self.errors['inactive'] = _("This account is inactive.")
raise forms.ValidationError(_("This account is inactive."))
except User.DoesNotExist:
pass

# Second check the password correct or not
self.user_cache = authenticate(username=username, password=password)
if self.user_cache is None:
"""then try login id/contact email/primary id"""
Expand Down
3 changes: 2 additions & 1 deletion tests/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def test_inactive_user_incorrect_password(self):
}

s = AuthTokenSerializer(data=d, context={'request': self.fake_request})
self.assertFailed(s)
assert s.is_valid() is False
assert 'User account is disabled.' in s.errors['non_field_errors']

def test_login_failed(self):
d = {
Expand Down
3 changes: 2 additions & 1 deletion tests/seahub/auth/forms/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def test_inactive_user_incorrect_password(self):
}

form = AuthenticationForm(None, data)
self.assertFailed(form)
assert form.is_valid() is False
assert 'This account is inactive.' in form.non_field_errors()

def test_login_success(self):
data = {
Expand Down

0 comments on commit df5e7be

Please sign in to comment.