use AutoCreateRule instead of custom_domain.auto_create_regex when creating new alias

This commit is contained in:
Son Nguyen Kim 2021-09-20 18:29:36 +02:00
parent 56c72d5fba
commit e5a8ce1492
1 changed files with 20 additions and 14 deletions

View File

@ -27,6 +27,7 @@ from app.models import (
Mailbox, Mailbox,
EmailLog, EmailLog,
Contact, Contact,
AutoCreateRule,
) )
@ -132,22 +133,28 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
if not custom_domain: if not custom_domain:
return None return None
if not custom_domain.catch_all and not custom_domain.auto_create_regex: if not custom_domain.catch_all and len(custom_domain.auto_create_rules) == 0:
return None return None
elif not custom_domain.catch_all and len(custom_domain.auto_create_rules) > 0:
if custom_domain.auto_create_regex and not custom_domain.catch_all:
local = get_email_local_part(address) local = get_email_local_part(address)
regex = re.compile(custom_domain.auto_create_regex) for rule in custom_domain.auto_create_rules:
if not re.fullmatch(regex, local): rule: AutoCreateRule
LOG.d( regex = re.compile(rule.regex)
"%s can't be auto created on %s as it fails regex %s", if re.fullmatch(regex, local):
address, LOG.d(
custom_domain, "%s passes %s on %s",
custom_domain.auto_create_regex, address,
) rule.regex,
return None custom_domain,
)
mailboxes = rule.mailboxes
break
else: # no rule passes
LOG.d("no rule passed to create %s", local)
return
else: # catch-all is enabled
mailboxes = custom_domain.mailboxes
# custom_domain has catch-all enabled or the address passes the regex
domain_user: User = custom_domain.user domain_user: User = custom_domain.user
if not domain_user.can_create_new_alias(): if not domain_user.can_create_new_alias():
@ -156,7 +163,6 @@ def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
try: try:
LOG.d("create alias %s for domain %s", address, custom_domain) LOG.d("create alias %s for domain %s", address, custom_domain)
mailboxes = custom_domain.mailboxes
alias = Alias.create( alias = Alias.create(
email=address, email=address,
user_id=custom_domain.user_id, user_id=custom_domain.user_id,