diff --git a/app/main/forms.py b/app/main/forms.py index 640e993de..80d651822 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -12,9 +12,9 @@ from wtforms import ( HiddenField ) from wtforms.fields.html5 import EmailField, TelField -from wtforms.validators import DataRequired, Email, Length, Regexp +from wtforms.validators import (DataRequired, Email, Length, Regexp) -from app.main.validators import Blacklist, CsvFileValidator +from app.main.validators import (Blacklist, CsvFileValidator, ValidEmailDomainRegex) from utils.recipients import ( validate_phone_number, @@ -24,13 +24,11 @@ from utils.recipients import ( def email_address(label='Email address'): - gov_uk_email \ - = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.gov.uk)" return EmailField(label, validators=[ Length(min=5, max=255), DataRequired(message='Email cannot be empty'), Email(message='Enter a valid email address'), - Regexp(regex=gov_uk_email, message='Enter a gov.uk email address')]) + ValidEmailDomainRegex()]) class UKMobileNumber(TelField): diff --git a/app/main/validators.py b/app/main/validators.py index 3149a7729..b33e9f162 100644 --- a/app/main/validators.py +++ b/app/main/validators.py @@ -1,3 +1,4 @@ +import re from wtforms import ValidationError from datetime import datetime from app.main.encryption import check_hash @@ -22,3 +23,18 @@ class CsvFileValidator(object): def __call__(self, form, field): if not form.file.data.mimetype == 'text/csv': raise ValidationError(self.message) + + +class ValidEmailDomainRegex(object): + + def __call__(self, form, field): + from flask import (current_app, url_for) + message = ( + 'Enter a central government email address.' + ' If you think you should have access' + ' contact us').format( + "https://docs.google.com/forms/d/1AL8U-xJX_HAFEiQiJszGQw0PcEaEUnYATSntEghNDGo/viewform") + valid_domains = current_app.config.get('EMAIL_DOMAIN_REGEXES', []) + email_regex = "(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*.({}))".format("|".join(valid_domains)) + if not re.match(email_regex, field.data): + raise ValidationError(message) diff --git a/app/templates/components/textbox.html b/app/templates/components/textbox.html index 78241a630..f4c0f368f 100644 --- a/app/templates/components/textbox.html +++ b/app/templates/components/textbox.html @@ -7,7 +7,8 @@ help_link_text=None, width='2-3', suffix=None, - disabled=False + disabled=False, + safe_error_message=False ) %}
Check your email address is correct and then resend the confirmation code.
If you have forgotten your password, we can send you an email to create a new password.
diff --git a/app/templates/views/invite-user.html b/app/templates/views/invite-user.html index b50b9d663..37961b8fa 100644 --- a/app/templates/views/invite-user.html +++ b/app/templates/views/invite-user.html @@ -16,7 +16,7 @@ Manage users – GOV.UK Notify