mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.8 KiB
84 lines
2.8 KiB
8 years ago
|
# ~*~ coding: utf-8 ~*~
|
||
|
|
||
|
from __future__ import absolute_import, unicode_literals
|
||
|
from django import forms
|
||
|
from django.utils.translation import ugettext_lazy as _
|
||
|
|
||
6 years ago
|
from orgs.mixins import OrgModelForm
|
||
6 years ago
|
from orgs.utils import current_org
|
||
6 years ago
|
from perms.models import AssetPermission
|
||
6 years ago
|
from assets.models import Asset
|
||
8 years ago
|
|
||
6 years ago
|
__all__ = [
|
||
|
'AssetPermissionForm',
|
||
|
]
|
||
|
|
||
8 years ago
|
|
||
6 years ago
|
class AssetPermissionForm(OrgModelForm):
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
super().__init__(*args, **kwargs)
|
||
|
users_field = self.fields.get('users')
|
||
|
if hasattr(users_field, 'queryset'):
|
||
6 years ago
|
users_field.queryset = current_org.get_org_users()
|
||
6 years ago
|
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()
|
||
6 years ago
|
|
||
8 years ago
|
class Meta:
|
||
7 years ago
|
model = AssetPermission
|
||
|
exclude = (
|
||
6 years ago
|
'id', 'date_created', 'created_by', 'org_id'
|
||
7 years ago
|
)
|
||
8 years ago
|
widgets = {
|
||
7 years ago
|
'users': forms.SelectMultiple(
|
||
|
attrs={'class': 'select2', 'data-placeholder': _("User")}
|
||
7 years ago
|
),
|
||
7 years ago
|
'user_groups': forms.SelectMultiple(
|
||
7 years ago
|
attrs={'class': 'select2', 'data-placeholder': _("User group")}
|
||
|
),
|
||
7 years ago
|
'assets': forms.SelectMultiple(
|
||
|
attrs={'class': 'select2', 'data-placeholder': _("Asset")}
|
||
|
),
|
||
|
'nodes': forms.SelectMultiple(
|
||
|
attrs={'class': 'select2', 'data-placeholder': _("Node")}
|
||
|
),
|
||
|
'system_users': forms.SelectMultiple(
|
||
7 years ago
|
attrs={'class': 'select2', 'data-placeholder': _('System user')}
|
||
|
),
|
||
6 years ago
|
'actions': forms.SelectMultiple(
|
||
|
attrs={'class': 'select2', 'data-placeholder': _('Action')}
|
||
|
)
|
||
8 years ago
|
}
|
||
7 years ago
|
labels = {
|
||
|
'nodes': _("Node"),
|
||
|
}
|
||
6 years ago
|
help_texts = {
|
||
|
'actions': _('Tips: The RDP protocol does not support separate '
|
||
|
'controls for uploading or downloading files')
|
||
|
}
|
||
7 years ago
|
|
||
|
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"]
|