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 _
|
|
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-06-19 08:45:14 +00:00
|
|
|
|
from orgs.mixins import BulkOrgResourceModelSerializer
|
|
|
|
|
from ..models import SystemUser
|
2018-03-27 10:34:41 +00:00
|
|
|
|
from .base import AuthSerializer
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
|
class SystemUserSerializer(BulkOrgResourceModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
|
"""
|
|
|
|
|
系统用户
|
|
|
|
|
"""
|
2019-05-21 08:24:01 +00:00
|
|
|
|
password = serializers.CharField(
|
|
|
|
|
required=False, write_only=True, label=_('Password')
|
|
|
|
|
)
|
|
|
|
|
unreachable_amount = serializers.SerializerMethodField(
|
|
|
|
|
label=_('Unreachable')
|
|
|
|
|
)
|
|
|
|
|
unreachable_assets = serializers.SerializerMethodField(
|
|
|
|
|
label=_('Unreachable assets')
|
|
|
|
|
)
|
|
|
|
|
reachable_assets = serializers.SerializerMethodField(
|
|
|
|
|
label=_('Reachable assets')
|
|
|
|
|
)
|
|
|
|
|
reachable_amount = serializers.SerializerMethodField(label=_('Reachable'))
|
|
|
|
|
assets_amount = serializers.SerializerMethodField(label=_('Asset'))
|
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 = [
|
2019-06-19 08:45:14 +00:00
|
|
|
|
'id', 'name', 'username', 'login_mode', 'login_mode_display',
|
2019-05-21 08:24:01 +00:00
|
|
|
|
'login_mode_display', 'priority', 'protocol', 'auto_push',
|
|
|
|
|
'password', 'assets_amount', 'reachable_amount', 'reachable_assets',
|
|
|
|
|
'unreachable_amount', 'unreachable_assets', 'cmd_filters', 'sudo',
|
|
|
|
|
'shell', 'comment', 'nodes', 'assets'
|
|
|
|
|
]
|
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
2019-06-19 08:45:14 +00:00
|
|
|
|
'created_by': {'read_only': True},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_unreachable_assets(obj):
|
2018-12-18 09:28:45 +00:00
|
|
|
|
return obj.assets_unreachable
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_reachable_assets(obj):
|
2018-12-18 09:28:45 +00:00
|
|
|
|
return obj.assets_reachable
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
def get_unreachable_amount(self, obj):
|
|
|
|
|
return len(self.get_unreachable_assets(obj))
|
|
|
|
|
|
|
|
|
|
def get_reachable_amount(self, obj):
|
|
|
|
|
return len(self.get_reachable_assets(obj))
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_assets_amount(obj):
|
2018-12-18 09:28:45 +00:00
|
|
|
|
return len(obj.get_related_assets())
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
|
class SystemUserAuthSerializer(AuthSerializer):
|
2018-03-09 04:53:08 +00:00
|
|
|
|
"""
|
|
|
|
|
系统用户认证信息
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
fields = [
|
|
|
|
|
"id", "name", "username", "protocol",
|
2018-07-17 04:06:47 +00:00
|
|
|
|
"login_mode", "password", "private_key",
|
2018-03-09 04:53:08 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
class AssetSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""
|
|
|
|
|
查看授权的资产系统用户的数据结构,这个和AssetSerializer不同,字段少
|
|
|
|
|
"""
|
2019-04-22 03:42:20 +00:00
|
|
|
|
actions = serializers.SerializerMethodField()
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2018-06-11 11:09:24 +00:00
|
|
|
|
fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority',
|
2019-04-22 03:42:20 +00:00
|
|
|
|
'protocol', 'comment', 'login_mode', 'actions',
|
2018-06-11 11:09:24 +00:00
|
|
|
|
)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-04-22 03:42:20 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_actions(obj):
|
|
|
|
|
return [action.name for action in obj.actions]
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|