diff --git a/orchestra/contrib/musician/saas/forms.py b/orchestra/contrib/musician/saas/forms.py index fee281dc..496646d1 100644 --- a/orchestra/contrib/musician/saas/forms.py +++ b/orchestra/contrib/musician/saas/forms.py @@ -5,6 +5,7 @@ from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from orchestra.utils.python import random_ascii from django.core.exceptions import ValidationError +from orchestra.core.validators import validate_user_nextcloud from django.forms.widgets import HiddenInput @@ -123,21 +124,17 @@ class NextcloudCreateForm(forms.ModelForm): self.fields['account'].widget = HiddenInput() self.fields['service'].choices = [("nextcloud","nextCloud")] self.fields['password'].help_text = _("Suggestion: %s") % random_ascii(20) - - - - def clean_password2(self): - password = self.cleaned_data.get("password") - password2 = self.cleaned_data.get("password2") + + def clean(self): + cleaned_data = super().clean() + password = cleaned_data.get("password") + password2 = cleaned_data.get("password2") + validate_user_nextcloud(cleaned_data.get("name")) if password and password2 and password != password2: raise ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) - return password - - def clean_password(self): - password = self.cleaned_data.get("password") self.fields['password'] = password self.instance.set_password(password) - \ No newline at end of file + return cleaned_data diff --git a/orchestra/core/validators.py b/orchestra/core/validators.py index 2c6b46ce..ff42ee3f 100644 --- a/orchestra/core/validators.py +++ b/orchestra/core/validators.py @@ -118,6 +118,14 @@ def validate_hostname(hostname): raise ValidationError(_("Not a valid hostname (%s).") % name) +def validate_user_nextcloud(value): + if len(value) > 64: + raise ValidationError(_("Too long for a username.")) + if len(value) < 3: + raise ValidationError(_("Too short for a username.")) + validators.RegexValidator(r'^[a-zA-Z\d.-]+$', _(f"Enter a valid username ({value})."))(value) + + def validate_username(value): validators.RegexValidator(r'^[\w.-]+$', _("Enter a valid username."))(value)