@@ -57,3 +57,38 @@ def validate(self, attrs: UserAdminSignInSerializerData) -> UserAdminSignInSeria
5757 raise serializers .PermissionDenied ("Only permissioned users can sign in using this route." )
5858
5959 return attrs
60+
61+
62+ class UserAdminPasswordChangeSerializerData (typing .TypedDict ):
63+ old_password : str
64+ new_password : str
65+ new_password_confirm : str
66+
67+
68+ class UserAdminPasswordChangeSerializer (JsonSchemaSerializer , ReadOnlyModelSerializer ):
69+ old_password = serializers .CharField (write_only = True , required = True )
70+ new_password = serializers .CharField (write_only = True , required = True )
71+ new_password_confirm = serializers .CharField (write_only = True , required = True )
72+
73+ class Meta :
74+ model = UserExt
75+ fields = ("old_password" , "new_password" , "new_password_confirm" )
76+
77+ def validate (self , attrs : UserAdminPasswordChangeSerializerData ) -> UserAdminPasswordChangeSerializerData :
78+ user : UserExt = self .instance
79+ if not user .check_password (attrs ["old_password" ]):
80+ raise serializers .ValidationError ("Old password is incorrect." )
81+
82+ if attrs ["old_password" ] == attrs ["new_password" ]:
83+ raise serializers .ValidationError ("New password cannot be the same as the old password." )
84+
85+ if attrs ["new_password" ] != attrs ["new_password_confirm" ]:
86+ raise serializers .ValidationError ("New password and confirmation do not match." )
87+
88+ return attrs
89+
90+ def save (self , ** kwargs : typing .Any ) -> UserExt :
91+ user : UserExt = self .instance
92+ user .set_password (self .validated_data ["new_password" ])
93+ user .save (update_fields = ["password" ])
94+ return user
0 commit comments