2015-05-13 12:16:51 +00:00
|
|
|
import os
|
2014-11-14 22:19:58 +00:00
|
|
|
import textwrap
|
|
|
|
|
|
|
|
from django import forms
|
2015-05-11 14:05:39 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2015-05-19 13:27:04 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-11-14 22:19:58 +00:00
|
|
|
|
|
|
|
from orchestra.forms import UserCreationForm, UserChangeForm
|
|
|
|
|
|
|
|
from . import settings
|
|
|
|
from .models import SystemUser
|
2016-02-09 12:17:42 +00:00
|
|
|
from .validators import validate_home, validate_paths_exist
|
2014-11-14 22:19:58 +00:00
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
|
2014-11-14 22:19:58 +00:00
|
|
|
class SystemUserFormMixin(object):
|
|
|
|
MOCK_USERNAME = '<username>'
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(SystemUserFormMixin, self).__init__(*args, **kwargs)
|
|
|
|
duplicate = lambda n: (n, n)
|
|
|
|
if self.instance.pk:
|
|
|
|
username = self.instance.username
|
|
|
|
choices=(
|
|
|
|
duplicate(self.account.main_systemuser.get_base_home()),
|
|
|
|
duplicate(self.instance.get_base_home()),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
username = self.MOCK_USERNAME
|
|
|
|
choices=(
|
|
|
|
duplicate(self.account.main_systemuser.get_base_home()),
|
|
|
|
duplicate(SystemUser(username=username).get_base_home()),
|
|
|
|
)
|
|
|
|
self.fields['home'].widget = forms.Select(choices=choices)
|
|
|
|
if self.instance.pk and (self.instance.is_main or self.instance.has_shell):
|
|
|
|
# hidde home option for shell users
|
2015-05-13 13:52:20 +00:00
|
|
|
self.fields['home'].widget.input_type = 'hidden'
|
|
|
|
self.fields['directory'].widget.input_type = 'hidden'
|
2014-11-14 22:19:58 +00:00
|
|
|
elif self.instance.pk and (self.instance.get_base_home() == self.instance.home):
|
2014-11-14 23:06:14 +00:00
|
|
|
self.fields['directory'].widget = forms.HiddenInput()
|
2015-05-08 14:05:57 +00:00
|
|
|
else:
|
|
|
|
self.fields['directory'].widget = forms.TextInput(attrs={'size':'70'})
|
2014-11-14 23:06:14 +00:00
|
|
|
if not self.instance.pk or not self.instance.is_main:
|
2014-11-14 22:19:58 +00:00
|
|
|
# Some javascript for hidde home/directory inputs when convinient
|
2015-05-13 13:52:20 +00:00
|
|
|
self.fields['shell'].widget.attrs['onChange'] = textwrap.dedent("""\
|
|
|
|
field = $(".field-home, .field-directory");
|
|
|
|
input = $("#id_home, #id_directory");
|
|
|
|
if ($.inArray(this.value, %s) < 0) {
|
|
|
|
field.addClass("hidden");
|
2014-11-14 22:19:58 +00:00
|
|
|
} else {
|
|
|
|
field.removeClass("hidden");
|
|
|
|
input.removeAttr("type");
|
2015-10-05 13:31:08 +00:00
|
|
|
};""" % list(settings.SYSTEMUSERS_DISABLED_SHELLS)
|
2015-05-13 13:52:20 +00:00
|
|
|
)
|
|
|
|
self.fields['home'].widget.attrs['onChange'] = textwrap.dedent("""\
|
|
|
|
field = $(".field-box.field-directory");
|
|
|
|
input = $("#id_directory");
|
|
|
|
if (this.value.search("%s") > 0) {
|
|
|
|
field.addClass("hidden");
|
|
|
|
} else {
|
|
|
|
field.removeClass("hidden");
|
|
|
|
input.removeAttr("type");
|
|
|
|
};""" % username
|
|
|
|
)
|
2014-11-14 22:19:58 +00:00
|
|
|
|
2015-07-29 09:05:07 +00:00
|
|
|
def clean_directory(self):
|
|
|
|
directory = self.cleaned_data['directory']
|
|
|
|
return directory.lstrip('/')
|
|
|
|
|
2014-11-14 22:19:58 +00:00
|
|
|
def clean(self):
|
2015-04-09 14:32:10 +00:00
|
|
|
super(SystemUserFormMixin, self).clean()
|
2015-05-12 12:38:40 +00:00
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
home = cleaned_data.get('home')
|
2015-05-13 13:52:20 +00:00
|
|
|
shell = cleaned_data.get('shell')
|
2014-11-14 22:19:58 +00:00
|
|
|
if home and self.MOCK_USERNAME in home:
|
2015-05-12 12:38:40 +00:00
|
|
|
username = cleaned_data.get('username', '')
|
|
|
|
cleaned_data['home'] = home.replace(self.MOCK_USERNAME, username)
|
2015-05-13 13:52:20 +00:00
|
|
|
elif home and shell not in settings.SYSTEMUSERS_DISABLED_SHELLS:
|
|
|
|
cleaned_data['home'] = ''
|
|
|
|
cleaned_data['directory'] = ''
|
2015-05-12 12:38:40 +00:00
|
|
|
validate_home(self.instance, cleaned_data, self.account)
|
|
|
|
return cleaned_data
|
2014-11-14 22:19:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserCreationForm(SystemUserFormMixin, UserCreationForm):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserChangeForm(SystemUserFormMixin, UserChangeForm):
|
|
|
|
pass
|
2015-05-08 14:05:57 +00:00
|
|
|
|
|
|
|
|
2016-02-09 12:17:42 +00:00
|
|
|
class LinkForm(forms.Form):
|
|
|
|
base_home = forms.ChoiceField(label=_("Target path"), choices=(),
|
|
|
|
help_text=_("Target link will be under this directory."))
|
2015-05-11 14:05:39 +00:00
|
|
|
home_extension = forms.CharField(label=_("Home extension"), required=False, initial='',
|
2015-05-08 14:05:57 +00:00
|
|
|
widget=forms.TextInput(attrs={'size':'70'}), help_text=_("Relative to chosen home."))
|
2016-02-09 12:17:42 +00:00
|
|
|
link_name = forms.CharField(label=_("Link name"), required=False, initial='',
|
|
|
|
widget=forms.TextInput(attrs={'size':'70'}),
|
|
|
|
help_text=_("If left blank or relative path: link will be created in each user home."))
|
2015-05-08 14:05:57 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2015-05-11 14:05:39 +00:00
|
|
|
self.instance = args[0]
|
2016-02-09 12:17:42 +00:00
|
|
|
self.queryset = kwargs.pop('queryset', [])
|
2015-05-08 14:05:57 +00:00
|
|
|
super_args = []
|
|
|
|
if len(args) > 1:
|
|
|
|
super_args.append(args[1])
|
2016-02-09 12:17:42 +00:00
|
|
|
super(LinkForm, self).__init__(*super_args, **kwargs)
|
2015-05-11 14:05:39 +00:00
|
|
|
related_users = type(self.instance).objects.filter(account=self.instance.account_id)
|
|
|
|
self.fields['base_home'].choices = (
|
2015-05-08 14:05:57 +00:00
|
|
|
(user.get_base_home(), user.get_base_home()) for user in related_users
|
|
|
|
)
|
2015-05-11 14:05:39 +00:00
|
|
|
|
2015-07-29 09:05:07 +00:00
|
|
|
def clean_home_extension(self):
|
|
|
|
home_extension = self.cleaned_data['home_extension']
|
|
|
|
return home_extension.lstrip('/')
|
|
|
|
|
2016-02-09 12:17:42 +00:00
|
|
|
def clean_link_name(self):
|
|
|
|
link_name = self.cleaned_data['link_name']
|
|
|
|
if link_name:
|
|
|
|
if link_name.startswith('/'):
|
|
|
|
if len(self.queryset) > 1:
|
|
|
|
raise ValidationError(_("Link name can not be a full path when multiple users."))
|
|
|
|
link_names = [os.path.dirname(link_name)]
|
|
|
|
else:
|
|
|
|
link_names = [os.path.join(user.home, os.path.dirname(link_names)) for user in self.queryset]
|
|
|
|
validate_paths_exist(self.instance, link_names)
|
|
|
|
return link_name
|
|
|
|
|
2015-05-11 14:05:39 +00:00
|
|
|
def clean(self):
|
2016-02-09 12:17:42 +00:00
|
|
|
cleaned_data = super(LinkForm, self).clean()
|
2015-05-12 12:38:40 +00:00
|
|
|
path = os.path.join(cleaned_data['base_home'], cleaned_data['home_extension'])
|
|
|
|
try:
|
2016-02-09 12:17:42 +00:00
|
|
|
validate_paths_exist(self.instance, [path])
|
2015-05-12 12:38:40 +00:00
|
|
|
except ValidationError as err:
|
2015-05-11 14:05:39 +00:00
|
|
|
raise ValidationError({
|
2015-05-12 12:38:40 +00:00
|
|
|
'home_extension': err,
|
2015-05-11 14:05:39 +00:00
|
|
|
})
|
|
|
|
return cleaned_data
|
2016-02-09 12:17:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PermissionForm(LinkForm):
|
|
|
|
set_action = forms.ChoiceField(label=_("Action"), initial='grant',
|
|
|
|
choices=(
|
|
|
|
('grant', _("Grant")),
|
|
|
|
('revoke', _("Revoke"))
|
|
|
|
))
|
|
|
|
base_home = forms.ChoiceField(label=_("Set permissions to"), choices=(),
|
|
|
|
help_text=_("User will be granted/revoked access to this directory."))
|
|
|
|
home_extension = forms.CharField(label=_("Home extension"), required=False, initial='',
|
|
|
|
widget=forms.TextInput(attrs={'size':'70'}), help_text=_("Relative to chosen home."))
|
|
|
|
permissions = forms.ChoiceField(label=_("Permissions"), initial='read-write',
|
|
|
|
choices=(
|
|
|
|
('rw', _("Read and write")),
|
|
|
|
('r', _("Read only")),
|
|
|
|
('w', _("Write only"))
|
|
|
|
))
|