2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
from django import forms
|
2016-11-05 04:13:40 +01:00
|
|
|
from django.conf import settings
|
2016-04-28 23:07:41 +02:00
|
|
|
from django.contrib.auth.forms import SetPasswordForm, AuthenticationForm, \
|
|
|
|
PasswordResetForm
|
2016-11-05 04:13:40 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-06-03 01:02:58 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-12-20 10:41:46 +01:00
|
|
|
from django.core.validators import validate_email
|
2016-11-05 04:13:40 +01:00
|
|
|
from django.db.models.query import QuerySet
|
2016-06-03 01:02:58 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2016-11-05 04:13:40 +01:00
|
|
|
from jinja2 import Markup as mark_safe
|
|
|
|
|
2017-08-25 07:12:26 +02:00
|
|
|
from zerver.lib.actions import do_change_password, user_email_is_unique, \
|
|
|
|
validate_email_for_realm
|
2016-11-05 03:26:30 +01:00
|
|
|
from zerver.lib.name_restrictions import is_reserved_subdomain, is_disposable_domain
|
2017-02-08 05:04:14 +01:00
|
|
|
from zerver.lib.request import JsonableError
|
2017-07-02 05:27:01 +02:00
|
|
|
from zerver.lib.send_email import send_email, FromAddress
|
2017-02-08 05:04:14 +01:00
|
|
|
from zerver.lib.users import check_full_name
|
2016-07-19 14:35:08 +02:00
|
|
|
from zerver.lib.utils import get_subdomain, check_subdomain
|
2013-08-07 17:59:45 +02:00
|
|
|
from zerver.models import Realm, get_user_profile_by_email, UserProfile, \
|
2017-01-04 05:30:48 +01:00
|
|
|
get_realm_by_email_domain, get_realm, \
|
2016-11-11 21:13:30 +01:00
|
|
|
get_unique_open_realm, email_to_domain, email_allowed_for_realm
|
2013-11-13 23:23:59 +01:00
|
|
|
from zproject.backends import password_auth_enabled
|
2016-11-05 04:13:40 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import re
|
2013-08-12 00:47:28 +02:00
|
|
|
import DNS
|
2016-11-05 04:13:40 +01:00
|
|
|
|
2017-04-24 12:19:54 +02:00
|
|
|
from typing import Any, Callable, List, Optional, Text, Dict
|
2012-09-26 20:08:39 +02:00
|
|
|
|
2016-06-23 23:34:37 +02:00
|
|
|
MIT_VALIDATION_ERROR = u'That user does not exist at MIT or is a ' + \
|
|
|
|
u'<a href="https://ist.mit.edu/email-lists">mailing list</a>. ' + \
|
|
|
|
u'If you want to sign up an alias for Zulip, ' + \
|
2016-07-19 14:35:08 +02:00
|
|
|
u'<a href="mailto:support@zulipchat.com">contact us</a>.'
|
|
|
|
WRONG_SUBDOMAIN_ERROR = "Your Zulip account is not a member of the " + \
|
|
|
|
"organization associated with this subdomain. " + \
|
2017-07-02 05:27:01 +02:00
|
|
|
"Please contact %s with any questions!" % (FromAddress.SUPPORT,)
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2016-11-06 00:29:55 +01:00
|
|
|
def email_is_not_mit_mailing_list(email):
|
2016-12-27 07:09:35 +01:00
|
|
|
# type: (Text) -> None
|
2016-07-27 02:39:14 +02:00
|
|
|
"""Prevent MIT mailing lists from signing up for Zulip"""
|
2016-11-06 00:29:55 +01:00
|
|
|
if "@mit.edu" in email:
|
|
|
|
username = email.rsplit("@", 1)[0]
|
2013-08-12 00:47:28 +02:00
|
|
|
# Check whether the user exists and can get mail.
|
|
|
|
try:
|
|
|
|
DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT)
|
2015-11-01 17:08:33 +01:00
|
|
|
except DNS.Base.ServerError as e:
|
2013-08-12 00:47:28 +02:00
|
|
|
if e.rcode == DNS.Status.NXDOMAIN:
|
2016-06-23 23:34:37 +02:00
|
|
|
raise ValidationError(mark_safe(MIT_VALIDATION_ERROR))
|
2013-08-12 00:47:28 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
class RegistrationForm(forms.Form):
|
2017-03-23 00:15:06 +01:00
|
|
|
MAX_PASSWORD_LENGTH = 100
|
2017-06-16 14:40:41 +02:00
|
|
|
full_name = forms.CharField(max_length=UserProfile.MAX_NAME_LENGTH)
|
2014-03-28 00:45:03 +01:00
|
|
|
# The required-ness of the password field gets overridden if it isn't
|
|
|
|
# actually required for a realm
|
2017-08-07 10:12:37 +02:00
|
|
|
password = forms.CharField(widget=forms.PasswordInput, max_length=MAX_PASSWORD_LENGTH)
|
2017-03-23 00:15:06 +01:00
|
|
|
realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
|
2016-09-16 19:05:14 +02:00
|
|
|
|
2017-06-15 11:35:04 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# type: (*Any, **Any) -> None
|
2017-06-15 19:24:38 +02:00
|
|
|
|
|
|
|
# Since the superclass doesn't except random extra kwargs, we
|
|
|
|
# remove it from the kwargs dict before initializing.
|
|
|
|
realm_creation = kwargs['realm_creation']
|
|
|
|
del kwargs['realm_creation']
|
|
|
|
|
2017-06-15 11:35:04 +02:00
|
|
|
super(RegistrationForm, self).__init__(*args, **kwargs)
|
|
|
|
if settings.TERMS_OF_SERVICE:
|
|
|
|
self.fields['terms'] = forms.BooleanField(required=True)
|
2017-06-15 19:24:46 +02:00
|
|
|
self.fields['realm_name'] = forms.CharField(
|
|
|
|
max_length=Realm.MAX_REALM_NAME_LENGTH,
|
|
|
|
required=realm_creation)
|
2012-09-28 22:47:05 +02:00
|
|
|
|
2017-02-08 05:04:14 +01:00
|
|
|
def clean_full_name(self):
|
|
|
|
# type: () -> Text
|
|
|
|
try:
|
|
|
|
return check_full_name(self.cleaned_data['full_name'])
|
|
|
|
except JsonableError as e:
|
2017-07-20 00:22:36 +02:00
|
|
|
raise ValidationError(e.msg)
|
2017-02-08 05:04:14 +01:00
|
|
|
|
2016-07-19 14:35:08 +02:00
|
|
|
def clean_realm_subdomain(self):
|
|
|
|
# type: () -> str
|
2016-10-31 23:28:20 +01:00
|
|
|
if settings.REALMS_HAVE_SUBDOMAINS:
|
|
|
|
error_strings = {
|
|
|
|
'too short': _("Subdomain needs to have length 3 or greater."),
|
|
|
|
'extremal dash': _("Subdomain cannot start or end with a '-'."),
|
|
|
|
'bad character': _("Subdomain can only have lowercase letters, numbers, and '-'s."),
|
|
|
|
'unavailable': _("Subdomain unavailable. Please choose a different one.")}
|
|
|
|
else:
|
|
|
|
error_strings = {
|
|
|
|
'too short': _("Short name needs at least 3 characters."),
|
|
|
|
'extremal dash': _("Short name cannot start or end with a '-'."),
|
|
|
|
'bad character': _("Short name can only have lowercase letters, numbers, and '-'s."),
|
|
|
|
'unavailable': _("Short name unavailable. Please choose a different one.")}
|
|
|
|
subdomain = self.cleaned_data['realm_subdomain']
|
|
|
|
if not subdomain:
|
|
|
|
return ''
|
|
|
|
if len(subdomain) < 3:
|
|
|
|
raise ValidationError(error_strings['too short'])
|
|
|
|
if subdomain[0] == '-' or subdomain[-1] == '-':
|
|
|
|
raise ValidationError(error_strings['extremal dash'])
|
|
|
|
if not re.match('^[a-z0-9-]*$', subdomain):
|
|
|
|
raise ValidationError(error_strings['bad character'])
|
|
|
|
if is_reserved_subdomain(subdomain) or \
|
2017-01-04 05:30:48 +01:00
|
|
|
get_realm(subdomain) is not None:
|
2016-10-31 23:28:20 +01:00
|
|
|
raise ValidationError(error_strings['unavailable'])
|
|
|
|
return subdomain
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2013-01-08 23:26:40 +01:00
|
|
|
class ToSForm(forms.Form):
|
|
|
|
terms = forms.BooleanField(required=True)
|
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
class HomepageForm(forms.Form):
|
2017-08-25 07:12:26 +02:00
|
|
|
email = forms.EmailField()
|
2012-12-13 21:08:07 +01:00
|
|
|
|
2013-08-07 17:59:45 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-06-05 20:47:49 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2016-12-24 02:34:36 +01:00
|
|
|
self.realm = kwargs.pop('realm', None)
|
2013-08-07 17:59:45 +02:00
|
|
|
super(HomepageForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean_email(self):
|
2016-06-05 20:47:49 +02:00
|
|
|
# type: () -> str
|
2016-07-27 02:39:14 +02:00
|
|
|
"""Returns the email if and only if the user's email address is
|
|
|
|
allowed to join the realm they are trying to join."""
|
2016-11-05 16:28:55 +01:00
|
|
|
email = self.cleaned_data['email']
|
2016-11-06 00:49:35 +01:00
|
|
|
|
2016-07-27 02:39:14 +02:00
|
|
|
if get_unique_open_realm():
|
2016-11-05 16:28:55 +01:00
|
|
|
return email
|
2016-07-27 02:39:14 +02:00
|
|
|
|
2016-11-06 00:49:35 +01:00
|
|
|
# Otherwise, the user is trying to join a specific realm.
|
2016-12-24 02:34:36 +01:00
|
|
|
realm = self.realm
|
|
|
|
if realm is None and not settings.REALMS_HAVE_SUBDOMAINS:
|
2016-11-09 02:40:54 +01:00
|
|
|
realm = get_realm_by_email_domain(email)
|
2016-11-06 00:55:39 +01:00
|
|
|
|
2016-11-06 01:44:52 +01:00
|
|
|
if realm is None:
|
|
|
|
if settings.REALMS_HAVE_SUBDOMAINS:
|
2017-04-14 11:01:24 +02:00
|
|
|
raise ValidationError(_("The organization you are trying to "
|
|
|
|
"join using {email} does not "
|
|
|
|
"exist.").format(email=email))
|
2016-11-06 01:44:52 +01:00
|
|
|
else:
|
2017-04-14 11:01:24 +02:00
|
|
|
raise ValidationError(_("Your email address, {email}, does not "
|
|
|
|
"correspond to any existing "
|
|
|
|
"organization.").format(email=email))
|
2016-11-06 01:44:52 +01:00
|
|
|
|
|
|
|
if realm.invite_required:
|
2017-04-14 11:01:24 +02:00
|
|
|
raise ValidationError(_("Please request an invite for {email} "
|
|
|
|
"from the organization "
|
|
|
|
"administrator.").format(email=email))
|
2016-11-06 00:49:35 +01:00
|
|
|
|
2016-11-06 01:41:38 +01:00
|
|
|
if not email_allowed_for_realm(email, realm):
|
|
|
|
raise ValidationError(
|
2017-04-14 11:01:24 +02:00
|
|
|
_("Your email address, {email}, is not in one of the domains "
|
2017-04-20 20:39:16 +02:00
|
|
|
"that are allowed to register for accounts in this organization.").format(
|
|
|
|
string_id=realm.string_id, email=email))
|
2016-11-06 01:41:38 +01:00
|
|
|
|
2017-08-25 07:12:26 +02:00
|
|
|
validate_email_for_realm(realm, email)
|
|
|
|
|
2016-11-06 00:29:55 +01:00
|
|
|
if realm.is_zephyr_mirror_realm:
|
|
|
|
email_is_not_mit_mailing_list(email)
|
2016-07-27 02:39:14 +02:00
|
|
|
|
2016-11-06 00:29:55 +01:00
|
|
|
return email
|
2013-08-07 17:59:45 +02:00
|
|
|
|
2016-11-05 03:26:30 +01:00
|
|
|
def email_is_not_disposable(email):
|
2016-12-27 07:09:35 +01:00
|
|
|
# type: (Text) -> None
|
2016-11-11 21:13:30 +01:00
|
|
|
if is_disposable_domain(email_to_domain(email)):
|
2016-11-05 03:26:30 +01:00
|
|
|
raise ValidationError(_("Please use your real email address."))
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
class RealmCreationForm(forms.Form):
|
2016-12-24 03:24:15 +01:00
|
|
|
# This form determines whether users can create a new realm.
|
2016-11-05 03:26:30 +01:00
|
|
|
email = forms.EmailField(validators=[user_email_is_unique, email_is_not_disposable])
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2012-12-13 21:08:07 +01:00
|
|
|
class LoggingSetPasswordForm(SetPasswordForm):
|
|
|
|
def save(self, commit=True):
|
2016-06-05 20:47:49 +02:00
|
|
|
# type: (bool) -> UserProfile
|
2013-03-29 17:39:53 +01:00
|
|
|
do_change_password(self.user, self.cleaned_data['new_password1'],
|
2017-03-14 06:07:14 +01:00
|
|
|
commit=commit)
|
2012-12-13 21:08:07 +01:00
|
|
|
return self.user
|
2013-05-03 00:26:53 +02:00
|
|
|
|
2016-04-28 23:07:41 +02:00
|
|
|
class ZulipPasswordResetForm(PasswordResetForm):
|
|
|
|
def get_users(self, email):
|
2016-06-05 20:47:49 +02:00
|
|
|
# type: (str) -> QuerySet
|
2016-04-28 23:07:41 +02:00
|
|
|
"""Given an email, return matching user(s) who should receive a reset.
|
|
|
|
|
|
|
|
This is modified from the original in that it allows non-bot
|
|
|
|
users who don't have a usable password to reset their
|
|
|
|
passwords.
|
|
|
|
"""
|
|
|
|
if not password_auth_enabled:
|
2016-04-28 23:13:59 +02:00
|
|
|
logging.info("Password reset attempted for %s even though password auth is disabled." % (email,))
|
2016-04-28 23:07:41 +02:00
|
|
|
return []
|
2016-04-28 23:13:59 +02:00
|
|
|
result = UserProfile.objects.filter(email__iexact=email, is_active=True,
|
|
|
|
is_bot=False)
|
|
|
|
if len(result) == 0:
|
|
|
|
logging.info("Password reset attempted for %s; no active account." % (email,))
|
|
|
|
return result
|
2016-04-28 23:07:41 +02:00
|
|
|
|
2017-04-24 12:19:54 +02:00
|
|
|
def send_mail(self, subject_template_name, email_template_name,
|
|
|
|
context, from_email, to_email, html_email_template_name=None):
|
|
|
|
# type: (str, str, Dict[str, Any], str, str, str) -> None
|
|
|
|
"""
|
|
|
|
Currently we don't support accounts in multiple subdomains using
|
2017-06-06 05:53:04 +02:00
|
|
|
a single email address. We override this function so that we do
|
2017-04-24 12:19:54 +02:00
|
|
|
not send a reset link to an email address if the reset attempt is
|
|
|
|
done on the subdomain which does not match user.realm.subdomain.
|
|
|
|
|
|
|
|
Once we start supporting accounts with the same email in
|
2017-06-06 05:53:04 +02:00
|
|
|
multiple subdomains, we may be able to refactor this function.
|
|
|
|
|
|
|
|
A second reason we override this function is so that we can send
|
|
|
|
the mail through the functions in zerver.lib.send_email, to match
|
|
|
|
how we send all other mail in the codebase.
|
2017-04-24 12:19:54 +02:00
|
|
|
"""
|
2017-07-11 05:11:08 +02:00
|
|
|
user = get_user_profile_by_email(to_email)
|
2017-04-24 12:19:54 +02:00
|
|
|
attempted_subdomain = get_subdomain(getattr(self, 'request'))
|
|
|
|
context['attempted_realm'] = False
|
2017-07-11 05:11:08 +02:00
|
|
|
if not check_subdomain(user.realm.subdomain, attempted_subdomain):
|
2017-04-24 12:19:54 +02:00
|
|
|
context['attempted_realm'] = get_realm(attempted_subdomain)
|
|
|
|
|
2017-07-11 05:11:08 +02:00
|
|
|
send_email('zerver/emails/password_reset', to_user_id=user.id,
|
2017-07-10 00:11:06 +02:00
|
|
|
from_name="Zulip Account Security",
|
|
|
|
from_address=FromAddress.NOREPLY, context=context)
|
2017-04-24 12:19:54 +02:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# type: (*Any, **Any) -> None
|
|
|
|
"""Currently we don't support accounts in multiple subdomains using
|
|
|
|
a single email addresss. We override this function so that we can
|
|
|
|
inject request parameter in context. This parameter will be used
|
|
|
|
by send_mail function.
|
|
|
|
|
|
|
|
Once we start supporting accounts with the same email in
|
|
|
|
multiple subdomains, we may be able to delete or refactor this
|
|
|
|
function.
|
|
|
|
"""
|
|
|
|
setattr(self, 'request', kwargs.get('request'))
|
|
|
|
super(ZulipPasswordResetForm, self).save(*args, **kwargs)
|
|
|
|
|
2013-12-09 22:26:10 +01:00
|
|
|
class CreateUserForm(forms.Form):
|
2013-05-03 00:26:53 +02:00
|
|
|
full_name = forms.CharField(max_length=100)
|
|
|
|
email = forms.EmailField()
|
2014-01-07 19:51:18 +01:00
|
|
|
|
|
|
|
class OurAuthenticationForm(AuthenticationForm):
|
|
|
|
def clean_username(self):
|
2016-06-05 20:47:49 +02:00
|
|
|
# type: () -> str
|
2014-01-07 19:51:18 +01:00
|
|
|
email = self.cleaned_data['username']
|
|
|
|
try:
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
2014-01-09 16:27:01 +01:00
|
|
|
return email
|
2014-01-07 19:51:18 +01:00
|
|
|
|
|
|
|
if user_profile.realm.deactivated:
|
|
|
|
error_msg = u"""Sorry for the trouble, but %s has been deactivated.
|
|
|
|
|
2015-09-30 02:58:50 +02:00
|
|
|
Please contact %s to reactivate this group.""" % (
|
|
|
|
user_profile.realm.name,
|
2017-07-02 05:27:01 +02:00
|
|
|
FromAddress.SUPPORT)
|
2014-01-07 19:51:18 +01:00
|
|
|
raise ValidationError(mark_safe(error_msg))
|
|
|
|
|
2017-06-27 13:10:16 +02:00
|
|
|
if not user_profile.is_active and not user_profile.is_mirror_dummy:
|
2017-06-19 13:36:48 +02:00
|
|
|
error_msg = (u"Sorry for the trouble, but your account has been "
|
|
|
|
u"deactivated. Please contact %s to reactivate "
|
2017-07-02 05:27:01 +02:00
|
|
|
u"it.") % (FromAddress.SUPPORT,)
|
2017-06-19 13:36:48 +02:00
|
|
|
raise ValidationError(mark_safe(error_msg))
|
|
|
|
|
2016-07-19 14:35:08 +02:00
|
|
|
if not check_subdomain(get_subdomain(self.request), user_profile.realm.subdomain):
|
|
|
|
logging.warning("User %s attempted to password login to wrong subdomain %s" %
|
|
|
|
(user_profile.email, get_subdomain(self.request)))
|
|
|
|
raise ValidationError(mark_safe(WRONG_SUBDOMAIN_ERROR))
|
2014-01-07 19:51:18 +01:00
|
|
|
return email
|
2016-12-20 10:41:46 +01:00
|
|
|
|
|
|
|
class MultiEmailField(forms.Field):
|
|
|
|
def to_python(self, emails):
|
|
|
|
# type: (Text) -> List[Text]
|
|
|
|
"""Normalize data to a list of strings."""
|
|
|
|
if not emails:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return [email.strip() for email in emails.split(',')]
|
|
|
|
|
|
|
|
def validate(self, emails):
|
|
|
|
# type: (List[Text]) -> None
|
|
|
|
"""Check if value consists only of valid emails."""
|
|
|
|
super(MultiEmailField, self).validate(emails)
|
|
|
|
for email in emails:
|
|
|
|
validate_email(email)
|
|
|
|
|
|
|
|
class FindMyTeamForm(forms.Form):
|
|
|
|
emails = MultiEmailField(
|
2017-03-29 07:52:51 +02:00
|
|
|
help_text=_("Add up to 10 comma-separated email addresses."))
|
2016-12-20 10:41:46 +01:00
|
|
|
|
|
|
|
def clean_emails(self):
|
|
|
|
# type: () -> List[Text]
|
|
|
|
emails = self.cleaned_data['emails']
|
|
|
|
if len(emails) > 10:
|
2017-03-29 07:52:51 +02:00
|
|
|
raise forms.ValidationError(_("Please enter at most 10 emails."))
|
2016-12-20 10:41:46 +01:00
|
|
|
|
|
|
|
return emails
|