本文整理匯總了Python中django.contrib.auth.backends.ModelBackend方法的典型用法代碼示例。如果您正苦於以下問題:Python backends.ModelBackend方法的具體用法?Python backends.ModelBackend怎麽用?Python backends.ModelBackend使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth.backends
的用法示例。
在下文中一共展示了backends.ModelBackend方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: authenticate
# 需要導入模塊: from django.contrib.auth import backends [as 別名]
# 或者: from django.contrib.auth.backends import ModelBackend [as 別名]
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
示例2: test_context_processor_dependencies
# 需要導入模塊: from django.contrib.auth import backends [as 別名]
# 或者: from django.contrib.auth.backends import ModelBackend [as 別名]
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:])
示例3: get_all_permissions
# 需要導入模塊: from django.contrib.auth import backends [as 別名]
# 或者: from django.contrib.auth.backends import ModelBackend [as 別名]
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
示例4: user_can_authenticate
# 需要導入模塊: from django.contrib.auth import backends [as 別名]
# 或者: from django.contrib.auth.backends import ModelBackend [as 別名]
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
示例5: authenticate
# 需要導入模塊: from django.contrib.auth import backends [as 別名]
# 或者: from django.contrib.auth.backends import ModelBackend [as 別名]
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)