Example #1
0
def logout(request):
    """
    Logs the user out. This will clear the user's login session data
    from the auth session storage.
    """
    from django.conf import settings
    # TODO: pre logout signal

    user_prop = getattr(settings, 'NEWAUTH_USER_PROPERTY', DEFAULT_USER_PROPERTY)
    if hasattr(request, user_prop):
        from newauth.models import (
            AnonymousUser, get_anonymous_user_model,
        )

        user = getattr(request, user_prop)
        if hasattr(user, '_backend_name'):
            # Set the current request user to the anonymous user
            # given by the backend of the user being logged out.
            setattr(request, user_prop, get_anonymous_user_model(user._backend_name)())
        elif not isinstance(user, AnonymousUserBase):
            # if the user has no backend set and is not already
            # an anonymous user then set the user to the default
            # anonymous user.
            setattr(request, user_prop, AnonymousUser())
    request.session.flush()
Example #2
0
def get_user(user_id, backend_name=None):
    """
    Get the user from a user_id. If a backend_name
    is given then the backend with that name will
    be used.

    If a backend_name is not given (default) then
    all backends are tried.

:
    This method should be used to get a user
    rather than getting the user directly from
    the database as it associates the correct
    backend with the user.
    """
    # Avoid making DB queries when we know that the user
    # doesn't exist.
    if user_id is not None:
        if backend_name:
            backends = [(backend_name, load_backends(backend_name))]
        else:
            backends = get_backends()

        for name, backend_list in backends:
            for backend in backend_list:
                user = backend.get_user(user_id=user_id)
                if user:
                    user._backend = backend
                    user._backend_name = name
                    return user

    # If the backend name is given then use
    # that backend's anonymous user
    if backend_name:
        from newauth.models import get_anonymous_user_model
        return get_anonymous_user_model(backend_name)()

    # If no backend is specified and no
    # backend can get the user then use
    # the default anonymous user class.
    from newauth.models import AnonymousUser
    return AnonymousUser()
Example #3
0
 def anon_user_model(self):
     return get_anonymous_user_model(self.backend_name)