本文整理汇总了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)