Files
ds-chat/backend/app/services/smtp_settings_service.py
T
ksmith b724f8a33b Add admin-invited signups and email notifications (Gitea issue #15)
Site admins can invite a brand-new person by email from the Admin portal
Users tab -- a signup-link email lets them set their own username/password
and lands them in the app already logged in. Existing users invited to a
room now also get an email. Closes the "invited but never notified" gap
from both directions.

SMTP is configured through the Admin Settings tab at runtime (not the env
file), persisted in a new smtp_settings table with the password encrypted
at rest via a Fernet key derived from SESSION_SECRET -- the first
reversible secret this app stores in the database. A "send test email"
button surfaces real delivery errors; the invite/notification paths
themselves never fail loudly, since an SMTP outage shouldn't block an
action that already succeeded in the database.

New site_invites table mirrors RoomInvite's shape but targets an email
address with no room context; the raw signup token is hashed the same way
API tokens are, and only ever exists in the email link. POST /api/signup
is the first genuinely public, unauthenticated account-creation endpoint
in this app, reusing the existing register_user path for identical
validation.
2026-08-14 17:38:56 -06:00

49 lines
1.4 KiB
Python

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.crypto import encrypt
from app.models import SmtpSettings
async def get_smtp_settings(db: AsyncSession) -> SmtpSettings | None:
result = await db.execute(select(SmtpSettings).limit(1))
return result.scalar_one_or_none()
async def upsert_smtp_settings(
db: AsyncSession,
*,
host: str,
port: int,
username: str | None,
password: str | None,
from_address: str,
use_tls: bool,
) -> SmtpSettings:
settings_row = await get_smtp_settings(db)
if settings_row is None:
settings_row = SmtpSettings(
host=host,
port=port,
username=username,
from_address=from_address,
use_tls=use_tls,
)
db.add(settings_row)
else:
settings_row.host = host
settings_row.port = port
settings_row.username = username
settings_row.from_address = from_address
settings_row.use_tls = use_tls
# A blank password in the request means "keep the current one" -- the
# frontend never has the plaintext to send back, only whether one is
# already set (SmtpSettingsRead.has_password).
if password:
settings_row.password_encrypted = encrypt(password)
await db.commit()
await db.refresh(settings_row)
return settings_row