Projet

Général

Profil

0001-manager-dont-require-username-or-email-for-passwordl.patch

Benjamin Dauvergne, 14 décembre 2018 21:15

Télécharger (5,25 ko)

Voir les différences:

Subject: [PATCH 1/2] manager: dont require username or email for passwordless
 accounts (fixes #28916)

 src/authentic2/manager/forms.py | 45 +++++++++++++++++++++++----------
 tests/test_manager.py           |  1 +
 2 files changed, 32 insertions(+), 14 deletions(-)
src/authentic2/manager/forms.py
193 193
                self.data._mutable = False
194 194

  
195 195
    def clean(self):
196
        if 'username' in self.fields or 'email' in self.fields:
196
        if (self.instance.has_usable_password() and (
197
                'username' in self.fields or
198
                'email' in self.fields)):
197 199
            if not self.cleaned_data.get('username') and \
198 200
               not self.cleaned_data.get('email'):
199 201
                raise forms.ValidationError(
......
234 236
    }
235 237
    notification_template_prefix = \
236 238
        'authentic2/manager/change-password-notification'
239
    require_password = True
237 240

  
238 241
    def clean_password2(self):
239 242
        password1 = self.cleaned_data.get("password1")
......
247 250

  
248 251
    def clean(self):
249 252
        super(UserChangePasswordForm, self).clean()
250
        if not self.cleaned_data.get('generate_password') \
251
                and not self.cleaned_data.get('password1') \
252
                and not self.cleaned_data.get('send_password_reset'):
253
        if (self.require_password and
254
                not self.cleaned_data.get('generate_password') and
255
                not self.cleaned_data.get('password1') and
256
                not self.cleaned_data.get('send_password_reset')):
253 257
            raise forms.ValidationError(
254 258
                _('You must choose password generation or type a new'
255 259
                  '  one or send a password reset mail'))
256
        if (self.instance and self.instance.pk and not self.instance.email and
257
            (self.cleaned_data.get('send_mail')
258
             or self.cleaned_data.get('generate_password'
259
             or self.cleaned_data.get('send_password_reset')))):
260
        if (not self.has_email() and
261
            (self.cleaned_data.get('send_mail') or
262
             self.cleaned_data.get('generate_password' or
263
             self.cleaned_data.get('send_password_reset')))):
260 264
            raise forms.ValidationError(
261 265
                _('User does not have a mail, we cannot send the '
262 266
                  'informations to him.'))
263 267

  
268
    def has_email(self):
269
        return bool(self.instance and self.instance.email)
270

  
264 271
    def save(self, commit=True):
265 272
        user = super(UserChangePasswordForm, self).save(commit=False)
266 273
        new_password = None
......
297 304
        label=_("Confirmation"),
298 305
        required=False)
299 306
    send_mail = forms.BooleanField(
300
        initial=True,
307
        initial=False,
301 308
        label=_('Send informations to user'),
302 309
        required=False)
303 310

  
......
309 316
class UserAddForm(UserChangePasswordForm, UserEditForm):
310 317
    css_class = "user-form"
311 318
    form_id = "id_user_add_form"
319
    require_password = False
312 320

  
313 321
    notification_template_prefix = \
314 322
        'authentic2/manager/new-account-notification'
......
328 336

  
329 337
    def clean(self):
330 338
        super(UserAddForm, self).clean()
331
        User = get_user_model()
332

  
333
        if not self.cleaned_data.get('username') and \
334
           not self.cleaned_data.get('email'):
339
        # check if this account is going to be real online account, i.e. with a
340
        # password, it it's the case complain that there is no identifiers.
341
        has_password = (
342
            self.cleaned_data.get('new_password1') or
343
            self.cleaned_data.get('generate_password') or
344
            self.cleaned_data.get('send_password_reset'))
345

  
346
        if (has_password and
347
                not self.cleaned_data.get('username') and
348
                not self.cleaned_data.get('email')):
335 349
            raise forms.ValidationError(
336
                _('You must set a username or an email.'))
350
                _('You must set a username or an email to set a password or send an activation link.'))
351

  
352
    def has_email(self):
353
        return bool(self.cleaned_data.get('email'))
337 354

  
338 355
    def save(self, commit=True):
339 356
        self.instance.ou = self.ou
tests/test_manager.py
150 150
        form.set('email', 'john.doe@gmail.com')
151 151
        form.set('password1', 'ABcd1234')
152 152
        form.set('password2', 'ABcd1234')
153
        form.set('send_mail', True)
153 154
        form.submit().follow()
154 155
        app.get('/logout/').form.submit()
155 156
    assert User.objects.filter(ou_id=new_ou.id).count() == 100
156
-