Ticket #12103: 12103_with_working_tests.diff
File 12103_with_working_tests.diff, 3.8 KB (added by , 14 years ago) |
---|
-
docs/topics/auth.txt
1046 1046 1047 1047 A form for logging a user in. 1048 1048 1049 The ``AuthenticationForm`` rejects users whose ``is_active`` flag is set to ``False``. 1050 You may override this behavior with a custom policy to determine which users can log in. 1051 Do this with a custom form that subclasses ``AuthenticationForm`` and overrides the 1052 ``confirm_login_allowed(self, user)`` method. This method will raise a ``forms.ValidationError`` 1053 if the given user may not log in. 1054 1055 For example, to allow all users to log in, regardless of activation status:: 1056 1057 .. code-block:: python 1058 1059 class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm): 1060 def confirm_login_allowed(self, user): 1061 pass 1062 1063 Or to allow only some active users to log in: 1064 1065 .. code-block:: python 1066 1067 class PickyAuthenticationForm(AuthenticationForm): 1068 def confirm_login_allowed(self, user): 1069 if not user.is_active: 1070 raise forms.ValidationError(_("This account is inactive.")) 1071 if user.username.startswith('b'): 1072 raise forms.ValidationError(_("Sorry, accounts starting with 'b' aren't welcome here.")) 1073 1049 1074 .. class:: PasswordChangeForm 1050 1075 1051 1076 A form for allowing a user to change their password. -
django/contrib/auth/tests/forms.py
102 102 [u'This account is inactive.']) 103 103 104 104 105 def test_custom_login_allowed_policy(self): 106 # The user is inactive, but allowed to login. 107 data = { 108 'username': 'inactive', 109 'password': 'password', 110 } 111 112 class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm): 113 def confirm_login_allowed(self, user): 114 pass 115 116 form = AuthenticationFormWithInactiveUsersOkay(None, data) 117 self.assertTrue(form.is_valid()) 118 105 119 def test_success(self): 106 120 # The success case 107 121 data = { -
django/contrib/auth/forms.py
85 85 self.user_cache = authenticate(username=username, password=password) 86 86 if self.user_cache is None: 87 87 raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive.")) 88 el if not self.user_cache.is_active:89 raise forms.ValidationError(_("This account is inactive."))88 else: 89 self.confirm_login_allowed(self.user_cache) 90 90 self.check_for_test_cookie() 91 91 return self.cleaned_data 92 92 … … 96 96 _("Your Web browser doesn't appear to have cookies enabled. " 97 97 "Cookies are required for logging in.")) 98 98 99 def confirm_login_allowed(self, user): 100 """ 101 Controls whether the given ``auth.User`` object may log in. This is a policy setting, 102 independent of end-user authentication. This default behavior is to allow login by 103 active users, and reject login by inactive users. 104 105 If the given user cannot log in, this method should raise a ``forms.ValidationError``. 106 107 If the given user may log in, this method should return None. 108 """ 109 if not user.is_active: 110 raise forms.ValidationError(_("This account is inactive.")) 111 99 112 def get_user_id(self): 100 113 if self.user_cache: 101 114 return self.user_cache.id