Log in a user without requiring credentials

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.conf import settings


def login_user(request, user):
    """
    Log in a user without requiring credentials (using ``login`` from
    ``django.contrib.auth``, first finding a matching backend).

    """
    from django.contrib.auth import load_backend, login
    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'):
        return login(request, user)

More like this

  1. view by view basic authentication decorator by Scanner 5 years ago
  2. LoginRequiredMixin by slink 1 year ago
  3. Anonymous required decorator by Motanelu 2 years, 4 months ago
  4. Auth decorators with 403 by Magus 5 years ago
  5. apache authentication via cookies by sean 5 years, 2 months ago

Comments

(Forgotten your password?)