本文整理汇总了Python中django.contrib.auth.load_backend方法的典型用法代码示例。如果您正苦于以下问题:Python auth.load_backend方法的具体用法?Python auth.load_backend怎么用?Python auth.load_backend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth
的用法示例。
在下文中一共展示了auth.load_backend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: with_perm
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def with_perm(self, perm, is_active=True, include_superusers=True, backend=None, obj=None):
if backend is None:
backends = auth._get_backends(return_tuples=True)
if len(backends) == 1:
backend, _ = backends[0]
else:
raise ValueError(
'You have multiple authentication backends configured and '
'therefore must provide the `backend` argument.'
)
elif not isinstance(backend, str):
raise TypeError(
'backend must be a dotted import path string (got %r).'
% backend
)
else:
backend = auth.load_backend(backend)
if hasattr(backend, 'with_perm'):
return backend.with_perm(
perm,
is_active=is_active,
include_superusers=include_superusers,
obj=obj,
)
return self.none()
示例2: clean_username
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def clean_username(self, username, request):
"""
Allows the backend to clean the username, if the backend defines a
clean_username method.
"""
backend_str = request.session[auth.BACKEND_SESSION_KEY]
backend = auth.load_backend(backend_str)
try:
LOGGER.debug(
_('calling the backend {0} clean_username with {1}'),
backend,
username)
username = backend.clean_username(username)
LOGGER.debug(_('cleaned username is {0}'), username)
except AttributeError: # Backend has no clean_username method.
pass
return username
示例3: clean_username
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def clean_username(self, username, request):
"""
Allows the backend to clean the username, if the backend defines a
clean_username method.
"""
backend_str = request.session[auth.BACKEND_SESSION_KEY]
backend = auth.load_backend(backend_str)
try:
LOGGER.debug('calling the backend %s clean_username with %s',
backend,
username)
username = backend.clean_username(username)
LOGGER.debug('cleaned username is %s', username)
except AttributeError: # Backend has no clean_username method.
pass
return username
示例4: getUserFromSessionId
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def getUserFromSessionId(self, session_id):
"""Return the user from `session_id`."""
session_engine = self.factory.getSessionEngine()
session_wrapper = session_engine.SessionStore(session_id)
user_id = session_wrapper.get(SESSION_KEY)
backend = session_wrapper.get(BACKEND_SESSION_KEY)
if backend is None:
return None
auth_backend = load_backend(backend)
if user_id is not None and auth_backend is not None:
user = auth_backend.get_user(user_id)
# Get the user again prefetching the SSHKey for the user. This is
# done so a query is not made for each action that is possible on
# a node in the node listing.
return (
User.objects.filter(id=user.id)
.prefetch_related("sshkey_set")
.first()
)
else:
return None
示例5: verifica_token
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def verifica_token(request, me, token):
verifica = Token.verifica(token, redirect=True)
if not verifica:
return errore_generico(request, me, titolo="Token scaduto",
messaggio="Potresti aver seguito un link che è scaduto.")
persona, url = verifica
user = Utenza.objects.get(persona=persona)
if not hasattr(user, 'backend'):
for backend in settings.AUTHENTICATION_BACKENDS:
if user == load_backend(backend).get_user(user.pk):
user.backend = backend
break
if hasattr(user, 'backend'):
login(request, user)
return redirect(url)
示例6: clean_username
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def clean_username(self, username, request):
"""
Allows the backend to clean the username, if the backend defines a
clean_username method.
"""
backend_str = request.session[auth.BACKEND_SESSION_KEY]
backend = auth.load_backend(backend_str)
try:
username = backend.clean_username(username)
except AttributeError: # Backend has no clean_username method.
pass
return username
示例7: _remove_invalid_user
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def _remove_invalid_user(self, request):
"""
Removes the current authenticated user in the request which is invalid
but only if the user is authenticated via the RemoteUserBackend.
"""
try:
stored_backend = load_backend(request.session.get(auth.BACKEND_SESSION_KEY, ''))
except ImportError:
# backend failed to load
auth.logout(request)
else:
if isinstance(stored_backend, RemoteUserBackend):
auth.logout(request)
示例8: force_login
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def force_login(self, user, backend=None):
def get_backend():
from django.contrib.auth import load_backend
for backend_path in settings.AUTHENTICATION_BACKENDS:
backend = load_backend(backend_path)
if hasattr(backend, 'get_user'):
return backend_path
if backend is None:
backend = get_backend()
user.backend = backend
self._login(user, backend)
示例9: clean_username
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def clean_username(self, username, request):
"""
Allow the backend to clean the username, if the backend defines a
clean_username method.
"""
backend_str = request.session[auth.BACKEND_SESSION_KEY]
backend = auth.load_backend(backend_str)
try:
username = backend.clean_username(username)
except AttributeError: # Backend has no clean_username method.
pass
return username
示例10: _remove_invalid_user
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def _remove_invalid_user(self, request):
"""
Remove the current authenticated user in the request which is invalid
but only if the user is authenticated via the RemoteUserBackend.
"""
try:
stored_backend = load_backend(request.session.get(auth.BACKEND_SESSION_KEY, ''))
except ImportError:
# backend failed to load
auth.logout(request)
else:
if isinstance(stored_backend, RemoteUserBackend):
auth.logout(request)
示例11: with_perm
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def with_perm(
self, perm, is_active=True, include_superusers=True, backend=None, obj=None
):
if backend is None:
backends = auth._get_backends(return_tuples=True)
if len(backends) == 1:
backend, _ = backends[0]
else:
raise ValueError(
"You have multiple authentication backends configured and "
"therefore must provide the `backend` argument."
)
elif not isinstance(backend, str):
raise TypeError(
"backend must be a dotted import path string (got %r)." % backend
)
else:
backend = auth.load_backend(backend)
if hasattr(backend, "with_perm"):
return backend.with_perm(
perm,
is_active=is_active,
include_superusers=include_superusers,
obj=obj,
)
return self.none()
示例12: get_user
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def get_user(self):
try:
user_id = self.request.session['u2f_pre_verify_user_pk']
backend_path = self.request.session['u2f_pre_verify_user_backend']
assert backend_path in settings.AUTHENTICATION_BACKENDS
backend = load_backend(backend_path)
user = backend.get_user(user_id)
if user is not None:
user.backend = backend_path
return user
except (KeyError, AssertionError):
return None
示例13: get_user
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def get_user(scope):
"""
Return the user model instance associated with the given scope.
If no user is retrieved, return an instance of `AnonymousUser`.
"""
if "session" not in scope:
raise ValueError("Cannot find session in scope. You should wrap your consumer in SessionMiddleware.")
user = None
session = scope["session"]
with scope["tenant"]:
try:
user_id = _get_user_session_key(session)
backend_path = session[BACKEND_SESSION_KEY]
except KeyError:
pass
else:
if backend_path in settings.AUTHENTICATION_BACKENDS:
backend = load_backend(backend_path)
user = backend.get_user(user_id)
# Verify the session
if hasattr(user, "get_session_auth_hash"):
session_hash = session.get(HASH_SESSION_KEY)
session_hash_verified = session_hash and constant_time_compare(
session_hash, user.get_session_auth_hash()
)
if not session_hash_verified:
session.flush()
user = None
return user or AnonymousUser()
示例14: get_user
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def get_user(self):
try:
user_id = self.request.session['u2f_pre_verify_user_pk']
backend_path = self.request.session['u2f_pre_verify_user_backend']
self.request.session['verfied_u2f'] = False
assert backend_path in settings.AUTHENTICATION_BACKENDS
backend = load_backend(backend_path)
user = backend.get_user(user_id)
if user is not None:
user.backend = backend_path
return user
except (KeyError, AssertionError):
return None
示例15: process_request
# 需要导入模块: from django.contrib import auth [as 别名]
# 或者: from django.contrib.auth import load_backend [as 别名]
def process_request(self, request):
# Overwrite process_request from auth.middleware because it force
# user logout when REMOTE_USER header is not present which can
# cause problem while deploying with Kerberos authentication when
# we need to enable both anonymous access and kerberos login.
# AuthenticationMiddleware is required so that request.user exists.
if not hasattr(request, 'user'):
raise ImproperlyConfigured(
"The Django remote user auth middleware requires the"
" authentication middleware to be installed. Edit your"
" MIDDLEWARE setting to insert"
" 'django.contrib.auth.middleware.AuthenticationMiddleware'"
" before the RemoteUserMiddleware class.")
if settings.DEBUG and getattr(settings, "DEBUG_USER", None):
request.META[self.header] = settings.DEBUG_USER
try:
username = request.META[self.header]
except KeyError:
# When the page which requires kerberos login was redirected from
# kerberos login entrance, 'REMOTE_USER' header is lost in request
# meta, thus the RemoteUserMiddleware will make it falling into
# redirect loop.
return
# If the user is already authenticated and that user is the user we are
# getting passed in the headers, then the correct user is already
# persisted in the session and we don't need to continue.
if request.user.is_authenticated:
if request.user.get_username() == self.clean_username(username, request):
return
# We are seeing this user for the first time in this session, attempt
# to authenticate the user.
user = auth.authenticate(remote_user=username, request=request)
if user:
# User is valid. Set request.user and persist user in the session
# by logging the user in.
request.user = user
request.session['auth_backend'] = user.backend
backend = load_backend(user.backend)
if getattr(backend, 'save_login', True):
auth.login(request, user)