pipeline.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.acl.is_staff:
  10. return None
  11. ip_ban = get_request_ip_ban(strategy.request)
  12. if ip_ban:
  13. raise SocialAuthBanned(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.acl.is_staff:
  17. return None
  18. user_ban = get_user_ban(user)
  19. if user_ban:
  20. raise SocialAuthBanned(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
  30. try:
  31. user = UserModel.objects.get_by_email(email)
  32. except UserModel.DoesNotExist:
  33. return None
  34. if not user.is_active:
  35. backend_name = get_social_auth_backend_name(backend.name)
  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. return {'user': user, 'is_new': False}
  44. def create_user(strategy, details, backend, user=None, *args, **kwargs):
  45. """Aggressively attempt to register and sign in new user"""
  46. if user:
  47. return None
  48. def create_user_with_form(strategy, details, backend, user=None, *args, **kwargs):
  49. """Alternatively to create_user lets user confirm account creation before authenticating"""
  50. if user:
  51. return None