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}
|
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")
|
@router.post("/auth/local")
|
||||||
def local_login(body: dict, session: Session = Depends(get_session)) -> dict:
|
def local_login(body: dict, session: Session = Depends(get_session)) -> dict:
|
||||||
if _setting(session).login_mode != "local":
|
if _setting(session).login_mode != "local":
|
||||||
|
|||||||
@@ -110,6 +110,38 @@ def test_local_login_task_and_admin(client: TestClient):
|
|||||||
assert "小区" in downloaded.content.decode("utf-8-sig")
|
assert "小区" in downloaded.content.decode("utf-8-sig")
|
||||||
|
|
||||||
|
|
||||||
|
def test_admin_is_judged_locally_when_users_use_nqi(client: TestClient, monkeypatch):
|
||||||
|
from app.nqi.client import NqiError
|
||||||
|
|
||||||
|
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
|
||||||
|
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
|
||||||
|
client.post("/api/admin/users", json={"username": "user", "password": "user-pass", "role": "user"}, headers=headers)
|
||||||
|
saved = client.put(
|
||||||
|
"/api/admin/settings",
|
||||||
|
json={"loginMode": "nqi", "credentialRefreshSeconds": 300, "workerConcurrency": 4},
|
||||||
|
headers=headers,
|
||||||
|
)
|
||||||
|
assert saved.status_code == 200
|
||||||
|
calls: list[str] = []
|
||||||
|
|
||||||
|
async def fail_open(self):
|
||||||
|
calls.append("open")
|
||||||
|
raise NqiError("不应访问平台")
|
||||||
|
|
||||||
|
monkeypatch.setattr("app.api.http.NqiClient.open_login", fail_open)
|
||||||
|
wrong = client.post("/api/auth/login", json={"username": "admin", "password": "wrong"})
|
||||||
|
assert wrong.status_code == 400
|
||||||
|
assert wrong.json()["detail"] == "登录失败"
|
||||||
|
ok = client.post("/api/auth/login", json={"username": "admin", "password": "admin-pass"})
|
||||||
|
assert ok.status_code == 200
|
||||||
|
assert ok.json()["user"]["role"] == "admin"
|
||||||
|
assert calls == []
|
||||||
|
other = client.post("/api/auth/login", json={"username": "user", "password": "user-pass"})
|
||||||
|
assert other.status_code == 400
|
||||||
|
assert other.json()["detail"] == "不应访问平台"
|
||||||
|
assert calls == ["open"]
|
||||||
|
|
||||||
|
|
||||||
def test_pool_add_is_allowed_when_login_is_local(client: TestClient, monkeypatch):
|
def test_pool_add_is_allowed_when_login_is_local(client: TestClient, monkeypatch):
|
||||||
from app.nqi.client import NqiError
|
from app.nqi.client import NqiError
|
||||||
|
|
||||||
|
|||||||
@@ -35,44 +35,50 @@ const busy = ref(false);
|
|||||||
let countdownTimer = 0;
|
let countdownTimer = 0;
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if (props.purpose === "pool") {
|
if (props.purpose !== "pool") return;
|
||||||
mode.value = "nqi";
|
mode.value = "nqi";
|
||||||
} else {
|
await startNqi();
|
||||||
const result = await api<{ mode: string }>("/api/auth/mode");
|
|
||||||
mode.value = result.mode;
|
|
||||||
}
|
|
||||||
if (mode.value === "nqi") await startNqi();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => window.clearInterval(countdownTimer));
|
onUnmounted(() => window.clearInterval(countdownTimer));
|
||||||
|
|
||||||
function submitLogin() {
|
function submitLogin() {
|
||||||
if (busy.value) return;
|
if (busy.value || captchaLoading.value) return;
|
||||||
if (mode.value === "local") {
|
if (props.purpose === "pool") {
|
||||||
enterLocal();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!sessionId.value) {
|
if (!sessionId.value) {
|
||||||
startNqi();
|
startNqi();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
finishNqi();
|
finishNqi();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (sessionId.value) {
|
||||||
|
finishNqi();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
beginLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
function fail(error: unknown) {
|
async function beginLogin() {
|
||||||
errorText.value = error instanceof Error ? error.message : "登录失败";
|
|
||||||
}
|
|
||||||
|
|
||||||
async function enterLocal() {
|
|
||||||
busy.value = true;
|
busy.value = true;
|
||||||
errorText.value = "";
|
errorText.value = "";
|
||||||
try {
|
try {
|
||||||
const result = await api<{ token: string; user: { role: string; name: string } }>("/api/auth/local", {
|
const result = await api<{ status: string; token?: string; user?: { role: string; name: string }; sessionId?: string; captcha?: string }>("/api/auth/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ username: username.value, password: password.value }),
|
body: JSON.stringify({ username: username.value, password: password.value }),
|
||||||
});
|
});
|
||||||
|
if (result.status === "ok" && result.token && result.user) {
|
||||||
saveSession(result.token, result.user);
|
saveSession(result.token, result.user);
|
||||||
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result.status === "need_captcha") {
|
||||||
|
mode.value = "nqi";
|
||||||
|
sessionId.value = result.sessionId || "";
|
||||||
|
captchaUrl.value = result.captcha || "";
|
||||||
|
captchaBroken.value = !captchaUrl.value;
|
||||||
|
captcha.value = "";
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
fail(error);
|
fail(error);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -80,6 +86,10 @@ async function enterLocal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fail(error: unknown) {
|
||||||
|
errorText.value = error instanceof Error ? error.message : "登录失败";
|
||||||
|
}
|
||||||
|
|
||||||
async function startNqi() {
|
async function startNqi() {
|
||||||
captchaLoading.value = true;
|
captchaLoading.value = true;
|
||||||
captchaBroken.value = false;
|
captchaBroken.value = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user