Python django.contrib.auth.backends.ModelBackend() Examples
The following are 5
code examples of django.contrib.auth.backends.ModelBackend().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.contrib.auth.backends
, or try the search function
.
Example #1
Source File: auth_backends.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def authenticate(self, request, username=None, password=None, **kwargs): """ This is mostly copied from the default ModelBackend. Attempts to fetch users by username or email address, instead of just by username. """ if not username or not password: return None UserModel = get_user_model() # noqa pylint: disable=invalid-name try: user = UserModel._default_manager.get(Q(username=username) | Q(email=username)) except (UserModel.DoesNotExist, UserModel.MultipleObjectsReturned): # Run the default password hasher once to reduce the timing # difference between an existing and a non-existing user (#20760). UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate(user): return user return None
Example #2
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_context_processor_dependencies(self): expected = [ checks.Error( "'django.contrib.auth.context_processors.auth' must be " "enabled in DjangoTemplates (TEMPLATES) if using the default " "auth backend in order to use the admin application.", id='admin.E402', ), checks.Error( "'django.contrib.messages.context_processors.messages' must " "be enabled in DjangoTemplates (TEMPLATES) in order to use " "the admin application.", id='admin.E404', ) ] self.assertEqual(admin.checks.check_dependencies(), expected) # The first error doesn't happen if # 'django.contrib.auth.backends.ModelBackend' isn't in # AUTHENTICATION_BACKENDS. with self.settings(AUTHENTICATION_BACKENDS=[]): self.assertEqual(admin.checks.check_dependencies(), expected[1:])
Example #3
Source File: auth_backends.py From connect with MIT License | 6 votes |
def get_all_permissions(self, user_obj, obj=None): """Return all permissions for a user""" # Anonymous users should have no permissions by default if user_obj.is_anonymous() or obj is not None: return set() # This should still work even if django removes `user._perm_cache` from # future releases of the auth `ModelBackend` if not hasattr(user_obj, '_perm_cache'): key = '{userkey}_permissions'.format(userkey=user_obj.cache_key) cache_result = cache.get(key) if cache_result is None: user_obj._perm_cache = super( CachedModelAuthBackend, self).get_all_permissions( user_obj, obj) # Cache permissions for 15 minutes. As adding a user to a group # will result in a change of the modified_at column and thus # the `cache_key` we don't have to hugely worry about changes cache.set(key, user_obj._perm_cache, 60*30) else: user_obj._perm_cache = cache_result return user_obj._perm_cache
Example #4
Source File: backends.py From Django-blog with MIT License | 5 votes |
def user_can_authenticate(self, user): """ 重载了这个方法, 达到django.contrib.auth.backends.AllowAllUsersModelBackend一样的效果 跟 AllowAllUsersModelBackend 一样的效果, 但 is_active为False为False, 会报未激活 ModelBackend原先的user_can_authenticate方法, 但is_active为False, 会报账号不存在 即 请输入一个正确的 用户名 和密码. 注意他们都是大区分大小写的. """ return True
Example #5
Source File: utils.py From django-oidc-provider with MIT License | 5 votes |
def authenticate(self, *args, **kwargs): if django.VERSION[0] >= 2 or (django.VERSION[0] == 1 and django.VERSION[1] >= 11): assert len(args) > 0 and args[0] return ModelBackend().authenticate(*args, **kwargs)