Fix LoginForm clean incase of login failure
[astakos] / snf-astakos-app / astakos / im / forms.py
index a8fce76..2e136ae 100644 (file)
@@ -35,7 +35,8 @@ from datetime import datetime
 
 from django import forms
 from django.utils.translation import ugettext as _
-from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordResetForm
+from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, \
+    PasswordResetForm, PasswordChangeForm
 from django.core.mail import send_mail
 from django.contrib.auth.tokens import default_token_generator
 from django.template import Context, loader
@@ -318,6 +319,12 @@ class LoginForm(AuthenticationForm):
         check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip)
         if not check.is_valid:
             raise forms.ValidationError(_('You have not entered the correct words'))
+    
+    def clean(self):
+        super(LoginForm, self).clean()
+        if self.user_cache and self.user_cache.provider not in ('local', ''):
+            raise forms.ValidationError(_('Local login is not the current authentication method for this account.'))
+        return self.cleaned_data
 
 class ProfileForm(forms.ModelForm):
     """
@@ -460,3 +467,21 @@ class InvitationForm(forms.ModelForm):
         except Invitation.DoesNotExist:
             pass
         return username
+
+class ExtendedPasswordChangeForm(PasswordChangeForm):
+    """
+    Extends PasswordChangeForm by enabling user
+    to optionally renew also the token.
+    """
+    renew = forms.BooleanField(label='Renew token', required=False)
+    
+    def __init__(self, user, *args, **kwargs):
+        super(ExtendedPasswordChangeForm, self).__init__(user, *args, **kwargs)
+    
+    def save(self, commit=True):
+        user = super(ExtendedPasswordChangeForm, self).save(commit=False)
+        if self.cleaned_data.get('renew'):
+            user.renew_token()
+        if commit:
+            user.save()
+        return user
\ No newline at end of file