• BaiJiangJie's avatar
    [Feature] 添加功能 RemoteApp (#2706) · 1eca5179
    BaiJiangJie authored
    * [Feature] RemoteApp添加Model
    
    * [Feature] RemoteApp添加ViewSet API
    
    * [Feature] RemoteApp添加获取connection-info API
    
    * [Feature] Perms模块修改目录结构
    
    * [Feature] RemoteAppPermission添加Model
    
    * [Feature] RemoteAppPermission添加ViewSet API
    
    * [Feature] RemoteAppPermission添加用户/用户组获取被授权的RemoteApp API
    
    * [Feature] RemoteAppPermission添加校验用户对RemoteApp的权限 API
    
    * [Feature] RemoteAppPermission添加获取用户授权的RemoteApp树 API
    
    * [Feature] RemoteAppPermission添加<添加/移除>所授权的<用户/RemoteApp> API
    
    * [Feature] RemoteApp添加创建、更新、详情、删除、用户RemoteApp等页面
    
    * [Feature] RemoteAppPermission添加创建、更新、详情、删除、授权用户、授权RemoteApp等页面
    
    * [Feature] RemoteApp从assets模块迁移到新添加的applications模块
    
    * [Feature] RemoteApp/RemoteAppPermission添加迁移文件
    
    * [Feature] RemoteApp/RemoteAppPermission修改小细节
    
    * [Feature] RemoteApp/RemoteAppPermission修改小细节2
    
    * [Feature] RemoteApp/RemoteAppPermission修改小细节3
    
    * [Feature] RemoteApp更新迁移文件
    
    * [Feature] RemoteApp/RemoteAppPermission添加翻译信息
    
    * [Feature] RemoteApp/RemoteAppPermission删除迁移文件
    
    * [Feature] RemoteApp/RemoteAppPermission添加迁移文件
    
    * [Feature] RemoteApp/RemoteAppPermission修改代码风格
    1eca5179
asset_permission.py 2.8 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 import OrgModelForm
from orgs.utils import current_org
from perms.models import AssetPermission
from assets.models import Asset

__all__ = [
    'AssetPermissionForm',
]


class AssetPermissionForm(OrgModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        users_field = self.fields.get('users')
        if hasattr(users_field, 'queryset'):
            users_field.queryset = current_org.get_org_users()
        assets_field = self.fields.get('assets')

        # 前端渲染优化, 防止过多资产
        if not self.data:
            instance = kwargs.get('instance')
            if instance:
                assets_field.queryset = instance.assets.all()
            else:
                assets_field.queryset = Asset.objects.none()

    class Meta:
        model = AssetPermission
        exclude = (
            'id', 'date_created', 'created_by', 'org_id'
        )
        widgets = {
            'users': forms.SelectMultiple(
                attrs={'class': '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': 'select2', 'data-placeholder': _("Node")}
            ),
            'system_users': forms.SelectMultiple(
                attrs={'class': 'select2', 'data-placeholder': _('System user')}
            ),
            'actions': forms.SelectMultiple(
                attrs={'class': 'select2', 'data-placeholder': _('Action')}
            )
        }
        labels = {
            'nodes': _("Node"),
        }
        help_texts = {
            'actions': _('Tips: The RDP protocol does not support separate '
                         'controls for uploading or downloading files')
        }

    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"]