2018-02-07 15:25:15 +00:00
|
|
|
|
from rest_framework import serializers
|
2019-05-21 08:24:01 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from django.db.models import Count
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
2021-01-01 23:25:23 +00:00
|
|
|
|
from common.drf.serializers import AdaptedBulkListSerializer
|
2019-12-05 07:09:25 +00:00
|
|
|
|
from common.mixins.serializers import BulkSerializerMixin
|
2019-08-01 09:10:02 +00:00
|
|
|
|
from common.utils import ssh_pubkey_gen
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from ..models import SystemUser, Asset
|
|
|
|
|
from .base import AuthSerializerMixin
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
__all__ = [
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'SystemUserSerializer', 'SystemUserListSerializer',
|
2019-12-05 07:09:25 +00:00
|
|
|
|
'SystemUserSimpleSerializer', 'SystemUserAssetRelationSerializer',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'SystemUserNodeRelationSerializer', 'SystemUserTaskSerializer',
|
|
|
|
|
'SystemUserUserRelationSerializer', 'SystemUserWithAuthInfoSerializer',
|
2019-12-05 07:09:25 +00:00
|
|
|
|
]
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
|
"""
|
|
|
|
|
系统用户
|
|
|
|
|
"""
|
2019-07-08 07:35:20 +00:00
|
|
|
|
auto_generate_key = serializers.BooleanField(initial=True, required=False, write_only=True)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2019-04-25 02:11:50 +00:00
|
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2019-05-21 08:24:01 +00:00
|
|
|
|
fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'id', 'name', 'username', 'protocol',
|
|
|
|
|
'password', 'public_key', 'private_key',
|
|
|
|
|
'login_mode', 'login_mode_display',
|
|
|
|
|
'priority', 'username_same_with_user',
|
2019-10-18 07:12:17 +00:00
|
|
|
|
'auto_push', 'cmd_filters', 'sudo', 'shell', 'comment',
|
2020-08-07 03:52:34 +00:00
|
|
|
|
'auto_generate_key', 'sftp_root', 'token',
|
2021-03-11 12:09:23 +00:00
|
|
|
|
'assets_amount', 'date_created', 'date_updated', 'created_by',
|
2020-10-28 11:13:43 +00:00
|
|
|
|
'home', 'system_groups', 'ad_domain'
|
2019-05-21 08:24:01 +00:00
|
|
|
|
]
|
|
|
|
|
extra_kwargs = {
|
2019-06-25 06:32:25 +00:00
|
|
|
|
'password': {"write_only": True},
|
|
|
|
|
'public_key': {"write_only": True},
|
|
|
|
|
'private_key': {"write_only": True},
|
2020-08-07 03:52:34 +00:00
|
|
|
|
'token': {"write_only": True},
|
2020-11-11 02:27:18 +00:00
|
|
|
|
'nodes_amount': {'label': _('Nodes amount')},
|
|
|
|
|
'assets_amount': {'label': _('Assets amount')},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
2019-06-19 08:45:14 +00:00
|
|
|
|
'created_by': {'read_only': True},
|
2020-11-13 06:46:25 +00:00
|
|
|
|
'ad_domain': {'required': False, 'allow_blank': True, 'label': _('Ad domain')},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_auto_push(self, value):
|
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
|
|
|
|
|
|
|
|
|
if login_mode == SystemUser.LOGIN_MANUAL or \
|
|
|
|
|
protocol in [SystemUser.PROTOCOL_TELNET,
|
|
|
|
|
SystemUser.PROTOCOL_VNC]:
|
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def validate_auto_generate_key(self, value):
|
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
|
|
|
|
|
|
|
|
|
if self.context["request"].method.lower() != "post":
|
|
|
|
|
value = False
|
|
|
|
|
elif self.instance:
|
|
|
|
|
value = False
|
|
|
|
|
elif login_mode == SystemUser.LOGIN_MANUAL:
|
|
|
|
|
value = False
|
|
|
|
|
elif protocol in [SystemUser.PROTOCOL_TELNET, SystemUser.PROTOCOL_VNC]:
|
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
def validate_username_same_with_user(self, username_same_with_user):
|
|
|
|
|
if not username_same_with_user:
|
|
|
|
|
return username_same_with_user
|
|
|
|
|
protocol = self.initial_data.get("protocol", "ssh")
|
|
|
|
|
queryset = SystemUser.objects.filter(
|
|
|
|
|
protocol=protocol, username_same_with_user=True
|
|
|
|
|
)
|
|
|
|
|
if self.instance:
|
|
|
|
|
queryset = queryset.exclude(id=self.instance.id)
|
|
|
|
|
exists = queryset.exists()
|
|
|
|
|
if not exists:
|
|
|
|
|
return username_same_with_user
|
|
|
|
|
error = _("Username same with user with protocol {} only allow 1").format(protocol)
|
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_username(self, username):
|
|
|
|
|
if username:
|
|
|
|
|
return username
|
2019-07-08 10:30:18 +00:00
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
2020-03-12 08:24:38 +00:00
|
|
|
|
username_same_with_user = self.initial_data.get("username_same_with_user")
|
|
|
|
|
if username_same_with_user:
|
|
|
|
|
return ''
|
2019-07-08 07:35:20 +00:00
|
|
|
|
if login_mode == SystemUser.LOGIN_AUTO and \
|
|
|
|
|
protocol != SystemUser.PROTOCOL_VNC:
|
|
|
|
|
msg = _('* Automatic login mode must fill in the username.')
|
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
return username
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
def validate_sftp_root(self, value):
|
|
|
|
|
if value in ['home', 'tmp']:
|
|
|
|
|
return value
|
|
|
|
|
if not value.startswith('/'):
|
|
|
|
|
error = _("Path should starts with /")
|
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
|
return value
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_password(self, password):
|
|
|
|
|
super().validate_password(password)
|
|
|
|
|
auto_gen_key = self.initial_data.get("auto_generate_key", False)
|
|
|
|
|
private_key = self.initial_data.get("private_key")
|
2019-08-05 02:01:03 +00:00
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
if not self.instance and not auto_gen_key and not password and \
|
|
|
|
|
not private_key and login_mode == SystemUser.LOGIN_AUTO:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
raise serializers.ValidationError(_("Password or private key required"))
|
|
|
|
|
return password
|
|
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
username = attrs.get("username", "manual")
|
2019-12-19 07:28:17 +00:00
|
|
|
|
auto_gen_key = attrs.pop("auto_generate_key", False)
|
2019-07-08 07:35:20 +00:00
|
|
|
|
protocol = attrs.get("protocol")
|
2019-12-19 07:28:17 +00:00
|
|
|
|
|
|
|
|
|
if protocol not in [SystemUser.PROTOCOL_RDP, SystemUser.PROTOCOL_SSH]:
|
|
|
|
|
return attrs
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
if auto_gen_key:
|
|
|
|
|
password = SystemUser.gen_password()
|
|
|
|
|
attrs["password"] = password
|
|
|
|
|
if protocol == SystemUser.PROTOCOL_SSH:
|
|
|
|
|
private_key, public_key = SystemUser.gen_key(username)
|
|
|
|
|
attrs["private_key"] = private_key
|
|
|
|
|
attrs["public_key"] = public_key
|
2019-08-01 09:10:02 +00:00
|
|
|
|
# 如果设置了private key,没有设置public key则生成
|
|
|
|
|
elif attrs.get("private_key", None):
|
|
|
|
|
private_key = attrs["private_key"]
|
|
|
|
|
password = attrs.get("password")
|
|
|
|
|
public_key = ssh_pubkey_gen(private_key, password=password,
|
|
|
|
|
username=username)
|
|
|
|
|
attrs["public_key"] = public_key
|
2019-07-08 07:35:20 +00:00
|
|
|
|
return attrs
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
class SystemUserListSerializer(SystemUserSerializer):
|
2020-08-11 07:38:19 +00:00
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
class Meta(SystemUserSerializer.Meta):
|
|
|
|
|
fields = [
|
|
|
|
|
'id', 'name', 'username', 'protocol',
|
2020-09-14 03:38:48 +00:00
|
|
|
|
'password', 'public_key', 'private_key',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'login_mode', 'login_mode_display',
|
|
|
|
|
'priority', "username_same_with_user",
|
|
|
|
|
'auto_push', 'sudo', 'shell', 'comment',
|
2020-08-11 07:38:19 +00:00
|
|
|
|
"assets_amount", 'home', 'system_groups',
|
2020-10-28 11:13:43 +00:00
|
|
|
|
'auto_generate_key', 'ad_domain',
|
2021-03-11 12:09:23 +00:00
|
|
|
|
'sftp_root', 'created_by', 'date_created',
|
|
|
|
|
'date_updated',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
]
|
2020-09-14 03:38:48 +00:00
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'password': {"write_only": True},
|
|
|
|
|
'public_key': {"write_only": True},
|
|
|
|
|
'private_key': {"write_only": True},
|
2020-11-11 02:27:18 +00:00
|
|
|
|
'nodes_amount': {'label': _('Nodes amount')},
|
|
|
|
|
'assets_amount': {'label': _('Assets amount')},
|
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
|
|
|
|
'created_by': {'read_only': True},
|
|
|
|
|
'ad_domain': {'label': _('Ad domain')},
|
2020-09-14 03:38:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
2020-03-12 08:24:38 +00:00
|
|
|
|
queryset = queryset.annotate(assets_amount=Count("assets"))
|
2019-07-04 07:36:57 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
class SystemUserWithAuthInfoSerializer(SystemUserSerializer):
|
|
|
|
|
class Meta(SystemUserSerializer.Meta):
|
2020-03-16 08:59:45 +00:00
|
|
|
|
fields = [
|
|
|
|
|
'id', 'name', 'username', 'protocol',
|
|
|
|
|
'password', 'public_key', 'private_key',
|
|
|
|
|
'login_mode', 'login_mode_display',
|
|
|
|
|
'priority', 'username_same_with_user',
|
|
|
|
|
'auto_push', 'sudo', 'shell', 'comment',
|
2020-10-28 11:13:43 +00:00
|
|
|
|
'auto_generate_key', 'sftp_root', 'token',
|
|
|
|
|
'ad_domain',
|
2020-03-16 08:59:45 +00:00
|
|
|
|
]
|
2020-03-12 08:24:38 +00:00
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'nodes_amount': {'label': _('Node')},
|
|
|
|
|
'assets_amount': {'label': _('Asset')},
|
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
|
|
|
|
'created_by': {'read_only': True},
|
|
|
|
|
}
|
2020-01-07 07:53:53 +00:00
|
|
|
|
|
2018-03-09 04:53:08 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
class SystemUserSimpleSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""
|
|
|
|
|
系统用户最基本信息的数据结构
|
|
|
|
|
"""
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2018-12-18 09:28:45 +00:00
|
|
|
|
fields = ('id', 'name', 'username')
|
|
|
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
class RelationMixin(BulkSerializerMixin, serializers.Serializer):
|
|
|
|
|
systemuser_display = serializers.ReadOnlyField()
|
|
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
|
fields.extend(['systemuser', "systemuser_display"])
|
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserAssetRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
|
asset_display = serializers.ReadOnlyField()
|
|
|
|
|
|
|
|
|
|
class Meta(RelationMixin.Meta):
|
|
|
|
|
model = SystemUser.assets.through
|
|
|
|
|
fields = [
|
|
|
|
|
'id', "asset", "asset_display",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserNodeRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
|
node_display = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
|
|
class Meta(RelationMixin.Meta):
|
|
|
|
|
model = SystemUser.nodes.through
|
|
|
|
|
fields = [
|
|
|
|
|
'id', 'node', "node_display",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def get_node_display(self, obj):
|
2020-09-29 09:13:32 +00:00
|
|
|
|
return obj.node.full_value
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserUserRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
|
user_display = serializers.ReadOnlyField()
|
|
|
|
|
|
|
|
|
|
class Meta(RelationMixin.Meta):
|
|
|
|
|
model = SystemUser.users.through
|
|
|
|
|
fields = [
|
|
|
|
|
'id', "user", "user_display",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserTaskSerializer(serializers.Serializer):
|
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
|
("test", "test"),
|
|
|
|
|
("push", "push"),
|
|
|
|
|
)
|
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
|
|
|
|
asset = serializers.PrimaryKeyRelatedField(
|
|
|
|
|
queryset=Asset.objects, allow_null=True, required=False, write_only=True
|
|
|
|
|
)
|
2020-12-08 09:29:22 +00:00
|
|
|
|
assets = serializers.PrimaryKeyRelatedField(
|
|
|
|
|
queryset=Asset.objects, allow_null=True, required=False, write_only=True,
|
|
|
|
|
many=True
|
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
task = serializers.CharField(read_only=True)
|