django.contrib.auth: 73 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/auth/__init__.py

Stats: 0 executed, 66 missed, 7 excluded, 31 ignored

  1. from warnings import warn
  2. from django.core.exceptions import ImproperlyConfigured
  3. from django.utils.importlib import import_module
  4. from django.contrib.auth.signals import user_logged_in, user_logged_out
  5. SESSION_KEY = '_auth_user_id'
  6. BACKEND_SESSION_KEY = '_auth_user_backend'
  7. REDIRECT_FIELD_NAME = 'next'
  8. def load_backend(path):
  9. i = path.rfind('.')
  10. module, attr = path[:i], path[i+1:]
  11. try:
  12. mod = import_module(module)
  13. except ImportError, e:
  14. raise ImproperlyConfigured('Error importing authentication backend %s: "%s"' % (path, e))
  15. except ValueError, e:
  16. raise ImproperlyConfigured('Error importing authentication backends. Is AUTHENTICATION_BACKENDS a correctly defined list or tuple?')
  17. try:
  18. cls = getattr(mod, attr)
  19. except AttributeError:
  20. raise ImproperlyConfigured('Module "%s" does not define a "%s" authentication backend' % (module, attr))
  21. if not hasattr(cls, 'supports_inactive_user'):
  22. warn("Authentication backends without a `supports_inactive_user` attribute are deprecated. Please define it in %s." % cls,
  23. DeprecationWarning)
  24. cls.supports_inactive_user = False
  25. return cls()
  26. def get_backends():
  27. from django.conf import settings
  28. backends = []
  29. for backend_path in settings.AUTHENTICATION_BACKENDS:
  30. backends.append(load_backend(backend_path))
  31. if not backends:
  32. raise ImproperlyConfigured('No authentication backends have been defined. Does AUTHENTICATION_BACKENDS contain anything?')
  33. return backends
  34. def authenticate(**credentials):
  35. """
  36. If the given credentials are valid, return a User object.
  37. """
  38. for backend in get_backends():
  39. try:
  40. user = backend.authenticate(**credentials)
  41. except TypeError:
  42. # This backend doesn't accept these credentials as arguments. Try the next one.
  43. continue
  44. if user is None:
  45. continue
  46. # Annotate the user object with the path of the backend.
  47. user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
  48. return user
  49. def login(request, user):
  50. """
  51. Persist a user id and a backend in the request. This way a user doesn't
  52. have to reauthenticate on every request. Note that data set during
  53. the anonymous session is retained when the user logs in.
  54. """
  55. if user is None:
  56. user = request.user
  57. # TODO: It would be nice to support different login methods, like signed cookies.
  58. if SESSION_KEY in request.session:
  59. if request.session[SESSION_KEY] != user.id:
  60. # To avoid reusing another user's session, create a new, empty
  61. # session if the existing session corresponds to a different
  62. # authenticated user.
  63. request.session.flush()
  64. else:
  65. request.session.cycle_key()
  66. request.session[SESSION_KEY] = user.id
  67. request.session[BACKEND_SESSION_KEY] = user.backend
  68. if hasattr(request, 'user'):
  69. request.user = user
  70. user_logged_in.send(sender=user.__class__, request=request, user=user)
  71. def logout(request):
  72. """
  73. Removes the authenticated user's ID from the request and flushes their
  74. session data.
  75. """
  76. # Dispatch the signal before the user is logged out so the receivers have a
  77. # chance to find out *who* logged out.
  78. user = getattr(request, 'user', None)
  79. if hasattr(user, 'is_authenticated') and not user.is_authenticated():
  80. user = None
  81. user_logged_out.send(sender=user.__class__, request=request, user=user)
  82. request.session.flush()
  83. if hasattr(request, 'user'):
  84. from django.contrib.auth.models import AnonymousUser
  85. request.user = AnonymousUser()
  86. def get_user(request):
  87. from django.contrib.auth.models import AnonymousUser
  88. try:
  89. user_id = request.session[SESSION_KEY]
  90. backend_path = request.session[BACKEND_SESSION_KEY]
  91. backend = load_backend(backend_path)
  92. user = backend.get_user(user_id) or AnonymousUser()
  93. except KeyError:
  94. user = AnonymousUser()
  95. return user