pipeline.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext as _
  3. from misago.core.exceptions import SocialAuthFailed, SocialAuthBanned
  4. from misago.users.bans import get_request_ip_ban, get_user_ban
  5. from .utils import get_social_auth_backend_name
  6. UserModel = get_user_model()
  7. def validate_ip_not_banned(strategy, details, backend, user=None, *args, **kwargs):
  8. """Pipeline step that interrupts pipeline if found user is non-staff and IP banned"""
  9. if user and user.is_staff:
  10. return None
  11. ip_ban = get_request_ip_ban(strategy.request)
  12. if ip_ban:
  13. raise SocialAuthBanned(backend, ip_ban)
  14. def validate_user_not_banned(strategy, details, backend, user=None, *args, **kwargs):
  15. """Pipeline step that interrupts pipeline if found user is non-staff and banned"""
  16. if user and user.is_staff:
  17. return None
  18. user_ban = get_user_ban(user)
  19. if user_ban:
  20. raise SocialAuthBanned(backend, user_ban)
  21. def associate_by_email(strategy, details, backend, user=None, *args, **kwargs):
  22. """If user with e-mail from provider exists in database and is active,
  23. this step authenticates them.
  24. """
  25. if user:
  26. return None
  27. email = details.get('email')
  28. if not email:
  29. return None
  30. try:
  31. user = UserModel.objects.get_by_email(email)
  32. except UserModel.DoesNotExist:
  33. return None
  34. backend_name = get_social_auth_backend_name(backend.name)
  35. if not user.is_active:
  36. raise SocialAuthFailed(
  37. backend,
  38. _(
  39. "The e-mail address associated with your %(backend)s account is "
  40. "not available for use on this site."
  41. ) % {'backend': backend_name}
  42. )
  43. if user.requires_activation_by_admin:
  44. raise SocialAuthFailed(
  45. backend,
  46. _(
  47. "Your account has to be activated by site administrator before you will be able to "
  48. "sign in with %(backend)s."
  49. ) % {'backend': backend_name}
  50. )
  51. return {'user': user, 'is_new': False}
  52. def create_user(strategy, details, backend, user=None, *args, **kwargs):
  53. """Aggressively attempt to register and sign in new user"""
  54. if user:
  55. return None
  56. def create_user_with_form(strategy, details, backend, user=None, *args, **kwargs):
  57. """Alternatively to create_user lets user confirm account creation before authenticating"""
  58. if user:
  59. return None