• BaiJiangJie's avatar
    [Feature] 添加功能:数据库应用 (#3551) · 16f727c6
    BaiJiangJie authored
    * [Update] 添加数据库应用Model
    
    * [Update] 添加数据库应用ViewSet
    
    * [Update] 添加数据库应用HTML
    
    * [Update] 更新数据库应用迁移文件
    
    * [Update] 添加数据库应用授权Model
    
    * [Update] 添加数据库应用授权ViewSet(待续)
    
    * [Update] 添加数据库应用授权ViewSet(完结)
    
    * [Update] 添加数据库应用授权View(待续)
    
    * [Update] 添加数据库应用授权View(待续2)
    
    * [Update] 修改远程应用授权View(小问题)
    
    * [Update] 添加数据库应用授权View(待续3)
    
    * [Update] 添加数据库应用授权View(完结)
    
    * [Update] 添加数据库应用授权相关API
    
    * [Update] 添加数据库应用View(用户页面)
    
    * [Update] 修改数据库应用授权Model/View/API(系统用户)
    
    * [Update] 修改系统用户Model/View(添加mysql协议)
    
    * [Update] 修改用户页面(我的应用)
    
    * [Update] 添加迁移文件
    
    * [Update] 添加迁移文件2
    
    * [Update] 续添加迁移文件2(Model更改)
    
    * [Update] 修改系统用户序列类(mysql协议自动生成密码问题)
    
    * [Update] 修改数据库应用/资产等授权序列类
    
    * [Update] 修改命令列表/会话详情命令溢出
    
    * [Update] 修改授权详情中添加系统用户的过滤
    
    * [Update] 修改列表动作的宽度
    16f727c6
asset_permission.py 3.91 KB
# ~*~ coding: utf-8 ~*~

from __future__ import absolute_import, unicode_literals
from django import forms
from django.utils.translation import ugettext_lazy as _

from orgs.mixins.forms import OrgModelForm
from assets.models import Asset, Node, SystemUser
from ..models import AssetPermission, Action

__all__ = [
    'AssetPermissionForm',
]


class ActionField(forms.MultipleChoiceField):
    def __init__(self, *args, **kwargs):
        kwargs['choices'] = Action.CHOICES
        kwargs['initial'] = Action.ALL
        kwargs['label'] = _("Action")
        kwargs['widget'] = forms.CheckboxSelectMultiple()
        kwargs['help_text'] = _(
            'Tips: The RDP protocol does not support separate controls '
            'for uploading or downloading files'
        )
        super().__init__(*args, **kwargs)

    def to_python(self, value):
        value = super().to_python(value)
        return Action.choices_to_value(value)

    def prepare_value(self, value):
        if value is None:
            return value
        value = Action.value_to_choices(value)
        return value


class AssetPermissionForm(OrgModelForm):
    actions = ActionField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if self.data:
            return
        # 前端渲染优化, 防止过多资产
        users_field = self.fields.get('users')
        assets_field = self.fields['assets']
        nodes_field = self.fields['nodes']
        if self.instance:
            assets_field.queryset = self.instance.assets.all()
            nodes_field.queryset = self.instance.nodes.all()
            users_field.queryset = self.instance.users.all()
        else:
            assets_field.queryset = Asset.objects.none()
            nodes_field.queryset = Node.objects.none()
            users_field.queryset = []

        # 过滤系统用户
        system_users_field = self.fields.get('system_users')
        system_users_field.queryset = SystemUser.objects.exclude(
            protocol=SystemUser.PROTOCOL_MYSQL
        )

    def set_nodes_initial(self, nodes):
        field = self.fields['nodes']
        field.choices = [(n.id, n.full_value) for n in nodes]
        field.initial = nodes

    def set_assets_initial(self, assets):
        field = self.fields['assets']
        field.choices = [(a.id, a.hostname) for a in assets]
        field.initial = assets

    class Meta:
        model = AssetPermission
        exclude = (
            'id', 'date_created', 'created_by', 'org_id'
        )
        widgets = {
            'users': forms.SelectMultiple(
                attrs={'class': 'users-select2', 'data-placeholder': _("User")}
            ),
            'user_groups': forms.SelectMultiple(
                attrs={'class': 'select2', 'data-placeholder': _("User group")}
            ),
            'assets': forms.SelectMultiple(
                attrs={'class': 'select2', 'data-placeholder': _("Asset")}
            ),
            'nodes': forms.SelectMultiple(
                attrs={'class': 'nodes-select2', 'data-placeholder': _("Node")}
            ),
            'system_users': forms.SelectMultiple(
                attrs={'class': 'select2', 'data-placeholder': _('System user')}
            ),
        }
        labels = {
            'nodes': _("Node"),
        }

    def clean_user_groups(self):
        users = self.cleaned_data.get('users')
        user_groups = self.cleaned_data.get('user_groups')

        if not users and not user_groups:
            raise forms.ValidationError(
                _("User or group at least one required"))
        return self.cleaned_data["user_groups"]

    def clean_asset_groups(self):
        assets = self.cleaned_data.get('assets')
        asset_groups = self.cleaned_data.get('asset_groups')

        if not assets and not asset_groups:
            raise forms.ValidationError(
                _("Asset or group at least one required"))

        return self.cleaned_data["asset_groups"]