Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: validating the new given password to be not same as current #791

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions djoser/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __getattribute__(self, item):
"SEND_CONFIRMATION_EMAIL": False,
"USER_CREATE_PASSWORD_RETYPE": False,
"SET_PASSWORD_RETYPE": False,
"DUPLICATION_PASSWORD_CHECK": False,
"PASSWORD_RESET_CONFIRM_RETYPE": False,
"SET_USERNAME_RETYPE": False,
"USERNAME_RESET_CONFIRM_RETYPE": False,
Expand Down
5 changes: 5 additions & 0 deletions djoser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ def validate(self, attrs):
try:
uid = utils.decode_uid(self.initial_data.get("uid", ""))
self.user = User.objects.get(pk=uid)
# check's the new given password to be not same as current password in DB
if settings.DUPLICATION_PASSWORD_CHECK:
new_password = self.initial_data.get("new_password" , None)
if new_password and self.user.check_password(new_password):
raise ValidationError({"password": "New password cannot be the same as current password"})
except (User.DoesNotExist, ValueError, TypeError, OverflowError):
key_error = "invalid_uid"
raise ValidationError(
Expand Down