mailu: backfill mailu_enabled for legacy users

This commit is contained in:
Brad Stein 2026-01-18 02:03:13 -03:00
parent c9cb088198
commit 0d27107411
2 changed files with 19 additions and 9 deletions

View File

@ -160,7 +160,13 @@ def test_main_generates_password_and_upserts(monkeypatch):
"email": "user2@example.com", "email": "user2@example.com",
"attributes": {"mailu_app_password": ["keepme"], "mailu_enabled": ["true"]}, "attributes": {"mailu_app_password": ["keepme"], "mailu_enabled": ["true"]},
}, },
{"id": "u3", "username": "user3", "email": "user3@other.com", "attributes": {}}, {
"id": "u3",
"username": "user3",
"email": "user3@example.com",
"attributes": {"mailu_email": ["user3@example.com"]},
},
{"id": "u4", "username": "user4", "email": "user4@other.com", "attributes": {}},
] ]
updated = [] updated = []
@ -199,6 +205,6 @@ def test_main_generates_password_and_upserts(monkeypatch):
sync.main() sync.main()
# Only mail-enabled users are synced and backfilled. # Only mail-enabled users (or legacy users with a mailbox) are synced and backfilled.
assert len(updated) == 2 assert len(updated) == 3
assert conns and len(conns[0]._cursor.executions) == 2 assert conns and len(conns[0]._cursor.executions) == 3

View File

@ -26,6 +26,7 @@ KC_CLIENT_SECRET = os.environ["KEYCLOAK_CLIENT_SECRET"]
MAILU_DOMAIN = os.environ["MAILU_DOMAIN"] MAILU_DOMAIN = os.environ["MAILU_DOMAIN"]
MAILU_DEFAULT_QUOTA = int(os.environ.get("MAILU_DEFAULT_QUOTA", "20000000000")) MAILU_DEFAULT_QUOTA = int(os.environ.get("MAILU_DEFAULT_QUOTA", "20000000000"))
MAILU_ENABLED_ATTR = os.environ.get("MAILU_ENABLED_ATTR", "mailu_enabled") MAILU_ENABLED_ATTR = os.environ.get("MAILU_ENABLED_ATTR", "mailu_enabled")
MAILU_EMAIL_ATTR = "mailu_email"
DB_CONFIG = { DB_CONFIG = {
"host": os.environ["MAILU_DB_HOST"], "host": os.environ["MAILU_DB_HOST"],
@ -153,12 +154,12 @@ def get_attribute_value(attributes, key):
def mailu_enabled(attributes) -> bool: def mailu_enabled(attributes) -> bool:
raw = get_attribute_value(attributes, MAILU_ENABLED_ATTR) raw = get_attribute_value(attributes, MAILU_ENABLED_ATTR)
if raw is None: if raw is None:
return False return bool(get_attribute_value(attributes, MAILU_EMAIL_ATTR))
return str(raw).strip().lower() in {"1", "true", "yes", "y", "on"} return str(raw).strip().lower() in {"1", "true", "yes", "y", "on"}
def resolve_mailu_email(user, attributes): def resolve_mailu_email(user, attributes):
explicit = get_attribute_value(attributes, "mailu_email") explicit = get_attribute_value(attributes, MAILU_EMAIL_ATTR)
if explicit: if explicit:
return explicit return explicit
@ -227,14 +228,17 @@ def main():
attrs = user.get("attributes", {}) or {} attrs = user.get("attributes", {}) or {}
if user.get("enabled") is False: if user.get("enabled") is False:
continue continue
needs_update = False
if get_attribute_value(attrs, MAILU_ENABLED_ATTR) is None and get_attribute_value(attrs, MAILU_EMAIL_ATTR):
attrs[MAILU_ENABLED_ATTR] = ["true"]
needs_update = True
if not mailu_enabled(attrs): if not mailu_enabled(attrs):
continue continue
app_pw = get_attribute_value(attrs, "mailu_app_password") app_pw = get_attribute_value(attrs, "mailu_app_password")
mailu_email = resolve_mailu_email(user, attrs) mailu_email = resolve_mailu_email(user, attrs)
needs_update = False if not get_attribute_value(attrs, MAILU_EMAIL_ATTR):
if not get_attribute_value(attrs, "mailu_email"): attrs[MAILU_EMAIL_ATTR] = [mailu_email]
attrs["mailu_email"] = [mailu_email]
needs_update = True needs_update = True
if not app_pw: if not app_pw: