jumpserver/apps/perms/forms.py

98 lines
3.7 KiB
Python
Raw Normal View History

2016-09-10 13:08:10 +00:00
# ~*~ coding: utf-8 ~*~
from __future__ import absolute_import, unicode_literals
from django import forms
from django.utils.translation import ugettext_lazy as _
2016-09-11 14:45:24 +00:00
# from .hands import User, UserGroup, Asset, AssetGroup, SystemUser
from .models import AssetPermission
2017-12-29 18:29:29 +00:00
from users.models import User
2016-09-10 13:08:10 +00:00
2016-09-11 14:45:24 +00:00
class AssetPermissionForm(forms.ModelForm):
2017-12-29 18:29:29 +00:00
users = forms.ModelMultipleChoiceField(
queryset=User.objects.exclude(role=User.ROLE_APP),
widget=forms.SelectMultiple(
attrs={'class': 'select2', 'data-placeholder': _('Select users')},
2017-12-31 13:51:25 +00:00
),
label=_("User"),
required=False,
2017-12-29 18:29:29 +00:00
)
2016-09-10 13:08:10 +00:00
class Meta:
2016-09-11 14:45:24 +00:00
model = AssetPermission
2016-09-10 13:08:10 +00:00
fields = [
2016-09-11 14:45:24 +00:00
'name', 'users', 'user_groups', 'assets', 'asset_groups',
2016-09-16 01:55:26 +00:00
'system_users', 'is_active', 'date_expired', 'comment',
2016-09-10 13:08:10 +00:00
]
2016-09-11 08:59:19 +00:00
widgets = {
2017-01-20 12:13:22 +00:00
'user_groups': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select user groups')}),
'assets': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select assets')}),
'asset_groups': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select asset groups')}),
'system_users': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select system users')}),
2016-09-11 14:45:24 +00:00
}
help_texts = {
'name': '* required',
'system_users': '* required',
2016-09-11 08:59:19 +00:00
}
2017-12-21 03:31:13 +00:00
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"]
2017-12-21 03:31:13 +00:00
def clean_system_users(self):
from assets.utils import check_assets_have_system_user
errors = []
assets = self.cleaned_data['assets']
asset_groups = self.cleaned_data.get('asset_groups')
system_users = self.cleaned_data.get('system_users')
if not asset_groups and not assets:
return self.cleaned_data.get("system_users")
2017-12-21 03:31:13 +00:00
error_data = check_assets_have_system_user(assets, system_users)
if error_data:
for asset, system_users in error_data.items():
msg = _("Asset {} of cluster {} not have [{}] system users, please check \n")
2017-12-21 03:31:13 +00:00
error = forms.ValidationError(msg.format(
asset.hostname,
asset.cluster.name,
2017-12-21 03:31:13 +00:00
", ".join(system_user.name for system_user in system_users)
))
errors.append(error)
for group in asset_groups:
msg = _("Asset {}(group {}) of cluster {} not have [{}] system users, please check \n")
2017-12-21 03:31:13 +00:00
assets = group.assets.all()
error_data = check_assets_have_system_user(assets, system_users)
for asset, system_users in error_data.items():
errors.append(msg.format(
asset.hostname, group.name, asset.cluster.name,
2017-12-21 03:31:13 +00:00
", ".join(system_user.name for system_user in system_users)
))
if errors:
raise forms.ValidationError(errors)
return self.cleaned_data['system_users']