You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modwsgi.py 1.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from django import db
  2. from django.contrib import auth
  3. from django.utils.encoding import force_bytes
  4. def check_password(environ, username, password):
  5. """
  6. Authenticates against Django's auth database
  7. mod_wsgi docs specify None, True, False as return value depending
  8. on whether the user exists and authenticates.
  9. """
  10. UserModel = auth.get_user_model()
  11. # db connection state is managed similarly to the wsgi handler
  12. # as mod_wsgi may call these functions outside of a request/response cycle
  13. db.reset_queries()
  14. try:
  15. try:
  16. user = UserModel._default_manager.get_by_natural_key(username)
  17. except UserModel.DoesNotExist:
  18. return None
  19. if not user.is_active:
  20. return None
  21. return user.check_password(password)
  22. finally:
  23. db.close_old_connections()
  24. def groups_for_user(environ, username):
  25. """
  26. Authorizes a user based on groups
  27. """
  28. UserModel = auth.get_user_model()
  29. db.reset_queries()
  30. try:
  31. try:
  32. user = UserModel._default_manager.get_by_natural_key(username)
  33. except UserModel.DoesNotExist:
  34. return []
  35. if not user.is_active:
  36. return []
  37. return [force_bytes(group.name) for group in user.groups.all()]
  38. finally:
  39. db.close_old_connections()