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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django import db
  2. from django.contrib import auth
  3. UserModel = auth.get_user_model()
  4. def check_password(environ, username, password):
  5. """
  6. Authenticate 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. # db connection state is managed similarly to the wsgi handler
  11. # as mod_wsgi may call these functions outside of a request/response cycle
  12. db.reset_queries()
  13. try:
  14. try:
  15. user = UserModel._default_manager.get_by_natural_key(username)
  16. except UserModel.DoesNotExist:
  17. return None
  18. if not user.is_active:
  19. return None
  20. return user.check_password(password)
  21. finally:
  22. db.close_old_connections()
  23. def groups_for_user(environ, username):
  24. """
  25. Authorize a user based on groups
  26. """
  27. db.reset_queries()
  28. try:
  29. try:
  30. user = UserModel._default_manager.get_by_natural_key(username)
  31. except UserModel.DoesNotExist:
  32. return []
  33. if not user.is_active:
  34. return []
  35. return [group.name.encode() for group in user.groups.all()]
  36. finally:
  37. db.close_old_connections()