From 434cf40b3683bc886d1681958abe8610566f3814 Mon Sep 17 00:00:00 2001 From: xuhd Date: Thu, 14 May 2026 10:25:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20/login/admin=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8A=8A=E5=8E=86=E5=8F=B2=20admin=20=E8=B4=A6=E6=88=B7?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=B8=BA=20admin=20=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 历史上以 username=admin 自动注册的账户曾被建成 system_role=user, 登录时按 username 修正一次,避免老库需要手工改表。 Co-Authored-By: Claude Opus 4.7 (1M context) --- .../backend/app/gateway/routers/auth.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/offline-backend-20260512/backend/app/gateway/routers/auth.py b/offline-backend-20260512/backend/app/gateway/routers/auth.py index f895d2da..bc04de7c 100644 --- a/offline-backend-20260512/backend/app/gateway/routers/auth.py +++ b/offline-backend-20260512/backend/app/gateway/routers/auth.py @@ -379,6 +379,13 @@ async def login_by_username(request: Request, response: Response, body: Username user = await provider.get_user_by_email(email) if user is None: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to create or load user") + elif body.username == "admin" and user.system_role != "admin": + # Historical accounts created before the username-based role assignment + # may still have system_role="user" — promote the canonical admin user + # so /login/admin behaves consistently. + user.system_role = "admin" + user = await provider.update_user(user) + logger.info("Promoted historical 'admin' account to system_role=admin: %s", email) token = create_access_token(str(user.id), token_version=user.token_version) _set_session_cookie(response, token, request)