auth.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.core.exceptions import ValidationError
  2. from django.contrib.auth import authenticate
  3. from django.contrib.auth.forms import (AuthenticationForm as
  4. BaseAuthenticationForm)
  5. from django.template.defaultfilters import date as format_date
  6. from django.utils.translation import ugettext_lazy as _
  7. from misago.core import forms
  8. from misago.users.bans import get_user_ban
  9. class AuthenticationForm(forms.Form, BaseAuthenticationForm):
  10. """
  11. Base class for authenticating users, Floppy-forms and
  12. Misago login field comliant
  13. """
  14. username = forms.CharField(label=_("Username or e-mail"),
  15. required=False,
  16. max_length=254)
  17. password = forms.CharField(label=_("Password"), required=False,
  18. widget=forms.PasswordInput)
  19. error_messages = {
  20. 'empty_data': _("You have to fill out both fields."),
  21. 'invalid_login': _("Your login or password is incorrect."),
  22. 'inactive_user': _("You have to activate your account before "
  23. "you will be able to sign in."),
  24. 'inactive_admin': _("Administrator has to activate your account "
  25. "before you will be able to sign in."),
  26. }
  27. def clean(self):
  28. username = self.cleaned_data.get('username')
  29. password = self.cleaned_data.get('password')
  30. if username and password:
  31. self.user_cache = authenticate(username=username,
  32. password=password)
  33. if self.user_cache is None or not self.user_cache.is_active:
  34. raise ValidationError(
  35. self.error_messages['invalid_login'],
  36. code='invalid_login',
  37. )
  38. else:
  39. self.confirm_login_allowed(self.user_cache)
  40. else:
  41. raise ValidationError(
  42. self.error_messages['empty_data'],
  43. code='empty_data',
  44. )
  45. return self.cleaned_data
  46. def confirm_login_allowed(self, user):
  47. if user.requires_activation_by_admin:
  48. raise ValidationError(
  49. self.error_messages['inactive_admin'],
  50. code='inactive_admin',
  51. )
  52. if user.requires_activation_by_user:
  53. raise ValidationError(
  54. self.error_messages['inactive_user'],
  55. code='inactive_user',
  56. )
  57. self.user_ban = get_user_ban(user)
  58. if self.user_ban:
  59. if self.user_ban.valid_until:
  60. if self.user_ban.user_message:
  61. message = _("%(username)s, your account is "
  62. "banned until %(date)s for:")
  63. else:
  64. message = _("%(username)s, your account "
  65. "is banned until %(date)s.")
  66. date_format = {'date': format_date(self.user_ban.valid_until)}
  67. message = message % date_format
  68. else:
  69. if self.user_ban.user_message:
  70. message = _("%(username)s, your account is banned for:")
  71. else:
  72. message = _("%(username)s, your account is banned.")
  73. raise ValidationError(
  74. message % {'username': self.user_cache.username},
  75. code='banned',
  76. )
  77. class AdminAuthenticationForm(AuthenticationForm):
  78. required_css_class = 'required'
  79. def __init__(self, *args, **kwargs):
  80. self.error_messages.update({
  81. 'not_staff': _("Your account does not have admin privileges.")
  82. })
  83. super(AdminAuthenticationForm, self).__init__(*args, **kwargs)
  84. def confirm_login_allowed(self, user):
  85. if not user.is_staff:
  86. raise forms.ValidationError(
  87. self.error_messages['not_staff'],
  88. code='not_staff',
  89. )