Skip to content

Commit

Permalink
Address CRs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher committed Oct 22, 2023
1 parent b2d07ad commit 08e3d9f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dmoj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
DMOJ_EMAIL_CHANGE_LIMIT_WINDOW = 3600
DMOJ_EMAIL_CHANGE_LIMIT_COUNT = 10
# Number of minutes before an email change request activation key expires
DMOJ_EMAIL_CHANGE_EXPIRY_MINUTES = 10
DMOJ_EMAIL_CHANGE_EXPIRY_MINUTES = 60

# At the bare minimum, dark and light theme CSS file locations must be declared
DMOJ_THEME_CSS = {
Expand Down
6 changes: 2 additions & 4 deletions judge/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django_ace import AceWidget
from judge.models import Contest, Language, Organization, Problem, ProblemPointsVote, Profile, Submission, \
WebAuthnCredential
from judge.utils.mail import is_email_address_bad
from judge.utils.mail import validate_email_domain
from judge.utils.subscription import newsletter_id
from judge.widgets import HeavyPreviewPageDownWidget, Select2MultipleWidget, Select2Widget

Expand Down Expand Up @@ -108,9 +108,7 @@ def __init__(self, *args, user, **kwargs):
def clean_email(self):
if User.objects.filter(email=self.cleaned_data['email']).exists():
raise ValidationError(_('This email address is already taken.'))
if is_email_address_bad(self.cleaned_data['email']):
raise ValidationError(_('Your email provider is not allowed due to history of abuse. '
'Please use a reputable email provider.'))
validate_email_domain(self.cleaned_data['email'])
return self.cleaned_data['email']

def clean_password(self):
Expand Down
9 changes: 6 additions & 3 deletions judge/utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.forms import ValidationError
from django.template import loader
from django.utils.translation import gettext


bad_mail_regex = list(map(re.compile, settings.BAD_MAIL_PROVIDER_REGEX))


def is_email_address_bad(email):
def validate_email_domain(email):
if '@' in email:
domain = email.split('@')[-1].lower()
return domain in settings.BAD_MAIL_PROVIDERS or any(regex.match(domain) for regex in bad_mail_regex)
return False
if domain in settings.BAD_MAIL_PROVIDERS or any(regex.match(domain) for regex in bad_mail_regex):
raise ValidationError(gettext('Your email provider is not allowed due to history of abuse. '
'Please use a reputable email provider.'))


def send_mail(
Expand Down
6 changes: 2 additions & 4 deletions judge/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sortedm2m.forms import SortedMultipleChoiceField

from judge.models import Language, Organization, Profile, TIMEZONE
from judge.utils.mail import is_email_address_bad
from judge.utils.mail import validate_email_domain
from judge.utils.recaptcha import ReCaptchaField, ReCaptchaWidget
from judge.utils.subscription import Subscription, newsletter_id
from judge.widgets import Select2MultipleWidget, Select2Widget
Expand Down Expand Up @@ -40,9 +40,7 @@ def clean_email(self):
if User.objects.filter(email=self.cleaned_data['email']).exists():
raise forms.ValidationError(gettext('The email address "%s" is already taken. Only one registration '
'is allowed per address.') % self.cleaned_data['email'])
if is_email_address_bad(self.cleaned_data['email']):
raise forms.ValidationError(gettext('Your email provider is not allowed due to history of abuse. '
'Please use a reputable email provider.'))
validate_email_domain(self.cleaned_data['email'])
return self.cleaned_data['email']

def clean_organizations(self):
Expand Down
11 changes: 6 additions & 5 deletions judge/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,14 @@ def form_valid(self, form):
'activation_key': activation_key,
'new_email': new_email,
}
# When from_email is none, we use the DEFAULT_FROM_EMAIL setting
send_mail(
self.notify_subject_template_name, self.notify_email_template_name, context, None, self.request.user.email,
self.notify_html_email_template_name,
self.notify_subject_template_name, self.notify_email_template_name, context, from_email=None,
to_email=self.request.user.email, html_email_template_name=self.notify_html_email_template_name,
)
send_mail(
self.activate_subject_template_name, self.activate_email_template_name, context, None, new_email,
self.activate_html_email_template_name,
self.activate_subject_template_name, self.activate_email_template_name, context, from_email=None,
to_email=new_email, html_email_template_name=self.activate_html_email_template_name,
)

return generic_message(
Expand Down Expand Up @@ -587,7 +588,7 @@ def get(self, request, *args, **kwargs):
except (binascii.Error, signing.BadSignature):
raise ValueError(_('Invalid activation key. Please try again.'))
except signing.SignatureExpired:
raise ValueError(_('This request is expired. Please try again.'))
raise ValueError(_('This request has expired. Please try again.'))
if data['id'] != request.user.id:
raise ValueError(
_('Please try again from the account this email change was originally requested from.'),
Expand Down
4 changes: 2 additions & 2 deletions templates/registration/email_change_notify_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<b>
{% if site_admin_email %}
{% with link='<a href="mailto:%(email)s">%(email)s</a>'|safe|format(email=site_admin_email) %}
{{ _('If this was not you, please email us immediately at %(email)s.', email=link) }}
{{ _('If this was not you, please change your password and email us immediately at %(email)s.', email=link) }}
{% endwith %}
{% else %}
{{ _('If this was not you, please reply to this email immediately.') }}
{{ _('If this was not you, please change your password and reply to this email immediately.') }}
{% endif %}
</b>
</div></div>
Expand Down
4 changes: 2 additions & 2 deletions templates/registration/email_change_notify_email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{ _('If this was you, no further action is required.') }}
{% if site_admin_email %}
{{ _('If this was not you, please email us immediately at %(email)s.', email=site_admin_email) }}
{{ _('If this was not you, please change your password and email us immediately at %(email)s.', email=site_admin_email) }}
{% else %}
{{ _('If this was not you, please reply to this email immediately.') }}
{{ _('If this was not you, please change your password and reply to this email immediately.') }}
{% endif %}

0 comments on commit 08e3d9f

Please sign in to comment.