diff --git a/orchestra/apps/systemusers/forms.py b/orchestra/apps/systemusers/forms.py index 04cf3300..78318565 100644 --- a/orchestra/apps/systemusers/forms.py +++ b/orchestra/apps/systemusers/forms.py @@ -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): diff --git a/orchestra/apps/systemusers/models.py b/orchestra/apps/systemusers/models.py index b2264ee3..5f03532c 100644 --- a/orchestra/apps/systemusers/models.py +++ b/orchestra/apps/systemusers/models.py @@ -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) diff --git a/orchestra/apps/systemusers/serializers.py b/orchestra/apps/systemusers/serializers.py index 86e0d690..a7e76b1e 100644 --- a/orchestra/apps/systemusers/serializers.py +++ b/orchestra/apps/systemusers/serializers.py @@ -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):