From f5ff3ddd5c21cb9f0b6f8a288f0154f21c83989a Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 13 Dec 2018 16:18:28 +0100 Subject: [PATCH 1/2] manager: dont require username or email for passwordless accounts (fixes #28916) --- src/authentic2/manager/forms.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/authentic2/manager/forms.py b/src/authentic2/manager/forms.py index 8ef7f073..1232b3f4 100644 --- a/src/authentic2/manager/forms.py +++ b/src/authentic2/manager/forms.py @@ -193,7 +193,9 @@ class UserEditForm(LimitQuerysetFormMixin, CssClass, BaseUserForm): self.data._mutable = False def clean(self): - if 'username' in self.fields or 'email' in self.fields: + if (self.instance.has_usable_password() and ( + 'username' in self.fields or + 'email' in self.fields)): if not self.cleaned_data.get('username') and \ not self.cleaned_data.get('email'): raise forms.ValidationError( @@ -234,6 +236,7 @@ class UserChangePasswordForm(CssClass, forms.ModelForm): } notification_template_prefix = \ 'authentic2/manager/change-password-notification' + require_password = True def clean_password2(self): password1 = self.cleaned_data.get("password1") @@ -247,16 +250,17 @@ class UserChangePasswordForm(CssClass, forms.ModelForm): def clean(self): super(UserChangePasswordForm, self).clean() - if not self.cleaned_data.get('generate_password') \ - and not self.cleaned_data.get('password1') \ - and not self.cleaned_data.get('send_password_reset'): + if (self.require_password and + not self.cleaned_data.get('generate_password') and + not self.cleaned_data.get('password1') and + not self.cleaned_data.get('send_password_reset')): raise forms.ValidationError( _('You must choose password generation or type a new' ' one or send a password reset mail')) if (self.instance and self.instance.pk and not self.instance.email and - (self.cleaned_data.get('send_mail') - or self.cleaned_data.get('generate_password' - or self.cleaned_data.get('send_password_reset')))): + (self.cleaned_data.get('send_mail') or + self.cleaned_data.get('generate_password' or + self.cleaned_data.get('send_password_reset')))): raise forms.ValidationError( _('User does not have a mail, we cannot send the ' 'informations to him.')) @@ -309,6 +313,7 @@ class UserChangePasswordForm(CssClass, forms.ModelForm): class UserAddForm(UserChangePasswordForm, UserEditForm): css_class = "user-form" form_id = "id_user_add_form" + require_password = False notification_template_prefix = \ 'authentic2/manager/new-account-notification' @@ -328,10 +333,16 @@ class UserAddForm(UserChangePasswordForm, UserEditForm): def clean(self): super(UserAddForm, self).clean() - User = get_user_model() - - if not self.cleaned_data.get('username') and \ - not self.cleaned_data.get('email'): + # check if this account is going to be real online account, i.e. with a + # password, it it's the case complain that there is no identifiers. + has_password = ( + self.cleaned_data.get('new_password1') or + self.cleaned_data.get('generate_password') or + self.cleaned_data.get('send_password_reset')) + + if (has_password and + not self.cleaned_data.get('username') and + not self.cleaned_data.get('email')): raise forms.ValidationError( _('You must set a username or an email.')) -- 2.18.0