Fixed errors with systemuser form and serializer

This commit is contained in:
Marc 2014-11-14 23:50:06 +00:00
parent 66fb997f65
commit 3348218c62
3 changed files with 25 additions and 11 deletions

View file

@ -62,6 +62,7 @@ class SystemUserFormMixin(object):
if home and self.MOCK_USERNAME in home:
username = self.cleaned_data.get('username', '')
self.cleaned_data['home'] = home.replace(self.MOCK_USERNAME, username)
self.instance.validate_home(self.cleaned_data, self.account)
class SystemUserCreationForm(SystemUserFormMixin, UserCreationForm):

View file

@ -88,6 +88,29 @@ class SystemUser(models.Model):
'directory': directory_error,
})
def validate_home(self, data, account):
""" validates home based on account and data['shell'] """
if not 'username' in data and not self.pk:
# other validation will have raised for required username
return
user = type(self)(
username=data.get('username') or self.username,
shell=data.get('shell') or self.shell,
)
if 'home' in data and data['home']:
home = data['home'].rstrip('/')
user_home = user.get_home().rstrip('/')
account_home = account.main_systemuser.get_home().rstrip('/')
if user.has_shell:
if home != user_home:
raise ValidationError({
'home': _("Not a valid home directory.")
})
elif home not in (user_home, account_home):
raise ValidationError({
'home': _("Not a valid home directory.")
})
def set_password(self, raw_password):
self.password = make_password(raw_password)

View file

@ -40,17 +40,7 @@ class SystemUserSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
username=attrs.get('username') or self.object.username,
shell=attrs.get('shell') or self.object.shell,
)
if 'home' in attrs and attrs['home']:
home = attrs['home'].rstrip('/') + '/'
if user.has_shell:
if home != user.get_base_home():
raise ValidationError({
'home': _("Not a valid home directory.")
})
elif home not in (user.get_home(), self.account.main_systemuser.get_home()):
raise ValidationError({
'home': _("Not a valid home directory.")
})
user.validate_home(attrs, self.account)
return attrs
def validate_password(self, attrs, source):