Development of an internal social media platform with personalised dashboards for students
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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django import db
  2. from django.contrib import auth
  3. from django.utils.encoding import force_bytes
  4. UserModel = auth.get_user_model()
  5. def check_password(environ, username, password):
  6. """
  7. Authenticate against Django's auth database.
  8. mod_wsgi docs specify None, True, False as return value depending
  9. on whether the user exists and authenticates.
  10. """
  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. Authorize a user based on groups
  27. """
  28. db.reset_queries()
  29. try:
  30. try:
  31. user = UserModel._default_manager.get_by_natural_key(username)
  32. except UserModel.DoesNotExist:
  33. return []
  34. if not user.is_active:
  35. return []
  36. return [force_bytes(group.name) for group in user.groups.all()]
  37. finally:
  38. db.close_old_connections()