fix: 管理员账号先按本地密码判断
This commit is contained in:
@@ -93,6 +93,36 @@ def auth_mode(session: Session = Depends(get_session)) -> dict:
|
||||
return {"mode": _setting(session).login_mode}
|
||||
|
||||
|
||||
def _matched_local_user(session: Session, username: str) -> User | None:
|
||||
user = session.scalar(select(User).where(User.username == username))
|
||||
if user is None:
|
||||
return None
|
||||
if _setting(session).login_mode == "local" or user.role == "admin":
|
||||
return user
|
||||
return None
|
||||
|
||||
|
||||
@router.post("/auth/login")
|
||||
async def login(body: dict) -> dict:
|
||||
username = str(body.get("username") or "").strip()
|
||||
password = str(body.get("password") or "")
|
||||
if not username or not password:
|
||||
raise _bad("请填写账号和密码")
|
||||
with session_scope() as session:
|
||||
user = _matched_local_user(session, username)
|
||||
if user is not None:
|
||||
if not verify_password(password, user.password_hash):
|
||||
raise _bad("登录失败")
|
||||
return _token_for("local", str(user.id), user.role, user.username)
|
||||
mode = _setting(session).login_mode
|
||||
if mode != "nqi":
|
||||
raise _bad("账号或密码不正确")
|
||||
started = await nqi_start({"username": username, "purpose": "login"})
|
||||
if started.get("status") == "captcha":
|
||||
return {**started, "status": "need_captcha"}
|
||||
return started
|
||||
|
||||
|
||||
@router.post("/auth/local")
|
||||
def local_login(body: dict, session: Session = Depends(get_session)) -> dict:
|
||||
if _setting(session).login_mode != "local":
|
||||
|
||||
Reference in New Issue
Block a user