pipeline.py 2.6 KB

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