72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
"""A workspace's own outgoing mail configuration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
DateTime,
|
|
ForeignKey,
|
|
Integer,
|
|
String,
|
|
Text,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from app.config.database import Base
|
|
|
|
|
|
class TenantEmailSettings(Base):
|
|
__tablename__ = "tenant_email_settings"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True,
|
|
server_default=func.gen_random_uuid())
|
|
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"),
|
|
nullable=False)
|
|
smtp_host = Column(String(255), nullable=False)
|
|
smtp_port = Column(Integer, nullable=False, default=587)
|
|
smtp_user = Column(String(255))
|
|
smtp_password_enc = Column(Text)
|
|
use_ssl = Column(Boolean, nullable=False, default=False)
|
|
from_address = Column(String(255), nullable=False)
|
|
from_name = Column(String(150))
|
|
is_active = Column(Boolean, nullable=False, default=False)
|
|
last_verified_at = Column(DateTime(timezone=True))
|
|
last_error = Column(String(500))
|
|
created_at = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), nullable=False,
|
|
server_default=func.now(), onupdate=func.now())
|
|
|
|
@property
|
|
def smtp_password(self) -> str | None:
|
|
"""Encrypted, not hashed — unlike every other credential here.
|
|
|
|
The platform has to *present* this one to somebody else's server, so
|
|
there is no version of this where a hash would do. Fernet, like the
|
|
module trust credentials and the webhook signing secrets.
|
|
"""
|
|
from app.core.crypto import decrypt_json, is_encrypted
|
|
|
|
if not self.smtp_password_enc:
|
|
return None
|
|
if is_encrypted(self.smtp_password_enc):
|
|
return (decrypt_json(self.smtp_password_enc) or {}).get("password")
|
|
return None
|
|
|
|
@smtp_password.setter
|
|
def smtp_password(self, value: str | None) -> None:
|
|
from app.core.crypto import encrypt_json
|
|
|
|
self.smtp_password_enc = (
|
|
encrypt_json({"password": value}) if value else None
|
|
)
|
|
|
|
@property
|
|
def sender(self) -> str:
|
|
"""`Name <address>` when a name is set, which is what a mail client
|
|
shows. Without it every message reads as coming from an address."""
|
|
if self.from_name:
|
|
return f"{self.from_name} <{self.from_address}>"
|
|
return self.from_address
|