我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.auth.get_backends()。
def login_as(request, user_id): """ ????? ???... """ if not request.user.is_superuser: logout(request) return redirect(settings.LOGIN_URL) UserModel = get_user_model() try: user = UserModel.objects.get(pk=user_id) except ObjectDoesNotExist: logout(request) return redirect(settings.LOGIN_URL) else: for backend in get_backends(): if user == backend.get_user(user.pk): user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) break login(request, user) return redirect(LOGIN_AS_REDIRECT_URL)
def login(self, request, user): # HACK: This is not nice. The proper Django way is to use an # authentication backend if not hasattr(user, 'backend'): from .auth_backends import AuthenticationBackend backends = get_backends() for backend in backends: if isinstance(backend, AuthenticationBackend): # prefer our own backend break else: # Pick one backend = backends[0] backend_path = '.'.join([backend.__module__, backend.__class__.__name__]) user.backend = backend_path django_login(request, user)
def save(self, commit=True): can_register = False for b in get_backends(): if getattr(b, 'can_register', None): can_register = True instance = super(UserProfileForm, self).save(commit=commit) user = instance.user if can_register: user.username = self.cleaned_data['username'] user.email = self.cleaned_data['email'] user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() return instance
def _user_get_all_permissions(user, obj): permissions = set() for backend in auth.get_backends(): if hasattr(backend, "get_all_permissions"): permissions.update(backend.get_all_permissions(user, obj)) return permissions
def _user_has_perm(user, perm, obj): """ A backend can raise `PermissionDenied` to short-circuit permission checking. """ for backend in auth.get_backends(): if not hasattr(backend, 'has_perm'): continue try: if backend.has_perm(user, perm, obj): return True except PermissionDenied: return False return False
def _user_has_module_perms(user, app_label): """ A backend can raise `PermissionDenied` to short-circuit permission checking. """ for backend in auth.get_backends(): if not hasattr(backend, 'has_module_perms'): continue try: if backend.has_module_perms(user, app_label): return True except PermissionDenied: return False return False
def get_group_permissions(self, obj=None): """ Returns a list of permission strings that this user has through their groups. This method queries all available auth backends. If an object is passed in, only permissions matching this object are returned. """ permissions = set() for backend in auth.get_backends(): if hasattr(backend, "get_group_permissions"): permissions.update(backend.get_group_permissions(self, obj)) return permissions
def _authenticate(self, username=None, password=None, **kwargs): self.fail_unless_one_aaa_backend_is_configured() assert password is not None if not self.is_act_as_username(username): return None auth_username, act_as_username = username.split(self.sepchar) backends = [b for b in auth.get_backends() if not isinstance(b, ActAsBackend)] for backend in backends: auth_user = backend.authenticate( username=auth_username, password=password, **kwargs) if auth_user: return self.get_act_as_user( auth_user=auth_user, act_as_username=act_as_username)
def fail_unless_one_aaa_backend_is_configured(self): aaa_backends = list( type(backend) for backend in auth.get_backends() if isinstance(backend, ActAsBackend)) if len(aaa_backends) != 1: raise ValueError( 'There should be exactly one AAA backend configured, ' 'but there were {}'.format(aaa_backends))