PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/auth/forms.py

https://bitbucket.org/bluezoo/django
Python | 356 lines | 293 code | 33 blank | 30 comment | 30 complexity | 4e7f4a1defd32c512492bae8f879fa65 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from django.forms.util import flatatt
  4. from django.template import loader
  5. from django.utils.datastructures import SortedDict
  6. from django.utils.html import format_html, format_html_join
  7. from django.utils.http import int_to_base36
  8. from django.utils.safestring import mark_safe
  9. from django.utils.text import capfirst
  10. from django.utils.translation import ugettext, ugettext_lazy as _
  11. from django.contrib.auth import authenticate, get_user_model
  12. from django.contrib.auth.models import User
  13. from django.contrib.auth.hashers import UNUSABLE_PASSWORD, identify_hasher
  14. from django.contrib.auth.tokens import default_token_generator
  15. from django.contrib.sites.models import get_current_site
  16. UNMASKED_DIGITS_TO_SHOW = 6
  17. mask_password = lambda p: "%s%s" % (p[:UNMASKED_DIGITS_TO_SHOW], "*" * max(len(p) - UNMASKED_DIGITS_TO_SHOW, 0))
  18. class ReadOnlyPasswordHashWidget(forms.Widget):
  19. def render(self, name, value, attrs):
  20. encoded = value
  21. final_attrs = self.build_attrs(attrs)
  22. if not encoded or encoded == UNUSABLE_PASSWORD:
  23. summary = mark_safe("<strong>%s</strong>" % ugettext("No password set."))
  24. else:
  25. try:
  26. hasher = identify_hasher(encoded)
  27. except ValueError:
  28. summary = mark_safe("<strong>%s</strong>" % ugettext(
  29. "Invalid password format or unknown hashing algorithm."))
  30. else:
  31. summary = format_html_join('',
  32. "<strong>{0}</strong>: {1} ",
  33. ((ugettext(key), value)
  34. for key, value in hasher.safe_summary(encoded).items())
  35. )
  36. return format_html("<div{0}>{1}</div>", flatatt(final_attrs), summary)
  37. class ReadOnlyPasswordHashField(forms.Field):
  38. widget = ReadOnlyPasswordHashWidget
  39. def __init__(self, *args, **kwargs):
  40. kwargs.setdefault("required", False)
  41. super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
  42. def bound_data(self, data, initial):
  43. # Always return initial because the widget doesn't
  44. # render an input field.
  45. return initial
  46. class UserCreationForm(forms.ModelForm):
  47. """
  48. A form that creates a user, with no privileges, from the given username and
  49. password.
  50. """
  51. error_messages = {
  52. 'duplicate_username': _("A user with that username already exists."),
  53. 'password_mismatch': _("The two password fields didn't match."),
  54. }
  55. username = forms.RegexField(label=_("Username"), max_length=30,
  56. regex=r'^[\w.@+-]+$',
  57. help_text=_("Required. 30 characters or fewer. Letters, digits and "
  58. "@/./+/-/_ only."),
  59. error_messages={
  60. 'invalid': _("This value may contain only letters, numbers and "
  61. "@/./+/-/_ characters.")})
  62. password1 = forms.CharField(label=_("Password"),
  63. widget=forms.PasswordInput)
  64. password2 = forms.CharField(label=_("Password confirmation"),
  65. widget=forms.PasswordInput,
  66. help_text=_("Enter the same password as above, for verification."))
  67. class Meta:
  68. model = User
  69. fields = ("username",)
  70. def clean_username(self):
  71. # Since User.username is unique, this check is redundant,
  72. # but it sets a nicer error message than the ORM. See #13147.
  73. username = self.cleaned_data["username"]
  74. try:
  75. User.objects.get(username=username)
  76. except User.DoesNotExist:
  77. return username
  78. raise forms.ValidationError(self.error_messages['duplicate_username'])
  79. def clean_password2(self):
  80. password1 = self.cleaned_data.get("password1")
  81. password2 = self.cleaned_data.get("password2")
  82. if password1 and password2 and password1 != password2:
  83. raise forms.ValidationError(
  84. self.error_messages['password_mismatch'])
  85. return password2
  86. def save(self, commit=True):
  87. user = super(UserCreationForm, self).save(commit=False)
  88. user.set_password(self.cleaned_data["password1"])
  89. if commit:
  90. user.save()
  91. return user
  92. class UserChangeForm(forms.ModelForm):
  93. username = forms.RegexField(
  94. label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
  95. help_text=_("Required. 30 characters or fewer. Letters, digits and "
  96. "@/./+/-/_ only."),
  97. error_messages={
  98. 'invalid': _("This value may contain only letters, numbers and "
  99. "@/./+/-/_ characters.")})
  100. password = ReadOnlyPasswordHashField(label=_("Password"),
  101. help_text=_("Raw passwords are not stored, so there is no way to see "
  102. "this user's password, but you can change the password "
  103. "using <a href=\"password/\">this form</a>."))
  104. class Meta:
  105. model = User
  106. def __init__(self, *args, **kwargs):
  107. super(UserChangeForm, self).__init__(*args, **kwargs)
  108. f = self.fields.get('user_permissions', None)
  109. if f is not None:
  110. f.queryset = f.queryset.select_related('content_type')
  111. def clean_password(self):
  112. # Regardless of what the user provides, return the initial value.
  113. # This is done here, rather than on the field, because the
  114. # field does not have access to the initial value
  115. return self.initial["password"]
  116. class AuthenticationForm(forms.Form):
  117. """
  118. Base class for authenticating users. Extend this to get a form that accepts
  119. username/password logins.
  120. """
  121. username = forms.CharField(max_length=254)
  122. password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
  123. error_messages = {
  124. 'invalid_login': _("Please enter a correct %(username)s and password. "
  125. "Note that both fields may be case-sensitive."),
  126. 'no_cookies': _("Your Web browser doesn't appear to have cookies "
  127. "enabled. Cookies are required for logging in."),
  128. 'inactive': _("This account is inactive."),
  129. }
  130. def __init__(self, request=None, *args, **kwargs):
  131. """
  132. If request is passed in, the form will validate that cookies are
  133. enabled. Note that the request (a HttpRequest object) must have set a
  134. cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
  135. running this validation.
  136. """
  137. self.request = request
  138. self.user_cache = None
  139. super(AuthenticationForm, self).__init__(*args, **kwargs)
  140. # Set the label for the "username" field.
  141. UserModel = get_user_model()
  142. self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
  143. self.fields['username'].label = capfirst(self.username_field.verbose_name)
  144. def clean(self):
  145. username = self.cleaned_data.get('username')
  146. password = self.cleaned_data.get('password')
  147. if username and password:
  148. self.user_cache = authenticate(username=username,
  149. password=password)
  150. if self.user_cache is None:
  151. raise forms.ValidationError(
  152. self.error_messages['invalid_login'] % {
  153. 'username': self.username_field.verbose_name
  154. })
  155. elif not self.user_cache.is_active:
  156. raise forms.ValidationError(self.error_messages['inactive'])
  157. self.check_for_test_cookie()
  158. return self.cleaned_data
  159. def check_for_test_cookie(self):
  160. if self.request and not self.request.session.test_cookie_worked():
  161. raise forms.ValidationError(self.error_messages['no_cookies'])
  162. def get_user_id(self):
  163. if self.user_cache:
  164. return self.user_cache.id
  165. return None
  166. def get_user(self):
  167. return self.user_cache
  168. class PasswordResetForm(forms.Form):
  169. error_messages = {
  170. 'unknown': _("That email address doesn't have an associated "
  171. "user account. Are you sure you've registered?"),
  172. 'unusable': _("The user account associated with this email "
  173. "address cannot reset the password."),
  174. }
  175. email = forms.EmailField(label=_("Email"), max_length=254)
  176. def clean_email(self):
  177. """
  178. Validates that an active user exists with the given email address.
  179. """
  180. UserModel = get_user_model()
  181. email = self.cleaned_data["email"]
  182. self.users_cache = UserModel.objects.filter(email__iexact=email)
  183. if not len(self.users_cache):
  184. raise forms.ValidationError(self.error_messages['unknown'])
  185. if not any(user.is_active for user in self.users_cache):
  186. # none of the filtered users are active
  187. raise forms.ValidationError(self.error_messages['unknown'])
  188. if any((user.password == UNUSABLE_PASSWORD)
  189. for user in self.users_cache):
  190. raise forms.ValidationError(self.error_messages['unusable'])
  191. return email
  192. def save(self, domain_override=None,
  193. subject_template_name='registration/password_reset_subject.txt',
  194. email_template_name='registration/password_reset_email.html',
  195. use_https=False, token_generator=default_token_generator,
  196. from_email=None, request=None):
  197. """
  198. Generates a one-use only link for resetting password and sends to the
  199. user.
  200. """
  201. from django.core.mail import send_mail
  202. for user in self.users_cache:
  203. if not domain_override:
  204. current_site = get_current_site(request)
  205. site_name = current_site.name
  206. domain = current_site.domain
  207. else:
  208. site_name = domain = domain_override
  209. c = {
  210. 'email': user.email,
  211. 'domain': domain,
  212. 'site_name': site_name,
  213. 'uid': int_to_base36(user.pk),
  214. 'user': user,
  215. 'token': token_generator.make_token(user),
  216. 'protocol': use_https and 'https' or 'http',
  217. }
  218. subject = loader.render_to_string(subject_template_name, c)
  219. # Email subject *must not* contain newlines
  220. subject = ''.join(subject.splitlines())
  221. email = loader.render_to_string(email_template_name, c)
  222. send_mail(subject, email, from_email, [user.email])
  223. class SetPasswordForm(forms.Form):
  224. """
  225. A form that lets a user change set his/her password without entering the
  226. old password
  227. """
  228. error_messages = {
  229. 'password_mismatch': _("The two password fields didn't match."),
  230. }
  231. new_password1 = forms.CharField(label=_("New password"),
  232. widget=forms.PasswordInput)
  233. new_password2 = forms.CharField(label=_("New password confirmation"),
  234. widget=forms.PasswordInput)
  235. def __init__(self, user, *args, **kwargs):
  236. self.user = user
  237. super(SetPasswordForm, self).__init__(*args, **kwargs)
  238. def clean_new_password2(self):
  239. password1 = self.cleaned_data.get('new_password1')
  240. password2 = self.cleaned_data.get('new_password2')
  241. if password1 and password2:
  242. if password1 != password2:
  243. raise forms.ValidationError(
  244. self.error_messages['password_mismatch'])
  245. return password2
  246. def save(self, commit=True):
  247. self.user.set_password(self.cleaned_data['new_password1'])
  248. if commit:
  249. self.user.save()
  250. return self.user
  251. class PasswordChangeForm(SetPasswordForm):
  252. """
  253. A form that lets a user change his/her password by entering
  254. their old password.
  255. """
  256. error_messages = dict(SetPasswordForm.error_messages, **{
  257. 'password_incorrect': _("Your old password was entered incorrectly. "
  258. "Please enter it again."),
  259. })
  260. old_password = forms.CharField(label=_("Old password"),
  261. widget=forms.PasswordInput)
  262. def clean_old_password(self):
  263. """
  264. Validates that the old_password field is correct.
  265. """
  266. old_password = self.cleaned_data["old_password"]
  267. if not self.user.check_password(old_password):
  268. raise forms.ValidationError(
  269. self.error_messages['password_incorrect'])
  270. return old_password
  271. PasswordChangeForm.base_fields = SortedDict([
  272. (k, PasswordChangeForm.base_fields[k])
  273. for k in ['old_password', 'new_password1', 'new_password2']
  274. ])
  275. class AdminPasswordChangeForm(forms.Form):
  276. """
  277. A form used to change the password of a user in the admin interface.
  278. """
  279. error_messages = {
  280. 'password_mismatch': _("The two password fields didn't match."),
  281. }
  282. password1 = forms.CharField(label=_("Password"),
  283. widget=forms.PasswordInput)
  284. password2 = forms.CharField(label=_("Password (again)"),
  285. widget=forms.PasswordInput)
  286. def __init__(self, user, *args, **kwargs):
  287. self.user = user
  288. super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
  289. def clean_password2(self):
  290. password1 = self.cleaned_data.get('password1')
  291. password2 = self.cleaned_data.get('password2')
  292. if password1 and password2:
  293. if password1 != password2:
  294. raise forms.ValidationError(
  295. self.error_messages['password_mismatch'])
  296. return password2
  297. def save(self, commit=True):
  298. """
  299. Saves the new password.
  300. """
  301. self.user.set_password(self.cleaned_data["password1"])
  302. if commit:
  303. self.user.save()
  304. return self.user