Ticket #12103: 12103_method_on_form.2.diff
File 12103_method_on_form.2.diff, 3.0 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/forms.py
79 79 self.user_cache = authenticate(username=username, password=password) 80 80 if self.user_cache is None: 81 81 raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive.")) 82 el if not self.user_cache.is_active:83 raise forms.ValidationError(_("This account is inactive."))84 82 else: 83 self.confirm_login_allowed(user_cache) 84 85 85 # TODO: determine whether this should move to its own method. 86 86 if self.request: 87 87 if not self.request.session.test_cookie_worked(): … … 89 89 90 90 return self.cleaned_data 91 91 92 def confirm_login_allowed(self, user): 93 """ 94 Controls whether the given ``auth.User`` object may log in. This is a policy setting, 95 independent of end-user authentication. This default behavior is to allow login by 96 active users, and reject login by inactive users. 97 98 If the given user cannot log in, this method should raise a ``forms.ValidationError``. 99 100 If the given user may log in, this method should return None. 101 """ 102 if not user.is_active: 103 raise forms.ValidationError(_("This account is inactive.")) 104 92 105 def get_user_id(self): 93 106 if self.user_cache: 94 107 return self.user_cache.id -
docs/topics/auth.txt
1000 1000 1001 1001 A form for logging a user in. 1002 1002 1003 The ``AuthenticationForm`` rejects users whose ``is_active`` flag is set to ``False``. 1004 You may override this behavior with a custom policy to determine which users can log in. 1005 Do this with a custom form that subclasses ``AuthenticationForm`` and overrides the 1006 ``confirm_login_allowed(self, user)`` method. This method will raise a ``forms.ValidationError`` 1007 if the given user may not log in. 1008 1009 For example, to allow all users to log in, regardless of activation status:: 1010 1011 .. code-block:: python 1012 1013 class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm): 1014 def confirm_login_allowed(self, user): 1015 pass 1016 1017 Or to allow only some active users to log in: 1018 1019 .. code-block:: python 1020 1021 class PickyAuthenticationForm(AuthenticationForm): 1022 def confirm_login_allowed(self, user): 1023 if not user.is_active: 1024 raise forms.ValidationError(_("This account is inactive.")) 1025 if user.username.startswith('b'): 1026 raise forms.ValidationError(_("Sorry, accounts starting with 'b' aren't welcome here.")) 1027 1003 1028 .. class:: PasswordChangeForm 1004 1029 1005 1030 A form for allowing a user to change their password.