jumpserver/apps/users/forms.py

222 lines
7.1 KiB
Python
Raw Normal View History

2016-08-14 11:18:41 +00:00
# ~*~ coding: utf-8 ~*~
2016-08-16 14:13:06 +00:00
from django import forms
from django.contrib.auth.forms import AuthenticationForm
2016-09-03 16:51:36 +00:00
from django.utils.translation import gettext_lazy as _
from captcha.fields import CaptchaField
2016-11-09 15:49:10 +00:00
from common.utils import validate_ssh_public_key
2016-11-10 08:59:50 +00:00
from perms.models import AssetPermission
2016-08-21 14:37:55 +00:00
from .models import User, UserGroup
2016-08-14 11:18:41 +00:00
class UserLoginForm(AuthenticationForm):
2016-09-03 16:51:36 +00:00
username = forms.CharField(label=_('Username'), max_length=100)
password = forms.CharField(
label=_('Password'), widget=forms.PasswordInput, max_length=100,
strip=False)
2016-09-03 06:37:01 +00:00
captcha = CaptchaField()
2016-08-25 11:29:59 +00:00
2016-11-09 15:49:10 +00:00
class UserCreateUpdateForm(forms.ModelForm):
2016-08-14 11:18:41 +00:00
class Meta:
model = User
fields = [
2016-08-17 14:17:16 +00:00
'username', 'name', 'email', 'groups', 'wechat',
2016-08-20 17:16:30 +00:00
'phone', 'enable_otp', 'role', 'date_expired', 'comment',
2016-08-14 11:18:41 +00:00
]
2016-08-19 16:42:50 +00:00
help_texts = {
'username': '* required',
2016-10-31 11:31:56 +00:00
'name': '* required',
2016-08-19 16:42:50 +00:00
'email': '* required',
}
widgets = {
'groups': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Join user groups')}),
2016-08-19 16:42:50 +00:00
}
2016-08-16 14:13:06 +00:00
2017-03-29 16:51:36 +00:00
class UserProfileForm(forms.ModelForm):
class Meta:
model = User
fields = [
2017-03-30 08:28:00 +00:00
'username', 'name', 'email',
'wechat', 'phone',
2017-03-29 16:51:36 +00:00
]
help_texts = {
'username': '* required',
'name': '* required',
'email': '* required',
}
2017-03-30 08:28:00 +00:00
class UserPasswordForm(forms.Form):
old_password = forms.CharField(
2017-07-19 23:58:16 +00:00
max_length=128, widget=forms.PasswordInput)
2017-03-30 08:28:00 +00:00
new_password = forms.CharField(
min_length=5, max_length=128, widget=forms.PasswordInput)
confirm_password = forms.CharField(
min_length=5, max_length=128, widget=forms.PasswordInput)
def __init__(self, *args, **kwargs):
self.instance = kwargs.pop('instance')
super(UserPasswordForm, self).__init__(*args, **kwargs)
def clean_old_password(self):
old_password = self.cleaned_data['old_password']
if not self.instance.check_password(old_password):
raise forms.ValidationError(_('Old password error'))
return old_password
def clean_confirm_password(self):
new_password = self.cleaned_data['new_password']
confirm_password = self.cleaned_data['confirm_password']
if new_password != confirm_password:
raise forms.ValidationError(_('Password does not match'))
return confirm_password
def save(self):
password = self.cleaned_data['new_password']
self.instance.set_password(password)
self.instance.save()
return self.instance
class UserPublicKeyForm(forms.Form):
public_key = forms.CharField(
label=_('ssh public key'), max_length=5000,
widget=forms.Textarea(attrs={'placeholder': _('ssh-rsa AAAA...')}),
help_text=_('Paste your id_rsa.pub here.'))
def __init__(self, *args, **kwargs):
2017-03-31 15:46:00 +00:00
if 'instance' in kwargs:
self.instance = kwargs.pop('instance')
else:
self.instance = None
2017-03-30 08:28:00 +00:00
super(UserPublicKeyForm, self).__init__(*args, **kwargs)
def clean_public_key(self):
public_key = self.cleaned_data['public_key']
if self.instance.public_key and public_key == self.instance.public_key:
raise forms.ValidationError(_('Public key should not be the '
'same as your old one.'))
if not validate_ssh_public_key(public_key):
raise forms.ValidationError(_('Not a valid ssh public key'))
return public_key
def save(self):
public_key = self.cleaned_data['public_key']
self.instance.public_key = public_key
self.instance.save()
return self.instance
2017-04-10 14:55:15 +00:00
class UserBulkUpdateForm(forms.ModelForm):
2017-04-12 03:50:15 +00:00
role = forms.ChoiceField(
label=_('Role'),
choices=[('Admin', 'Administrator'), ('User', 'User')],
)
users = forms.MultipleChoiceField(
required=True,
help_text='* required',
label=_('Select users'),
2017-04-12 10:06:32 +00:00
# choices=[(user.id, user.name) for user in User.objects.all()],
2017-04-12 03:50:15 +00:00
widget=forms.SelectMultiple(
attrs={
'class': 'select2',
'data-placeholder': _('Select users')
}
)
)
class Meta:
model = User
2017-04-12 03:50:15 +00:00
fields = ['users', 'role', 'groups', 'date_expired', 'is_active', 'enable_otp']
widgets = {
'groups': forms.SelectMultiple(
attrs={'class': 'select2',
'data-placeholder': _('Select user groups')}),
}
def save(self, commit=True):
cleaned_data = {k: v for k, v in self.cleaned_data.items() if
v is not None}
users_id = cleaned_data.pop('users')
groups = cleaned_data.pop('groups')
users = User.objects.filter(id__in=users_id)
users.update(**cleaned_data)
if groups:
for user in users:
user.groups.set(groups)
return users
class UserGroupForm(forms.ModelForm):
2016-08-18 17:39:08 +00:00
class Meta:
model = UserGroup
fields = [
'name', 'comment'
2016-08-18 17:39:08 +00:00
]
2016-08-19 16:42:50 +00:00
help_texts = {
'name': '* required'
}
2016-09-08 13:51:44 +00:00
2016-09-16 17:04:52 +00:00
class UserPrivateAssetPermissionForm(forms.ModelForm):
def save(self, commit=True):
self.instance = super(UserPrivateAssetPermissionForm, self)\
.save(commit=commit)
2016-09-16 17:04:52 +00:00
self.instance.users = [self.user]
self.instance.save()
return self.instance
class Meta:
model = AssetPermission
fields = [
2016-11-04 10:33:16 +00:00
'assets', 'asset_groups', 'system_users', 'name',
2016-09-16 17:04:52 +00:00
]
widgets = {
'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-16 17:04:52 +00:00
}
2016-11-09 15:49:10 +00:00
2016-11-15 11:33:04 +00:00
class UserGroupPrivateAssetPermissionForm(forms.ModelForm):
def save(self, commit=True):
self.instance = super(UserGroupPrivateAssetPermissionForm, self)\
.save(commit=commit)
2016-11-15 11:33:04 +00:00
self.instance.user_groups = [self.user_group]
self.instance.save()
return self.instance
class Meta:
model = AssetPermission
fields = [
'assets', 'asset_groups', 'system_users', 'name',
]
widgets = {
'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-11-15 11:33:04 +00:00
}
2016-11-09 15:49:10 +00:00
class FileForm(forms.Form):
2016-11-24 07:45:08 +00:00
file = forms.FileField()