• BaiJiangJie's avatar
    Dev (#2838) · 9ca4a8c9
    BaiJiangJie authored
    * Dev ansible windows 2 (#2783)
    
    * [Update] 改密支持windows
    
    * [Update] 修改asset表结构
    
    * [Feature] Windows支持批量改密、测试可连接性等功能
    
    * [Update] 处理创建资产时labels的问题
    
    * [Update] 优化测试管理系统、系统用户可连接性任务执行逻辑
    
    * [Update] 优化ansible任务逻辑;添加自动推送rdp系统用户功能
    
    * [Update] 添加翻译
    
    * [Update] 优化ansible任务逻辑(测试系统用户可连接性, 通过协议过滤资产)
    
    * [Update] 更新翻译
    
    * [Update] 更新翻译
    
    * [Update] 推送windows系统用户,默认添加到Users、Remote Desktop Users组中
    
    * [Update] 优化小细节
    
    * [Update] 更新翻译,删除多余代码
    
    * [Update] 更新翻译信息
    
    * [Bugfix] 修复windows推送系统用户小bug (#2794)
    
    * [Update] 邮件设置添加配置项:发送账号 (#2796)
    
    * [Bugfix] 和资产相关的Serializer添加protocols字段; (#2800)
    
    * [Bugfix] 和资产相关的Serializer添加protocols字段;
    
    * [Bugfix] RemoteApp Form 修改过滤RDP协议资产
    
    * [Bugfix] 修改小问题
    
    * [Update] 用户授权相关API,如果需要切换到root org (#2803)
    
    * [Update] 用户授权相关API,如果需要切换到root org
    
    * [Update] 优化小问题
    
    * [Update] 增加审计员权限控制 (#2792)
    
    * [Update] 审计员
    
    * [Update] 增加审计员的权限控制
    
    * [Update] 增加审计员Api全校的控制
    
    * [Update] 优化auditor的api权限控制
    
    * [Update] 优化审计员权限控制
    
    * [Update]优化管理员权限的View
    
    * [Update] 优化超级管理权限的View
    
    * [Update] 添加审计员切换组织查询会话管理数据
    
    * [Update] 前端禁用审计员在线会话终断按钮
    
    * [Update]优化细节问题
    
    * [Update] Auth Info (#2806)
    
    * [Update] 修改支持auth info导出
    
    * [Update] 统一认证查看
    
    * [Update] 修改auth book manager
    
    * [Update] 修改auth info
    
    * [Update] 完成修改auth info
    
    * [Update] 优化api
    
    * [Update] 修改assets 的related
    
    * [Update] serializer mixin继承 (#2810)
    
    * [Update] serializer mixin继承
    
    * [Update] 修改system user更新serialzier
    
    * [Update] 修改success message
    
    * [Update] 添加一键禁用LDAP认证脚本 (#2813)
    
    * [Update] 修改资产创建格式
    
    * [Update] 兼容之前的protocols格式
    
    * [Update] Merge master_bugfix to dev_bugfix (#2817)
    
    * [Update] 邮件设置添加配置项:发送账号 (#2795)
    
    * [Bugfix] 修复普通用户被授权的RemoteApp列表加载为空的bug
    
    * [Bugfix] 修复普通用户加载被授权的RemoteApp为空的bug
    
    * [Update] 修改邮件测试的接受者为发送者
    
    * [Update] 修改小问题
    
    * [Update] 修改资产授权序列类返回资产protocols的协议格式/, 同时添加protocol和port字段
    
    * [Update] 修改文案 (#2823)
    
    * [Update] 修改文案
    
    * [Update] 修改文案2
    
    * [Bugfix] 修复资产没有管理用户时获取connectivity字段失败的bug
    
    * [Update] 优化测试可连接性时结果获取 (#2825)
    
    * [Update] 修改资产使用patch方法更新时页面不提示messages信息
    
    * [Update] 添加迁移文件,修改设置资产可连接性时管理用户为None的bug
    
    * [Update] 修改org.middleware自动切换组织的bug (#2829)
    
    * [Update] 修改org.middleware自动切换组织的bug
    
    * [Update] 将切换组织逻辑移动到PermsUtil中
    
    * [Update] 修改首页组织名称显示来源
    Unverified
    9ca4a8c9
remote_app.py 3.02 KB
# coding: utf-8
#


from rest_framework import serializers

from common.serializers import AdaptedBulkListSerializer
from orgs.mixins import BulkOrgResourceModelSerializer

from .. import const
from ..models import RemoteApp


__all__ = [
    'RemoteAppSerializer', 'RemoteAppConnectionInfoSerializer',
]


class RemoteAppParamsDictField(serializers.DictField):
    """
    RemoteApp field => params
    """
    @staticmethod
    def filter_attribute(attribute, instance):
        """
        过滤掉params字段值中write_only特性的key-value值
        For example, the chrome_password field is not returned when serializing
        {
            'chrome_target': 'http://www.jumpserver.org/',
            'chrome_username': 'admin',
            'chrome_password': 'admin',
        }
        """
        for field in const.REMOTE_APP_TYPE_MAP_FIELDS[instance.type]:
            if field.get('write_only', False):
                attribute.pop(field['name'], None)
        return attribute

    def get_attribute(self, instance):
        """
        序列化时调用
        """
        attribute = super().get_attribute(instance)
        attribute = self.filter_attribute(attribute, instance)
        return attribute

    @staticmethod
    def filter_value(dictionary, value):
        """
        过滤掉不属于当前app_type所包含的key-value值
        """
        app_type = dictionary.get('type', const.REMOTE_APP_TYPE_CHROME)
        fields = const.REMOTE_APP_TYPE_MAP_FIELDS[app_type]
        fields_names = [field['name'] for field in fields]
        no_need_keys = [k for k in value.keys() if k not in fields_names]
        for k in no_need_keys:
            value.pop(k)
        return value

    def get_value(self, dictionary):
        """
        反序列化时调用
        """
        value = super().get_value(dictionary)
        value = self.filter_value(dictionary, value)
        return value


class RemoteAppSerializer(BulkOrgResourceModelSerializer):
    params = RemoteAppParamsDictField()

    class Meta:
        model = RemoteApp
        list_serializer_class = AdaptedBulkListSerializer
        fields = [
            'id', 'name', 'asset', 'system_user', 'type', 'path', 'params',
            'comment', 'created_by', 'date_created', 'asset_info',
            'system_user_info', 'get_type_display',
        ]
        read_only_fields = [
            'created_by', 'date_created', 'asset_info',
            'system_user_info', 'get_type_display'
        ]


class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer):
    parameter_remote_app = serializers.SerializerMethodField()

    class Meta:
        model = RemoteApp
        fields = [
            'id', 'name', 'asset', 'system_user', 'parameter_remote_app',
        ]
        read_only_fields = ['parameter_remote_app']

    @staticmethod
    def get_parameter_remote_app(obj):
        parameter = {
            'program': const.REMOTE_APP_BOOT_PROGRAM_NAME,
            'working_directory': '',
            'parameters': obj.parameters,
        }
        return parameter