diff --git a/apps/users/models/user.py b/apps/users/models/user.py index bb9d82f64..3e38cbb18 100644 --- a/apps/users/models/user.py +++ b/apps/users/models/user.py @@ -47,6 +47,10 @@ class AuthMixin: post_user_change_password.send(self.__class__, user=self) super().set_password(raw_password) + def set_public_key(self, public_key): + self.public_key = public_key + self.save() + def can_update_password(self): return self.is_local @@ -79,6 +83,14 @@ class AuthMixin: pass return PubKey() + def get_public_key_comment(self): + return self.public_key_obj.comment + + def get_public_key_hash_md5(self): + if not callable(self.public_key_obj.hash_md5): + return '' + return self.public_key_obj.hash_md5() + def reset_password(self, new_password): self.set_password(new_password) self.save() diff --git a/apps/users/serializers/user.py b/apps/users/serializers/user.py index 20c412d32..437ec9302 100644 --- a/apps/users/serializers/user.py +++ b/apps/users/serializers/user.py @@ -193,8 +193,12 @@ class UserRoleSerializer(serializers.Serializer): class UserProfileSerializer(UserSerializer): admin_or_audit_orgs = UserOrgSerializer(many=True, read_only=True) current_org_roles = serializers.ListField(read_only=True) - public_key_comment = serializers.SerializerMethodField() - public_key_hash_md5 = serializers.SerializerMethodField() + public_key_comment = serializers.CharField( + source='get_public_key_comment', required=False, read_only=True, max_length=128 + ) + public_key_hash_md5 = serializers.CharField( + source='get_public_key_hash_md5', required=False, read_only=True, max_length=128 + ) class Meta(UserSerializer.Meta): fields = UserSerializer.Meta.fields + [ @@ -226,16 +230,6 @@ class UserProfileSerializer(UserSerializer): fields.remove('public_key') extra_kwargs.pop('public_key', None) - @staticmethod - def get_public_key_comment(obj): - return obj.public_key_obj.comment - - @staticmethod - def get_public_key_hash_md5(obj): - if callable(obj.public_key_obj.hash_md5): - return obj.public_key_obj.hash_md5() - return '' - class UserUpdatePasswordSerializer(serializers.ModelSerializer): old_password = serializers.CharField(required=True, max_length=128, write_only=True) @@ -273,4 +267,27 @@ class UserUpdatePasswordSerializer(serializers.ModelSerializer): class UserUpdatePublicKeySerializer(serializers.ModelSerializer): - pass + public_key_comment = serializers.CharField( + source='get_public_key_comment', required=False, read_only=True, max_length=128 + ) + public_key_hash_md5 = serializers.CharField( + source='get_public_key_hash_md5', required=False, read_only=True, max_length=128 + ) + + class Meta: + model = User + fields = ['public_key_comment', 'public_key_hash_md5', 'public_key'] + extra_kwargs = { + 'public_key': {'required': True, 'write_only': True, 'max_length': 2048} + } + + @staticmethod + def validate_public_key(value): + if not validate_ssh_public_key(value): + raise serializers.ValidationError(_('Not a valid ssh public key')) + return value + + def update(self, instance, validated_data): + new_public_key = self.validated_data.get('public_key') + instance.set_public_key(new_public_key) + return instance