2024-07-23 16:17:23 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from app.db import Session
|
|
|
|
from app.log import LOG
|
2024-07-30 18:00:24 +02:00
|
|
|
from app.models import User, SLDomain, CustomDomain, Mailbox
|
2024-10-16 16:57:59 +02:00
|
|
|
from app.user_audit_log_utils import emit_user_audit_log, UserAuditLogAction
|
2024-07-23 16:17:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CannotSetAlias(Exception):
|
|
|
|
def __init__(self, msg: str):
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
|
2024-07-30 18:00:24 +02:00
|
|
|
class CannotSetMailbox(Exception):
|
|
|
|
def __init__(self, msg: str):
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
|
2024-07-23 17:58:52 +02:00
|
|
|
def set_default_alias_domain(user: User, domain_name: Optional[str]):
|
2024-08-27 23:26:12 +02:00
|
|
|
if not domain_name:
|
2024-07-23 16:17:23 +02:00
|
|
|
LOG.i(f"User {user} has set no domain as default domain")
|
|
|
|
user.default_alias_public_domain_id = None
|
|
|
|
user.default_alias_custom_domain_id = None
|
|
|
|
Session.flush()
|
|
|
|
return
|
2024-08-27 23:26:12 +02:00
|
|
|
|
2024-07-23 16:17:23 +02:00
|
|
|
sl_domain: SLDomain = SLDomain.get_by(domain=domain_name)
|
|
|
|
if sl_domain:
|
|
|
|
if sl_domain.hidden:
|
|
|
|
LOG.i(f"User {user} has tried to set up a hidden domain as default domain")
|
|
|
|
raise CannotSetAlias("Domain does not exist")
|
|
|
|
if sl_domain.premium_only and not user.is_premium():
|
|
|
|
LOG.i(f"User {user} has tried to set up a premium domain as default domain")
|
|
|
|
raise CannotSetAlias("You cannot use this domain")
|
|
|
|
LOG.i(f"User {user} has set public {sl_domain} as default domain")
|
|
|
|
user.default_alias_public_domain_id = sl_domain.id
|
|
|
|
user.default_alias_custom_domain_id = None
|
|
|
|
Session.flush()
|
|
|
|
return
|
|
|
|
custom_domain = CustomDomain.get_by(domain=domain_name)
|
|
|
|
if not custom_domain:
|
|
|
|
LOG.i(
|
|
|
|
f"User {user} has tried to set up an non existing domain as default domain"
|
|
|
|
)
|
|
|
|
raise CannotSetAlias("Domain does not exist or it hasn't been verified")
|
|
|
|
if custom_domain.user_id != user.id or not custom_domain.verified:
|
|
|
|
LOG.i(
|
|
|
|
f"User {user} has tried to set domain {custom_domain} as default domain that does not belong to the user or that is not verified"
|
|
|
|
)
|
|
|
|
raise CannotSetAlias("Domain does not exist or it hasn't been verified")
|
|
|
|
LOG.i(f"User {user} has set custom {custom_domain} as default domain")
|
|
|
|
user.default_alias_public_domain_id = None
|
|
|
|
user.default_alias_custom_domain_id = custom_domain.id
|
|
|
|
Session.flush()
|
2024-07-30 18:00:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_default_mailbox(user: User, mailbox_id: int) -> Mailbox:
|
2024-10-16 16:57:59 +02:00
|
|
|
mailbox: Optional[Mailbox] = Mailbox.get(mailbox_id)
|
2024-07-30 18:00:24 +02:00
|
|
|
|
|
|
|
if not mailbox or mailbox.user_id != user.id:
|
|
|
|
raise CannotSetMailbox("Invalid mailbox")
|
|
|
|
|
|
|
|
if not mailbox.verified:
|
|
|
|
raise CannotSetMailbox("This is mailbox is not verified")
|
|
|
|
|
|
|
|
if mailbox.id == user.default_mailbox_id:
|
|
|
|
return mailbox
|
|
|
|
LOG.i(f"User {user} has set mailbox {mailbox} as his default one")
|
|
|
|
|
|
|
|
user.default_mailbox_id = mailbox.id
|
2024-10-16 16:57:59 +02:00
|
|
|
emit_user_audit_log(
|
|
|
|
user=user,
|
|
|
|
action=UserAuditLogAction.UpdateMailbox,
|
|
|
|
message=f"Set mailbox {mailbox.id} ({mailbox.email}) as default",
|
|
|
|
)
|
|
|
|
|
2024-07-30 18:00:24 +02:00
|
|
|
Session.commit()
|
|
|
|
return mailbox
|