loginas/utils.py
def login_as(user, request, store_original_user=True):
    """
    Utility function for forcing a login as specific user -- be careful about
    calling this carelessly :)
    """

    # Save the original user pk before it is replaced in the login method
    original_user_pk = request.user.pk

    # Find a suitable backend.
    if not hasattr(user, "backend"):
        for backend in django_settings.AUTHENTICATION_BACKENDS:
            if not hasattr(load_backend(backend), "get_user"):
                continue

            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
        else:
            raise ImproperlyConfigured("Could not found an appropriate authentication backend")

    # Add admin audit log entry
    if original_user_pk:
        change_message = "User {0} logged in as {1}.".format(request.user, user)
        LogEntry.objects.log_action(
            user_id=original_user_pk,
            content_type_id=ContentType.objects.get_for_model(user).pk,
            object_id=user.pk,
            object_repr=str(user),
            change_message=change_message,
            action_flag=CHANGE,
        )

    # Log the user in.
    if not hasattr(user, "backend"):
        return

    if la_settings.UPDATE_LAST_LOGIN:
        login(request, user)
    else:
        with no_update_last_login():
            login(request, user)

    # Set a flag on the session
    if store_original_user:
        messages.warning(
            request,
            la_settings.MESSAGE_LOGIN_SWITCH.format(username=user.__dict__[username_field]),
            extra_tags=la_settings.MESSAGE_EXTRA_TAGS,
        )
        request.session[la_settings.USER_SESSION_FLAG] = signer.sign(original_user_pk)
def login_as(user, request, store_original_user=True):
    """
    Utility function for forcing a login as specific user -- be careful about
    calling this carelessly :)
    """

    # Save the original user pk before it is replaced in the login method
    original_user_pk = request.user.pk

    # Find a suitable backend.
    if not hasattr(user, "backend"):
        for backend in django_settings.AUTHENTICATION_BACKENDS:
            if not hasattr(load_backend(backend), "get_user"):
                continue

            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
        else:
            raise ImproperlyConfigured("Could not found an appropriate authentication backend")

    # Add admin audit log entry
    if original_user_pk:
        change_message = "User {0} logged in as {1}.".format(request.user, user)
        LogEntry.objects.log_action(
            user_id=original_user_pk,
            content_type_id=ContentType.objects.get_for_model(user).pk,
            object_id=user.pk,
            object_repr=str(user),
            change_message=change_message,
            action_flag=CHANGE,
        )

    # Log the user in.
    if not hasattr(user, "backend"):
        return

    if la_settings.UPDATE_LAST_LOGIN:
        login(request, user)
    else:
        with no_update_last_login():
            login(request, user)

    # Set a flag on the session
    if store_original_user:
        messages.warning(
            request,
            la_settings.MESSAGE_LOGIN_SWITCH.format(username=user.__dict__[username_field]),
            extra_tags=la_settings.MESSAGE_EXTRA_TAGS,
        )
        request.session[la_settings.USER_SESSION_FLAG] = signer.sign(original_user_pk)
Utility function for forcing a login as specific user -- be careful about
    calling this carelessly :)