fix: Create ssh_key set is_active error

pull/15056/head
wangruidong 2025-03-18 11:26:46 +08:00
parent 428a4470c9
commit 06ac6ea240
3 changed files with 14 additions and 9 deletions

View File

@ -5,10 +5,9 @@ from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from common.serializers.fields import ReadableHiddenField, LabeledChoiceField
from ..models import SSHKey
from common.utils import validate_ssh_public_key
from users.exceptions import CreateSSHKeyExceedLimit
from ..models import SSHKey
__all__ = ['SSHKeySerializer', 'GenerateKeyType']
@ -21,6 +20,7 @@ class GenerateKeyType(TextChoices):
class SSHKeySerializer(serializers.ModelSerializer):
user = ReadableHiddenField(default=serializers.CurrentUserDefault())
is_active = serializers.BooleanField(default=True, label=_('Active'))
public_key_comment = serializers.CharField(
source='get_public_key_comment', required=False, read_only=True, max_length=128
)

View File

@ -18,7 +18,6 @@ from common.utils import (
lazyproperty,
)
from users.signals import post_user_change_password
from users.exceptions import CreateSSHKeyExceedLimit
logger = get_logger(__file__)
@ -134,10 +133,13 @@ class AuthMixin:
post_user_change_password.send(self.__class__, user=self)
super().set_password(raw_password) # noqa
def set_ssh_key(self, name, public_key, private_key):
def set_ssh_key(self, public_key, private_key, **kwargs):
if self.can_update_ssh_key():
from authentication.models import SSHKey
SSHKey.objects.create(name=name, public_key=public_key, private_key=private_key, user=self)
SSHKey.objects.create(
public_key=public_key, private_key=private_key, user=self, name=kwargs.get('name'),
comment=kwargs.get('comment'), is_active=kwargs.get('is_active')
)
post_user_change_password.send(self.__class__, user=self)
def can_create_ssh_key(self):

View File

@ -1,10 +1,11 @@
# ~*~ coding: utf-8 ~*~
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.views import View
from common.utils import get_logger, ssh_key_gen
from authentication.serializers import SSHKeySerializer
from common.permissions import IsValidUser
from common.utils import get_logger, ssh_key_gen
from common.views.mixins import PermissionsMixin
from users.exceptions import CreateSSHKeyExceedLimit
@ -18,13 +19,15 @@ class UserPublicKeyGenerateView(PermissionsMixin, View):
def get(self, request, *args, **kwargs):
username = request.user.username
key_name = request.GET.get('name', '')
serializer = SSHKeySerializer(data=request.GET, context={'request': request})
if not serializer.is_valid():
return JsonResponse(serializer.errors, status=400)
if not request.user.can_create_ssh_key():
return HttpResponse(
CreateSSHKeyExceedLimit().default_detail, status=400
)
private, public = ssh_key_gen(username=username, hostname='jumpserver')
request.user.set_ssh_key(key_name, public, private)
request.user.set_ssh_key(public, private, **serializer.validated_data)
response = HttpResponse(private, content_type='text/plain')
filename = "{0}-jumpserver.pem".format(username)
response['Content-Disposition'] = 'attachment; filename={}'.format(filename)