auth.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from django import forms
  2. from django.contrib.auth import authenticate, get_user_model
  3. from django.contrib.auth.forms import AuthenticationForm as BaseAuthenticationForm
  4. from django.core.exceptions import ValidationError
  5. from django.core.validators import validate_email
  6. from django.utils.translation import ugettext_lazy as _
  7. from misago.users.bans import get_user_ban
  8. UserModel = get_user_model()
  9. class MisagoAuthMixin(object):
  10. error_messages = {
  11. 'empty_data': _("Fill out both fields."),
  12. 'invalid_login': _("Login or password is incorrect."),
  13. 'inactive_user': _("You have to activate your account before you will be able to sign in."),
  14. 'inactive_admin': _(
  15. "Your account has to be activated by Administrator before you will be able to sign in."
  16. ),
  17. }
  18. def confirm_user_active(self, user):
  19. if user.requires_activation_by_admin:
  20. raise ValidationError(self.error_messages['inactive_admin'], code='inactive_admin')
  21. if user.requires_activation_by_user:
  22. raise ValidationError(self.error_messages['inactive_user'], code='inactive_user')
  23. def confirm_user_not_banned(self, user):
  24. if not user.is_staff:
  25. self.user_ban = get_user_ban(user)
  26. if self.user_ban:
  27. raise ValidationError('', code='banned')
  28. def get_errors_dict(self):
  29. error = self.errors.as_data()['__all__'][0]
  30. if error.code == 'banned':
  31. return self.user_ban.ban.get_serialized_message()
  32. else:
  33. error.message = error.messages[0]
  34. return {'detail': error.message, 'code': error.code}
  35. class AuthenticationForm(MisagoAuthMixin, BaseAuthenticationForm):
  36. """
  37. Base class for authenticating users, Floppy-forms and
  38. Misago login field compliant
  39. """
  40. username = forms.CharField(
  41. label=_("Username or e-mail"),
  42. required=False,
  43. max_length=254,
  44. )
  45. password = forms.CharField(
  46. label=_("Password"),
  47. strip=False,
  48. required=False,
  49. widget=forms.PasswordInput,
  50. )
  51. def clean(self):
  52. username = self.cleaned_data.get('username')
  53. password = self.cleaned_data.get('password')
  54. if username and password:
  55. self.user_cache = authenticate(username=username, password=password)
  56. if self.user_cache is None or not self.user_cache.is_active:
  57. raise ValidationError(self.error_messages['invalid_login'], code='invalid_login')
  58. else:
  59. self.confirm_login_allowed(self.user_cache)
  60. else:
  61. raise ValidationError(self.error_messages['empty_data'], code='empty_data')
  62. return self.cleaned_data
  63. def confirm_login_allowed(self, user):
  64. self.confirm_user_active(user)
  65. self.confirm_user_not_banned(user)
  66. class AdminAuthenticationForm(AuthenticationForm):
  67. required_css_class = 'required'
  68. def __init__(self, *args, **kwargs):
  69. self.error_messages.update({
  70. 'not_staff': _("Your account does not have admin privileges."),
  71. })
  72. super(AdminAuthenticationForm, self).__init__(*args, **kwargs)
  73. def confirm_login_allowed(self, user):
  74. if not user.is_staff:
  75. raise forms.ValidationError(self.error_messages['not_staff'], code='not_staff')