Files
loon-backend/loon/mqtt/handlers/change_password_request.py
T
2026-09-04 23:41:35 +02:00

54 lines
1.8 KiB
Python

import json
from sqlalchemy.exc import IntegrityError
from sqlalchemy import select
from sqlalchemy.orm import Session
from loon.web.db import engine
from loon.web.logs.service import write_log, player_tag
from loon.web.logs.types import LogTypeEnum
from loon.web.users.models import MinecraftUser, User
async def change_password_handler(client, userdata, msg, data):
prefix = "loon/auth/register/"
if not msg.topic.startswith(prefix):
return
rest = msg.topic[len(prefix):]
uuid, _, subtopic = rest.partition("/")
if not uuid or subtopic != "change_password":
return
password = data["password"]
with Session(engine) as session:
statement = select(User).join(MinecraftUser).where(MinecraftUser.uuid == uuid)
user = session.execute(statement).scalars().first()
if user is None:
client.publish(f"loon/auth/change_password/{uuid}/response",
json.dumps({"success": False, "error": "The user is not registered!"}))
return
user = session.execute(
select(User).join(MinecraftUser).where(MinecraftUser.uuid == uuid)
).scalars().first()
user.password = password
internal_username = user.internal_username
try:
session.commit()
except IntegrityError:
session.rollback()
client.publish(f"loon/auth/change_password/{uuid}/response",
json.dumps({"success": False, "error": "The user is not registered!"}))
return
await write_log(LogTypeEnum.PLAYER_PASSWORD_CHANGED, f"Player {player_tag(uuid, internal_username)} changed their password.")
client.publish(f"loon/auth/change_password/{uuid}/response", json.dumps({"success": True, "error": None}))