root / snf-astakos-app / astakos / im / forms.py @ fffa19d2
History | View | Annotate | Download (20.9 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
from urlparse import urljoin |
34 |
from datetime import datetime |
35 |
|
36 |
from django import forms |
37 |
from django.utils.translation import ugettext as _ |
38 |
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, \ |
39 |
PasswordResetForm, PasswordChangeForm, SetPasswordForm |
40 |
from django.core.mail import send_mail |
41 |
from django.contrib.auth.tokens import default_token_generator |
42 |
from django.template import Context, loader |
43 |
from django.utils.http import int_to_base36 |
44 |
from django.core.urlresolvers import reverse |
45 |
from django.utils.functional import lazy |
46 |
from django.utils.safestring import mark_safe |
47 |
from django.contrib import messages |
48 |
from django.utils.encoding import smart_str |
49 |
from django.forms.models import fields_for_model |
50 |
|
51 |
from astakos.im.models import ( |
52 |
AstakosUser, Invitation, get_latest_terms, |
53 |
EmailChange, PendingThirdPartyUser |
54 |
) |
55 |
from astakos.im.settings import (INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, |
56 |
BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL, |
57 |
RECAPTCHA_ENABLED, LOGGING_LEVEL, PASSWORD_RESET_EMAIL_SUBJECT, |
58 |
NEWPASSWD_INVALIDATE_TOKEN |
59 |
) |
60 |
from astakos.im.widgets import DummyWidget, RecaptchaWidget |
61 |
from astakos.im.functions import send_change_email |
62 |
|
63 |
# since Django 1.4 use django.core.urlresolvers.reverse_lazy instead
|
64 |
from astakos.im.util import reverse_lazy, reserved_email, get_query |
65 |
|
66 |
import logging |
67 |
import hashlib |
68 |
import recaptcha.client.captcha as captcha |
69 |
from random import random |
70 |
|
71 |
logger = logging.getLogger(__name__) |
72 |
|
73 |
class LocalUserCreationForm(UserCreationForm): |
74 |
"""
|
75 |
Extends the built in UserCreationForm in several ways:
|
76 |
|
77 |
* Adds email, first_name, last_name, recaptcha_challenge_field, recaptcha_response_field field.
|
78 |
* The username field isn't visible and it is assigned a generated id.
|
79 |
* User created is not active.
|
80 |
"""
|
81 |
recaptcha_challenge_field = forms.CharField(widget=DummyWidget) |
82 |
recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='')
|
83 |
|
84 |
class Meta: |
85 |
model = AstakosUser |
86 |
fields = ("email", "first_name", "last_name", "has_signed_terms", "has_signed_terms") |
87 |
|
88 |
def __init__(self, *args, **kwargs): |
89 |
"""
|
90 |
Changes the order of fields, and removes the username field.
|
91 |
"""
|
92 |
request = kwargs.get('request', None) |
93 |
if request:
|
94 |
kwargs.pop('request')
|
95 |
self.ip = request.META.get('REMOTE_ADDR', |
96 |
request.META.get('HTTP_X_REAL_IP', None)) |
97 |
|
98 |
super(LocalUserCreationForm, self).__init__(*args, **kwargs) |
99 |
self.fields.keyOrder = ['email', 'first_name', 'last_name', |
100 |
'password1', 'password2'] |
101 |
|
102 |
if RECAPTCHA_ENABLED:
|
103 |
self.fields.keyOrder.extend(['recaptcha_challenge_field', |
104 |
'recaptcha_response_field',])
|
105 |
if get_latest_terms():
|
106 |
self.fields.keyOrder.append('has_signed_terms') |
107 |
|
108 |
if 'has_signed_terms' in self.fields: |
109 |
# Overriding field label since we need to apply a link
|
110 |
# to the terms within the label
|
111 |
terms_link_html = '<a href="%s" target="_blank">%s</a>' \
|
112 |
% (reverse('latest_terms'), _("the terms")) |
113 |
self.fields['has_signed_terms'].label = \ |
114 |
mark_safe("I agree with %s" % terms_link_html)
|
115 |
|
116 |
def clean_email(self): |
117 |
email = self.cleaned_data['email'] |
118 |
if not email: |
119 |
raise forms.ValidationError(_("This field is required")) |
120 |
if reserved_email(email):
|
121 |
raise forms.ValidationError(_("This email is already used")) |
122 |
return email
|
123 |
|
124 |
def clean_has_signed_terms(self): |
125 |
has_signed_terms = self.cleaned_data['has_signed_terms'] |
126 |
if not has_signed_terms: |
127 |
raise forms.ValidationError(_('You have to agree with the terms')) |
128 |
return has_signed_terms
|
129 |
|
130 |
def clean_recaptcha_response_field(self): |
131 |
if 'recaptcha_challenge_field' in self.cleaned_data: |
132 |
self.validate_captcha()
|
133 |
return self.cleaned_data['recaptcha_response_field'] |
134 |
|
135 |
def clean_recaptcha_challenge_field(self): |
136 |
if 'recaptcha_response_field' in self.cleaned_data: |
137 |
self.validate_captcha()
|
138 |
return self.cleaned_data['recaptcha_challenge_field'] |
139 |
|
140 |
def validate_captcha(self): |
141 |
rcf = self.cleaned_data['recaptcha_challenge_field'] |
142 |
rrf = self.cleaned_data['recaptcha_response_field'] |
143 |
check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip)
|
144 |
if not check.is_valid: |
145 |
raise forms.ValidationError(_('You have not entered the correct words')) |
146 |
|
147 |
def save(self, commit=True): |
148 |
"""
|
149 |
Saves the email, first_name and last_name properties, after the normal
|
150 |
save behavior is complete.
|
151 |
"""
|
152 |
user = super(LocalUserCreationForm, self).save(commit=False) |
153 |
user.renew_token() |
154 |
if commit:
|
155 |
user.save() |
156 |
logger._log(LOGGING_LEVEL, 'Created user %s' % user.email, [])
|
157 |
return user
|
158 |
|
159 |
class InvitedLocalUserCreationForm(LocalUserCreationForm): |
160 |
"""
|
161 |
Extends the LocalUserCreationForm: email is readonly.
|
162 |
"""
|
163 |
class Meta: |
164 |
model = AstakosUser |
165 |
fields = ("email", "first_name", "last_name", "has_signed_terms") |
166 |
|
167 |
def __init__(self, *args, **kwargs): |
168 |
"""
|
169 |
Changes the order of fields, and removes the username field.
|
170 |
"""
|
171 |
super(InvitedLocalUserCreationForm, self).__init__(*args, **kwargs) |
172 |
|
173 |
#set readonly form fields
|
174 |
ro = ('email', 'username',) |
175 |
for f in ro: |
176 |
self.fields[f].widget.attrs['readonly'] = True |
177 |
|
178 |
|
179 |
def save(self, commit=True): |
180 |
user = super(InvitedLocalUserCreationForm, self).save(commit=False) |
181 |
level = user.invitation.inviter.level + 1
|
182 |
user.level = level |
183 |
user.invitations = INVITATIONS_PER_LEVEL.get(level, 0)
|
184 |
user.email_verified = True
|
185 |
if commit:
|
186 |
user.save() |
187 |
return user
|
188 |
|
189 |
class ThirdPartyUserCreationForm(forms.ModelForm): |
190 |
third_party_identifier = forms.CharField( |
191 |
widget=forms.HiddenInput(), |
192 |
label=''
|
193 |
) |
194 |
class Meta: |
195 |
model = AstakosUser |
196 |
fields = ['email', 'third_party_identifier', 'first_name', 'last_name'] |
197 |
|
198 |
def __init__(self, *args, **kwargs): |
199 |
"""
|
200 |
Changes the order of fields, and removes the username field.
|
201 |
"""
|
202 |
self.request = kwargs.get('request', None) |
203 |
if self.request: |
204 |
kwargs.pop('request')
|
205 |
|
206 |
latest_terms = get_latest_terms() |
207 |
if latest_terms:
|
208 |
self._meta.fields.append('has_signed_terms') |
209 |
|
210 |
super(ThirdPartyUserCreationForm, self).__init__(*args, **kwargs) |
211 |
|
212 |
if latest_terms:
|
213 |
self.fields.keyOrder.append('has_signed_terms') |
214 |
|
215 |
if 'has_signed_terms' in self.fields: |
216 |
# Overriding field label since we need to apply a link
|
217 |
# to the terms within the label
|
218 |
terms_link_html = '<a href="%s" target="_blank">%s</a>' \
|
219 |
% (reverse('latest_terms'), _("the terms")) |
220 |
self.fields['has_signed_terms'].label = \ |
221 |
mark_safe("I agree with %s" % terms_link_html)
|
222 |
|
223 |
def clean_email(self): |
224 |
email = self.cleaned_data['email'] |
225 |
if not email: |
226 |
raise forms.ValidationError(_("This field is required")) |
227 |
return email
|
228 |
|
229 |
def clean_has_signed_terms(self): |
230 |
has_signed_terms = self.cleaned_data['has_signed_terms'] |
231 |
if not has_signed_terms: |
232 |
raise forms.ValidationError(_('You have to agree with the terms')) |
233 |
return has_signed_terms
|
234 |
|
235 |
def save(self, commit=True): |
236 |
user = super(ThirdPartyUserCreationForm, self).save(commit=False) |
237 |
user.set_unusable_password() |
238 |
user.renew_token() |
239 |
user.provider = get_query(self.request).get('provider') |
240 |
if commit:
|
241 |
user.save() |
242 |
logger._log(LOGGING_LEVEL, 'Created user %s' % user.email, [])
|
243 |
return user
|
244 |
|
245 |
class InvitedThirdPartyUserCreationForm(ThirdPartyUserCreationForm): |
246 |
"""
|
247 |
Extends the ThirdPartyUserCreationForm: email is readonly.
|
248 |
"""
|
249 |
def __init__(self, *args, **kwargs): |
250 |
"""
|
251 |
Changes the order of fields, and removes the username field.
|
252 |
"""
|
253 |
super(InvitedThirdPartyUserCreationForm, self).__init__(*args, **kwargs) |
254 |
|
255 |
#set readonly form fields
|
256 |
ro = ('email',)
|
257 |
for f in ro: |
258 |
self.fields[f].widget.attrs['readonly'] = True |
259 |
|
260 |
def save(self, commit=True): |
261 |
user = super(InvitedThirdPartyUserCreationForm, self).save(commit=False) |
262 |
level = user.invitation.inviter.level + 1
|
263 |
user.level = level |
264 |
user.invitations = INVITATIONS_PER_LEVEL.get(level, 0)
|
265 |
user.email_verified = True
|
266 |
if commit:
|
267 |
user.save() |
268 |
return user
|
269 |
|
270 |
class ShibbolethUserCreationForm(ThirdPartyUserCreationForm): |
271 |
additional_email = forms.CharField(widget=forms.HiddenInput(), label='', required = False) |
272 |
|
273 |
def __init__(self, *args, **kwargs): |
274 |
super(ShibbolethUserCreationForm, self).__init__(*args, **kwargs) |
275 |
# copy email value to additional_mail in case user will change it
|
276 |
name = 'email'
|
277 |
field = self.fields[name]
|
278 |
self.initial['additional_email'] = self.initial.get(name, field.initial) |
279 |
self.initial['email'] = None |
280 |
|
281 |
def clean_email(self): |
282 |
email = self.cleaned_data['email'] |
283 |
for user in AstakosUser.objects.filter(email = email): |
284 |
if user.provider == 'shibboleth': |
285 |
raise forms.ValidationError(_(
|
286 |
"This email is already associated with another shibboleth \
|
287 |
account."
|
288 |
) |
289 |
) |
290 |
else:
|
291 |
raise forms.ValidationError(_("This email is already used")) |
292 |
super(ShibbolethUserCreationForm, self).clean_email() |
293 |
return email
|
294 |
|
295 |
def save(self, commit=True): |
296 |
user = super(ShibbolethUserCreationForm, self).save(commit=False) |
297 |
try:
|
298 |
p = PendingThirdPartyUser.objects.get( |
299 |
provider=user.provider, |
300 |
third_party_identifier=user.third_party_identifier |
301 |
) |
302 |
except:
|
303 |
pass
|
304 |
else:
|
305 |
p.delete() |
306 |
return user
|
307 |
|
308 |
class InvitedShibbolethUserCreationForm(ShibbolethUserCreationForm, InvitedThirdPartyUserCreationForm): |
309 |
pass
|
310 |
|
311 |
class LoginForm(AuthenticationForm): |
312 |
username = forms.EmailField(label=_("Email"))
|
313 |
recaptcha_challenge_field = forms.CharField(widget=DummyWidget) |
314 |
recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='')
|
315 |
|
316 |
def __init__(self, *args, **kwargs): |
317 |
was_limited = kwargs.get('was_limited', False) |
318 |
request = kwargs.get('request', None) |
319 |
if request:
|
320 |
self.ip = request.META.get('REMOTE_ADDR', |
321 |
request.META.get('HTTP_X_REAL_IP', None)) |
322 |
|
323 |
t = ('request', 'was_limited') |
324 |
for elem in t: |
325 |
if elem in kwargs.keys(): |
326 |
kwargs.pop(elem) |
327 |
super(LoginForm, self).__init__(*args, **kwargs) |
328 |
|
329 |
self.fields.keyOrder = ['username', 'password'] |
330 |
if was_limited and RECAPTCHA_ENABLED: |
331 |
self.fields.keyOrder.extend(['recaptcha_challenge_field', |
332 |
'recaptcha_response_field',])
|
333 |
|
334 |
def clean_recaptcha_response_field(self): |
335 |
if 'recaptcha_challenge_field' in self.cleaned_data: |
336 |
self.validate_captcha()
|
337 |
return self.cleaned_data['recaptcha_response_field'] |
338 |
|
339 |
def clean_recaptcha_challenge_field(self): |
340 |
if 'recaptcha_response_field' in self.cleaned_data: |
341 |
self.validate_captcha()
|
342 |
return self.cleaned_data['recaptcha_challenge_field'] |
343 |
|
344 |
def validate_captcha(self): |
345 |
rcf = self.cleaned_data['recaptcha_challenge_field'] |
346 |
rrf = self.cleaned_data['recaptcha_response_field'] |
347 |
check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip)
|
348 |
if not check.is_valid: |
349 |
raise forms.ValidationError(_('You have not entered the correct words')) |
350 |
|
351 |
def clean(self): |
352 |
"""
|
353 |
Override default behavior in order to check user's activation later
|
354 |
"""
|
355 |
try:
|
356 |
super(LoginForm, self).clean() |
357 |
except forms.ValidationError, e:
|
358 |
if self.user_cache is None: |
359 |
raise
|
360 |
if self.request: |
361 |
if not self.request.session.test_cookie_worked(): |
362 |
raise
|
363 |
return self.cleaned_data |
364 |
|
365 |
class ProfileForm(forms.ModelForm): |
366 |
"""
|
367 |
Subclass of ``ModelForm`` for permiting user to edit his/her profile.
|
368 |
Most of the fields are readonly since the user is not allowed to change them.
|
369 |
|
370 |
The class defines a save method which sets ``is_verified`` to True so as the user
|
371 |
during the next login will not to be redirected to profile page.
|
372 |
"""
|
373 |
renew = forms.BooleanField(label='Renew token', required=False) |
374 |
|
375 |
class Meta: |
376 |
model = AstakosUser |
377 |
fields = ('email', 'first_name', 'last_name', 'auth_token', 'auth_token_expires') |
378 |
|
379 |
def __init__(self, *args, **kwargs): |
380 |
super(ProfileForm, self).__init__(*args, **kwargs) |
381 |
instance = getattr(self, 'instance', None) |
382 |
ro_fields = ('email', 'auth_token', 'auth_token_expires') |
383 |
if instance and instance.id: |
384 |
for field in ro_fields: |
385 |
self.fields[field].widget.attrs['readonly'] = True |
386 |
|
387 |
def save(self, commit=True): |
388 |
user = super(ProfileForm, self).save(commit=False) |
389 |
user.is_verified = True
|
390 |
if self.cleaned_data.get('renew'): |
391 |
user.renew_token() |
392 |
if commit:
|
393 |
user.save() |
394 |
return user
|
395 |
|
396 |
class FeedbackForm(forms.Form): |
397 |
"""
|
398 |
Form for writing feedback.
|
399 |
"""
|
400 |
feedback_msg = forms.CharField(widget=forms.Textarea, label=u'Message')
|
401 |
feedback_data = forms.CharField(widget=forms.HiddenInput(), label='',
|
402 |
required=False)
|
403 |
|
404 |
class SendInvitationForm(forms.Form): |
405 |
"""
|
406 |
Form for sending an invitations
|
407 |
"""
|
408 |
|
409 |
email = forms.EmailField(required = True, label = 'Email address') |
410 |
first_name = forms.EmailField(label = 'First name')
|
411 |
last_name = forms.EmailField(label = 'Last name')
|
412 |
|
413 |
class ExtendedPasswordResetForm(PasswordResetForm): |
414 |
"""
|
415 |
Extends PasswordResetForm by overriding save method:
|
416 |
passes a custom from_email in send_mail.
|
417 |
|
418 |
Since Django 1.3 this is useless since ``django.contrib.auth.views.reset_password``
|
419 |
accepts a from_email argument.
|
420 |
"""
|
421 |
def clean_email(self): |
422 |
email = super(ExtendedPasswordResetForm, self).clean_email() |
423 |
try:
|
424 |
user = AstakosUser.objects.get(email=email, is_active=True)
|
425 |
if not user.has_usable_password(): |
426 |
raise forms.ValidationError(_("This account has not a usable password.")) |
427 |
except AstakosUser.DoesNotExist, e:
|
428 |
raise forms.ValidationError(_('That e-mail address doesn\'t have an associated user account. Are you sure you\'ve registered?')) |
429 |
return email
|
430 |
|
431 |
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', |
432 |
use_https=False, token_generator=default_token_generator, request=None): |
433 |
"""
|
434 |
Generates a one-use only link for resetting password and sends to the user.
|
435 |
"""
|
436 |
for user in self.users_cache: |
437 |
url = reverse('django.contrib.auth.views.password_reset_confirm',
|
438 |
kwargs={'uidb36':int_to_base36(user.id),
|
439 |
'token':token_generator.make_token(user)})
|
440 |
url = urljoin(BASEURL, url) |
441 |
t = loader.get_template(email_template_name) |
442 |
c = { |
443 |
'email': user.email,
|
444 |
'url': url,
|
445 |
'site_name': SITENAME,
|
446 |
'user': user,
|
447 |
'baseurl': BASEURL,
|
448 |
'support': DEFAULT_CONTACT_EMAIL
|
449 |
} |
450 |
from_email = DEFAULT_FROM_EMAIL |
451 |
send_mail(_(PASSWORD_RESET_EMAIL_SUBJECT), |
452 |
t.render(Context(c)), from_email, [user.email]) |
453 |
|
454 |
class EmailChangeForm(forms.ModelForm): |
455 |
class Meta: |
456 |
model = EmailChange |
457 |
fields = ('new_email_address',)
|
458 |
|
459 |
def clean_new_email_address(self): |
460 |
addr = self.cleaned_data['new_email_address'] |
461 |
if AstakosUser.objects.filter(email__iexact=addr):
|
462 |
raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.')) |
463 |
return addr
|
464 |
|
465 |
def save(self, email_template_name, request, commit=True): |
466 |
ec = super(EmailChangeForm, self).save(commit=False) |
467 |
ec.user = request.user |
468 |
activation_key = hashlib.sha1(str(random()) + smart_str(ec.new_email_address))
|
469 |
ec.activation_key=activation_key.hexdigest() |
470 |
if commit:
|
471 |
ec.save() |
472 |
send_change_email(ec, request, email_template_name=email_template_name) |
473 |
|
474 |
class SignApprovalTermsForm(forms.ModelForm): |
475 |
class Meta: |
476 |
model = AstakosUser |
477 |
fields = ("has_signed_terms",)
|
478 |
|
479 |
def __init__(self, *args, **kwargs): |
480 |
super(SignApprovalTermsForm, self).__init__(*args, **kwargs) |
481 |
|
482 |
def clean_has_signed_terms(self): |
483 |
has_signed_terms = self.cleaned_data['has_signed_terms'] |
484 |
if not has_signed_terms: |
485 |
raise forms.ValidationError(_('You have to agree with the terms')) |
486 |
return has_signed_terms
|
487 |
|
488 |
class InvitationForm(forms.ModelForm): |
489 |
username = forms.EmailField(label=_("Email"))
|
490 |
|
491 |
def __init__(self, *args, **kwargs): |
492 |
super(InvitationForm, self).__init__(*args, **kwargs) |
493 |
|
494 |
class Meta: |
495 |
model = Invitation |
496 |
fields = ('username', 'realname') |
497 |
|
498 |
def clean_username(self): |
499 |
username = self.cleaned_data['username'] |
500 |
try:
|
501 |
Invitation.objects.get(username = username) |
502 |
raise forms.ValidationError(_('There is already invitation for this email.')) |
503 |
except Invitation.DoesNotExist:
|
504 |
pass
|
505 |
return username
|
506 |
|
507 |
class ExtendedPasswordChangeForm(PasswordChangeForm): |
508 |
"""
|
509 |
Extends PasswordChangeForm by enabling user
|
510 |
to optionally renew also the token.
|
511 |
"""
|
512 |
if not NEWPASSWD_INVALIDATE_TOKEN: |
513 |
renew = forms.BooleanField(label='Renew token', required=False, |
514 |
initial=True,
|
515 |
help_text='Unsetting this may result in security risk.')
|
516 |
|
517 |
def __init__(self, user, *args, **kwargs): |
518 |
super(ExtendedPasswordChangeForm, self).__init__(user, *args, **kwargs) |
519 |
|
520 |
def save(self, commit=True): |
521 |
if NEWPASSWD_INVALIDATE_TOKEN or self.cleaned_data.get('renew'): |
522 |
self.user.renew_token()
|
523 |
return super(ExtendedPasswordChangeForm, self).save(commit=commit) |
524 |
|
525 |
class ExtendedSetPasswordForm(SetPasswordForm): |
526 |
"""
|
527 |
Extends SetPasswordForm by enabling user
|
528 |
to optionally renew also the token.
|
529 |
"""
|
530 |
if not NEWPASSWD_INVALIDATE_TOKEN: |
531 |
renew = forms.BooleanField(label='Renew token', required=False, |
532 |
initial=True,
|
533 |
help_text='Unsetting this may result in security risk.')
|
534 |
|
535 |
def __init__(self, user, *args, **kwargs): |
536 |
super(ExtendedSetPasswordForm, self).__init__(user, *args, **kwargs) |
537 |
|
538 |
def save(self, commit=True): |
539 |
if NEWPASSWD_INVALIDATE_TOKEN or self.cleaned_data.get('renew'): |
540 |
if isinstance(self.user, AstakosUser): |
541 |
self.user.renew_token()
|
542 |
return super(ExtendedSetPasswordForm, self).save(commit=commit) |