2019-07-01 11:30:18 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from assets.models import Node, SystemUser, Asset
|
|
|
|
|
from assets.serializers import ProtocolsField
|
2019-07-02 09:51:50 +00:00
|
|
|
|
from .asset_permission import ActionsField
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
2019-08-28 03:43:55 +00:00
|
|
|
|
'NodeGrantedSerializer',
|
|
|
|
|
'AssetGrantedSerializer',
|
2019-07-11 10:12:14 +00:00
|
|
|
|
'ActionsSerializer', 'AssetSystemUserSerializer',
|
2019-09-12 10:25:22 +00:00
|
|
|
|
'RemoteAppSystemUserSerializer',
|
2019-12-19 07:28:17 +00:00
|
|
|
|
'DatabaseAppSystemUserSerializer',
|
2019-07-01 11:30:18 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""
|
|
|
|
|
查看授权的资产系统用户的数据结构,这个和AssetSerializer不同,字段少
|
|
|
|
|
"""
|
2019-07-02 09:51:50 +00:00
|
|
|
|
actions = ActionsField(read_only=True)
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2019-07-11 10:12:14 +00:00
|
|
|
|
only_fields = (
|
2019-09-12 10:25:22 +00:00
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'sftp_root', 'username_same_with_user',
|
2019-07-01 11:30:18 +00:00
|
|
|
|
)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
fields = list(only_fields) + ["actions"]
|
|
|
|
|
read_only_fields = fields
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
|
2019-09-12 10:25:22 +00:00
|
|
|
|
class RemoteAppSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
only_fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
|
|
|
|
)
|
|
|
|
|
fields = list(only_fields)
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2019-12-19 07:28:17 +00:00
|
|
|
|
class DatabaseAppSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
only_fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
|
|
|
|
)
|
|
|
|
|
fields = list(only_fields)
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
class AssetGrantedSerializer(serializers.ModelSerializer):
|
2019-07-01 11:30:18 +00:00
|
|
|
|
"""
|
|
|
|
|
被授权资产的数据结构
|
|
|
|
|
"""
|
2019-07-11 10:12:14 +00:00
|
|
|
|
protocols = ProtocolsField(label=_('Protocols'), required=False, read_only=True)
|
2019-12-18 07:37:53 +00:00
|
|
|
|
platform = serializers.ReadOnlyField(source='platform_base')
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
model = Asset
|
|
|
|
|
only_fields = [
|
|
|
|
|
"id", "hostname", "ip", "protocols", "os", 'domain',
|
2019-07-22 03:25:04 +00:00
|
|
|
|
"platform", "comment", "org_id",
|
2019-07-01 11:30:18 +00:00
|
|
|
|
]
|
2019-08-28 03:43:55 +00:00
|
|
|
|
fields = only_fields + ['org_name']
|
2019-07-11 10:12:14 +00:00
|
|
|
|
read_only_fields = fields
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeGrantedSerializer(serializers.ModelSerializer):
|
2019-08-28 03:43:55 +00:00
|
|
|
|
assets_amount = serializers.SerializerMethodField()
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Node
|
|
|
|
|
fields = [
|
2019-08-28 03:43:55 +00:00
|
|
|
|
'id', 'name', 'key', 'value', 'org_id', "assets_amount"
|
2019-07-02 02:51:48 +00:00
|
|
|
|
]
|
2019-07-11 10:12:14 +00:00
|
|
|
|
read_only_fields = fields
|
2019-07-02 02:51:48 +00:00
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.tree = self.context.get("tree")
|
|
|
|
|
|
|
|
|
|
def get_assets_amount(self, obj):
|
|
|
|
|
if not self.tree:
|
|
|
|
|
return 0
|
|
|
|
|
return self.tree.assets_amount(obj.key)
|
|
|
|
|
|
2019-07-02 02:51:48 +00:00
|
|
|
|
|
|
|
|
|
class ActionsSerializer(serializers.Serializer):
|
2019-07-02 09:51:50 +00:00
|
|
|
|
actions = ActionsField(read_only=True)
|