fix: /login/admin 自动把历史 admin 账户升级为 admin 角色

历史上以 username=admin 自动注册的账户曾被建成 system_role=user,
登录时按 username 修正一次,避免老库需要手工改表。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
xuhdandClaude Opus 4.7 committed 2026-05-14 10:25:24 +08:00
1 parent 43044232bb
commit 434cf40b36
1 file changed
+7
@@ -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)