feat: Refines user registration approval process

Consolidates email notification and user unlocking logic
Validates Synapse server connection during startup
Adds configuration checks for admin email

Enhances error handling to notify admin if user unlocking fails
This commit is contained in:
Kumi 2024-11-16 18:38:56 +01:00
parent bcc73e58ba
commit 6328e718ce
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 33 additions and 24 deletions

View file

@ -14,36 +14,25 @@ def handle_status_change(sender, instance, created, **kwargs):
status = instance.status
if status == UserRegistration.STATUS_APPROVED:
send_mail(
"Registration Approved",
f"Congratulations, {instance.username}! Your registration at {settings.MATRIX_DOMAIN} has been approved.",
settings.DEFAULT_FROM_EMAIL,
[instance.email],
)
requests.put(
response = requests.put(
f"{settings.SYNAPSE_SERVER}/_synapse/admin/v2/users/@{instance.username}:{settings.MATRIX_DOMAIN}",
json={"locked": False},
headers={"Authorization": f"Bearer {settings.SYNAPSE_ADMIN_TOKEN}"},
)
response = requests.post(
f"{settings.SYNAPSE_SERVER}/_synapse/admin/v2/users/{settings.ADMIN_USER}/rooms?access_token={settings.SYNAPSE_ADMIN_TOKEN}",
json={"preset": "private_chat"},
)
if response.status_code != 200:
send_mail(
"Unlocking Failed",
f"Failed to unlock the user {instance.username}. Please unlock the user manually if required.",
settings.DEFAULT_FROM_EMAIL,
[settings.ADMIN_EMAIL],
)
room_id = response.json()["room_id"]
response = requests.post(
f"{settings.SYNAPSE_SERVER}/_synapse/admin/v2/rooms/{room_id}/invite",
json={"user_id": f"@{instance.username}:{settings.MATRIX_DOMAIN}"},
headers={"Authorization": f"Bearer {settings.SYNAPSE_ADMIN_TOKEN}"},
)
response = requests.post(
f"{settings.SYNAPSE_SERVER}/_synapse/admin/v2/rooms/{room_id}/send",
json={"msgtype": "m.text", "body": f"Welcome, {instance.username}!"},
headers={"Authorization": f"Bearer {settings.SYNAPSE_ADMIN_TOKEN}"},
send_mail(
"Registration Approved",
f"Congratulations, {instance.username}! Your registration at {settings.MATRIX_DOMAIN} has been approved.",
settings.DEFAULT_FROM_EMAIL,
[instance.email],
)
elif status == UserRegistration.STATUS_DENIED:

View file

@ -17,6 +17,7 @@ from django.core.management.utils import get_random_secret_key
import os
import yaml
import requests
CONFIG_PATH = os.environ.get("CONFIG_PATH", "config.yaml")
@ -66,6 +67,16 @@ SYNAPSE_SERVER = config["synapse"]["server"]
SYNAPSE_ADMIN_TOKEN = config["synapse"]["admin_token"]
MATRIX_DOMAIN = config["synapse"]["domain"]
response = requests.get(
f"{SYNAPSE_SERVER}/_matrix/client/r0/account/whoami",
headers={"Authorization": f"Bearer {SYNAPSE_ADMIN_TOKEN}"},
)
if response.status_code != 200:
raise ConnectionError("Failed to connect to the Synapse server.")
SYNAPSE_USER = response.json()["user_id"]
# Application definition
@ -172,6 +183,14 @@ if not all(key in config["email"] for key in ["host", "port", "username", "passw
"Please specify the email host, port, username, and password in the configuration file."
)
if "admin" not in config:
raise KeyError("Please specify an admin configuration in the configuration file.")
if "email" not in config["admin"]:
raise KeyError(
"Please specify an email address for the admin in the configuration file."
)
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = config["email"]["host"]
EMAIL_PORT = config["email"]["port"]
@ -181,3 +200,4 @@ EMAIL_HOST_USER = config["email"]["username"]
EMAIL_HOST_PASSWORD = config["email"]["password"]
EMAIL_SUBJECT_PREFIX = config["email"].get("subject_prefix", "")
DEFAULT_FROM_EMAIL = config["email"].get("from", EMAIL_HOST_USER)
ADMIN_EMAIL = config["admin"]["email"]