jumpserver/apps/users/forms.py

121 lines
4.0 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-08-21 14:37:55 +00:00
from .models import User, UserGroup
2016-09-16 17:04:52 +00:00
from .hands import AssetPermission
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-09-06 07:09:00 +00:00
class UserCreateForm(forms.ModelForm):
2016-09-08 13:51:44 +00:00
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',
'email': '* required',
}
widgets = {
2016-09-05 12:27:44 +00:00
'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
class UserUpdateForm(forms.ModelForm):
2016-08-17 14:17:16 +00:00
class Meta:
model = User
fields = [
2016-08-22 16:39:07 +00:00
'name', 'email', 'groups', 'wechat',
2016-08-20 17:16:30 +00:00
'phone', 'enable_otp', 'role', 'date_expired', 'comment',
2016-08-17 14:17:16 +00:00
]
2016-08-19 16:42:50 +00:00
help_texts = {
'username': '* required',
'email': '* required',
'groups': '* required'
}
2016-08-21 14:37:55 +00:00
widgets = {
2016-09-06 07:09:00 +00:00
'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Join user groups')}),
2016-08-21 14:37:55 +00:00
}
2016-08-18 17:39:08 +00:00
class UserGroupForm(forms.ModelForm):
2016-08-18 17:39:08 +00:00
class Meta:
model = UserGroup
fields = [
'name', 'comment',
]
2016-08-19 16:42:50 +00:00
help_texts = {
'name': '* required'
}
2016-09-08 13:51:44 +00:00
class UserInfoForm(forms.Form):
2016-09-10 05:16:58 +00:00
name = forms.CharField(max_length=20, label=_('name'))
avatar = forms.ImageField(label=_('avatar'), required=False)
wechat = forms.CharField(max_length=30, label=_('wechat'), required=False)
phone = forms.CharField(max_length=20, label=_('phone'), required=False)
enable_otp = forms.BooleanField(required=False, label=_('enable otp'))
2016-09-08 13:51:44 +00:00
class UserKeyForm(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_ras.pub here.'))
def clean_public_key(self):
2016-09-18 06:28:34 +00:00
public_key = self.cleaned_data['public_key']
if self.user._public_key and public_key == self.user.public_key:
raise forms.ValidationError(_('Public key should not be the same as your old one.'))
from sshpubkeys import SSHKey
from sshpubkeys.exceptions import InvalidKeyException
ssh = SSHKey(public_key)
try:
ssh.parse()
except InvalidKeyException as e:
print e
raise forms.ValidationError(_('Not a valid ssh public key'))
except NotImplementedError as e:
print e
raise forms.ValidationError(_('Not a valid ssh public key'))
return public_key
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-17 05:04:26 +00:00
self.instance.private_for = 'U'
2016-09-16 17:04:52 +00:00
self.instance.users = [self.user]
self.instance.save()
return self.instance
class Meta:
model = AssetPermission
fields = [
'assets', 'asset_groups', 'system_users', 'private_for', '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')}),
}