Make sure ALLOW_SPECIAL_EMAIL_DOMAINS should also work for pgAdmin docker apps. #6222

This commit is contained in:
Aditya Toshniwal 2024-09-19 14:03:43 +05:30
parent 53cdc80df2
commit 8fd5efaa84
3 changed files with 34 additions and 18 deletions

View File

@ -54,8 +54,14 @@ if [ ! -f /var/lib/pgadmin/pgadmin4.db ] && [ "${external_config_db_exists}" = "
if [ -n "${PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY}" ]; then
CHECK_EMAIL_DELIVERABILITY=${PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY}
fi
is_valid_email=$(cd /pgadmin4/pgadmin/utils && /venv/bin/python3 -c "from validation_utils import validate_email; val = validate_email('${PGADMIN_DEFAULT_EMAIL}', ${CHECK_EMAIL_DELIVERABILITY}); print(val)")
ALLOW_SPECIAL_EMAIL_DOMAINS="[]"
if [ -n "${PGADMIN_CONFIG_ALLOW_SPECIAL_EMAIL_DOMAINS}" ]; then
ALLOW_SPECIAL_EMAIL_DOMAINS=${PGADMIN_CONFIG_ALLOW_SPECIAL_EMAIL_DOMAINS}
fi
email_config="{'CHECK_EMAIL_DELIVERABILITY': ${CHECK_EMAIL_DELIVERABILITY}, 'ALLOW_SPECIAL_EMAIL_DOMAINS': ${ALLOW_SPECIAL_EMAIL_DOMAINS}}"
# email_config="{'CHECK_EMAIL_DELIVERABILITY': ${CHECK_EMAIL_DELIVERABILITY}}"
echo "email config is ${email_config}"
is_valid_email=$(cd /pgadmin4/pgadmin/utils && /venv/bin/python3 -c "from validation_utils import validate_email; val = validate_email('${PGADMIN_DEFAULT_EMAIL}', ${email_config}); print(val)")
if echo "${is_valid_email}" | grep "False" > /dev/null; then
echo "'${PGADMIN_DEFAULT_EMAIL}' does not appear to be a valid email address. Please reset the PGADMIN_DEFAULT_EMAIL environment variable and try again."
exit 1

View File

@ -137,13 +137,4 @@ def evaluate_and_patch_config(config: dict) -> dict:
'SESSION_COOKIE_PATH': os.environ["SCRIPT_NAME"],
}))
# Allow special email domains
try:
email_validator.SPECIAL_USE_DOMAIN_NAMES = [
d for d in email_validator.SPECIAL_USE_DOMAIN_NAMES
if d not in config.get('ALLOW_SPECIAL_EMAIL_DOMAINS', [])
]
except Exception:
pass
return config

View File

@ -7,19 +7,38 @@
#
##########################################################################
import email_validator
from email_validator import validate_email as email_validate, \
EmailNotValidError
def validate_email(email, check_email_deliverability=None):
def validate_email(email, email_config=None):
try:
if check_email_deliverability is None:
if email_config is None:
email_config = {}
import config
check_email_deliverability = config.CHECK_EMAIL_DELIVERABILITY
email_config['CHECK_EMAIL_DELIVERABILITY'] = \
config.CHECK_EMAIL_DELIVERABILITY
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \
config.ALLOW_SPECIAL_EMAIL_DOMAINS
# Allow special email domains
if isinstance(email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'], str):
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'] = \
email_config['ALLOW_SPECIAL_EMAIL_DOMAINS'].split(',')
try:
email_validator.SPECIAL_USE_DOMAIN_NAMES = [
d for d in email_validator.SPECIAL_USE_DOMAIN_NAMES
if d not in email_config['ALLOW_SPECIAL_EMAIL_DOMAINS']
]
except Exception:
pass
# Validate.
_ = email_validate(
email, check_deliverability=check_email_deliverability)
email,
check_deliverability=email_config['CHECK_EMAIL_DELIVERABILITY'])
# Update with the normalized form.
return True