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

Fix change password error #3536

Merged
14 changes: 11 additions & 3 deletions mathesar/api/ui/serializers/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.password_validation import validate_password
from rest_access_policy import FieldAccessMixin, PermittedPkRelatedField
from rest_framework import serializers
import django.contrib.auth.password_validation as validators

from mathesar.api.db.permissions.database import DatabaseAccessPolicy
from mathesar.api.db.permissions.schema import SchemaAccessPolicy
Expand Down Expand Up @@ -71,23 +72,30 @@


class ChangePasswordSerializer(MathesarErrorMessageMixin, serializers.Serializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password = serializers.CharField(write_only=True, required=True)
old_password = serializers.CharField(write_only=True, required=True)

def validate_old_password(self, value):
user = self.context['request'].user
if user.check_password(value) is True:
return value
return value
raise IncorrectOldPassword(field='old_password')

def validate_password(self, value):
try:
validate_password(value)
except serializers.ValidationError as exc:
hitenvidhani marked this conversation as resolved.
Show resolved Hide resolved
raise serializers.ValidationError(str(exc))
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
return value

def update(self, instance, validated_data):
instance.set_password(validated_data['password'])
instance.save()
return instance


class PasswordResetSerializer(MathesarErrorMessageMixin, serializers.Serializer):
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password = serializers.CharField(write_only=True, required=True)


class DatabaseRoleSerializer(MathesarErrorMessageMixin, serializers.ModelSerializer):
Expand Down
7 changes: 4 additions & 3 deletions mathesar/tests/api/test_user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ def test_user_password_reset_non_superuser(client_bob, user_bob):


def test_user_password_change(client_bob, user_bob):
hitenvidhani marked this conversation as resolved.
Show resolved Hide resolved
new_password = 'new_password'
new_password = 'NewPass0!'
old_password = 'password'
data = {
'password': new_password,
'old_password': 'password'
'password': 'NewPass0!',
hitenvidhani marked this conversation as resolved.
Show resolved Hide resolved
'old_password': old_password
}
response = client_bob.post('/api/ui/v0/users/password_change/', data=data)
assert response.status_code == 200
Expand Down
Loading