feat: 用户入口改用自有账号,管理入口只从应用托管进入
This commit is contained in:
@@ -31,16 +31,8 @@ def current_actor(request: Request, authorization: str | None = Header(default=N
|
||||
def _check_platform(request: Request, actor: Actor) -> None:
|
||||
"""平台账号的令牌跟随平台登录:换人、退出或角色变化后即失效。"""
|
||||
identity = platform_identity(request.headers)
|
||||
if identity is None or identity.user_id != actor.id or identity.role != actor.role:
|
||||
if identity is None or identity.user_id != actor.id or identity.role != actor.role or actor.role != "admin":
|
||||
raise HTTPException(status_code=401, detail="平台登录已变化,请重新进入")
|
||||
if actor.role == "user":
|
||||
from app.db import session_scope
|
||||
from app.models import AppSetting
|
||||
|
||||
with session_scope() as session:
|
||||
setting = session.get(AppSetting, 1)
|
||||
if setting is not None and setting.login_mode != "local":
|
||||
raise HTTPException(status_code=401, detail="当前使用大数据平台账号登录")
|
||||
|
||||
|
||||
def require_admin(actor: Actor) -> None:
|
||||
|
||||
+16
-17
@@ -103,18 +103,19 @@ def platform_login(request: Request, session: Session = db_session) -> dict:
|
||||
identity = platform_identity(request.headers)
|
||||
if identity is None:
|
||||
raise _bad("没有平台登录", 401)
|
||||
if identity.role == "user" and _setting(session).login_mode != "local":
|
||||
raise _bad("当前使用大数据平台账号登录", 403)
|
||||
if identity.role != "admin":
|
||||
raise _bad("请从应用托管进入管理", 403)
|
||||
return _token_for("platform", identity.user_id, identity.role, identity.name)
|
||||
|
||||
|
||||
def _matched_local_user(session: Session, username: str) -> User | None:
|
||||
if hosted():
|
||||
return 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":
|
||||
mode = _setting(session).login_mode
|
||||
if hosted():
|
||||
return user if mode == "local" and user.role == "user" else None
|
||||
if mode == "local" or user.role == "admin":
|
||||
return user
|
||||
return None
|
||||
|
||||
@@ -133,7 +134,7 @@ async def login(body: dict) -> dict:
|
||||
return _token_for("local", str(user.id), user.role, user.username)
|
||||
mode = _setting(session).login_mode
|
||||
if mode != "nqi":
|
||||
raise _bad("请从平台登录" if hosted() else "账号或密码不正确")
|
||||
raise _bad("账号或密码不正确")
|
||||
if str(body.get("sessionId") or ""):
|
||||
return await nqi_finish(body)
|
||||
started = await nqi_start({"username": username, "purpose": "login"})
|
||||
@@ -144,14 +145,12 @@ async def login(body: dict) -> dict:
|
||||
|
||||
@router.post("/auth/local")
|
||||
def local_login(body: dict, session: Session = db_session) -> dict:
|
||||
if hosted():
|
||||
raise _bad("请从平台登录")
|
||||
if _setting(session).login_mode != "local":
|
||||
raise _bad("当前使用大数据平台账号登录")
|
||||
username = str(body.get("username") or "").strip()
|
||||
password = str(body.get("password") or "")
|
||||
user = session.scalar(select(User).where(User.username == username))
|
||||
if user is None or not verify_password(password, user.password_hash):
|
||||
if user is None or not verify_password(password, user.password_hash) or (hosted() and user.role != "user"):
|
||||
raise _bad("账号或密码不正确")
|
||||
return _token_for("local", str(user.id), user.role, user.username)
|
||||
|
||||
@@ -373,15 +372,15 @@ def update_settings(body: dict, actor: Actor = Depends(current_actor), session:
|
||||
}
|
||||
|
||||
|
||||
def _require_local_users() -> None:
|
||||
if hosted():
|
||||
raise _bad("自有账号由平台管理", 404)
|
||||
def _require_local_users(session: Session) -> None:
|
||||
if _setting(session).login_mode != "local":
|
||||
raise _bad("当前使用大数据平台账号登录", 404)
|
||||
|
||||
|
||||
@router.get("/admin/users")
|
||||
def list_users(actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
_require_local_users()
|
||||
_require_local_users(session)
|
||||
rows = session.scalars(select(User).order_by(User.id)).all()
|
||||
return {"users": [{"id": row.id, "username": row.username, "role": row.role} for row in rows]}
|
||||
|
||||
@@ -389,10 +388,10 @@ def list_users(actor: Actor = Depends(current_actor), session: Session = db_sess
|
||||
@router.post("/admin/users")
|
||||
def create_user(body: dict, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
_require_local_users()
|
||||
_require_local_users(session)
|
||||
username = str(body.get("username") or "").strip()
|
||||
password = str(body.get("password") or "")
|
||||
role = "admin" if body.get("role") == "admin" else "user"
|
||||
role = "user" if hosted() else ("admin" if body.get("role") == "admin" else "user")
|
||||
if not username or not password:
|
||||
raise _bad("请填写账号和密码")
|
||||
if session.scalar(select(User).where(User.username == username)) is not None:
|
||||
@@ -404,7 +403,7 @@ def create_user(body: dict, actor: Actor = Depends(current_actor), session: Sess
|
||||
@router.delete("/admin/users/{user_id}")
|
||||
def delete_user(user_id: int, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
_require_local_users()
|
||||
_require_local_users(session)
|
||||
user = session.get(User, user_id)
|
||||
if user is None:
|
||||
raise _bad("账号不存在", 404)
|
||||
@@ -417,7 +416,7 @@ def delete_user(user_id: int, actor: Actor = Depends(current_actor), session: Se
|
||||
@router.put("/admin/users/{user_id}/password")
|
||||
def update_user_password(user_id: int, body: dict, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
_require_local_users()
|
||||
_require_local_users(session)
|
||||
user = session.get(User, user_id)
|
||||
if user is None:
|
||||
raise _bad("账号不存在", 404)
|
||||
|
||||
@@ -11,6 +11,8 @@ from fastapi.testclient import TestClient
|
||||
|
||||
TEST_DATA = Path(os.environ["DECOM_DATA_DIR"])
|
||||
|
||||
from sqlalchemy import select # noqa: E402
|
||||
|
||||
from app.auth.platform import base_href, platform_identity # noqa: E402
|
||||
from app.db import reset_engine, session_scope # noqa: E402
|
||||
from app.main import create_app # noqa: E402
|
||||
@@ -163,8 +165,8 @@ def test_platform_login_three_cases(hosted: TestClient):
|
||||
assert admin.json()["user"] == {"kind": "platform", "id": "a-1", "role": "admin", "name": "管理员甲"}
|
||||
|
||||
user = hosted.post("/api/auth/platform", headers=identity("user"))
|
||||
assert user.status_code == 200
|
||||
assert user.json()["user"] == {"kind": "platform", "id": "u-1", "role": "user", "name": "张三"}
|
||||
assert user.status_code == 403
|
||||
assert user.json()["detail"] == "请从应用托管进入管理"
|
||||
|
||||
saved = hosted.put(
|
||||
"/api/admin/settings",
|
||||
@@ -174,7 +176,7 @@ def test_platform_login_three_cases(hosted: TestClient):
|
||||
assert saved.status_code == 200
|
||||
refused = hosted.post("/api/auth/platform", headers=identity("user"))
|
||||
assert refused.status_code == 403
|
||||
assert refused.json()["detail"] == "当前使用大数据平台账号登录"
|
||||
assert refused.json()["detail"] == "请从应用托管进入管理"
|
||||
still_admin = hosted.post("/api/auth/platform", headers=identity("admin", user_id="a-1"))
|
||||
assert still_admin.status_code == 200
|
||||
|
||||
@@ -208,20 +210,23 @@ def test_local_password_login_closed_when_hosted(hosted: TestClient):
|
||||
with session_scope() as session:
|
||||
session.add(User(username="admin", password_hash=hash_password("admin-pass"), role="admin"))
|
||||
session.add(User(username="worker", password_hash=hash_password("worker-pass"), role="user"))
|
||||
for username, password in (("admin", "admin-pass"), ("worker", "worker-pass")):
|
||||
local = hosted.post("/api/auth/local", json={"username": username, "password": password})
|
||||
assert local.status_code == 400
|
||||
assert local.json()["detail"] == "请从平台登录"
|
||||
combined = hosted.post("/api/auth/login", json={"username": username, "password": password})
|
||||
assert combined.status_code == 400
|
||||
assert combined.json()["detail"] == "请从平台登录"
|
||||
for path in ("/api/auth/local", "/api/auth/login"):
|
||||
blocked = hosted.post(path, json={"username": "admin", "password": "admin-pass"})
|
||||
assert blocked.status_code == 400
|
||||
assert blocked.json()["detail"] == "账号或密码不正确"
|
||||
allowed = hosted.post("/api/auth/login", json={"username": "worker", "password": "worker-pass"})
|
||||
assert allowed.status_code == 200
|
||||
assert allowed.json()["user"]["role"] == "user"
|
||||
|
||||
admin_headers = identity("admin", user_id="a-1", username="boss", display="管理员甲")
|
||||
headers = bearer(admin_token(hosted), admin_headers)
|
||||
assert hosted.get("/api/admin/users", headers=headers).status_code == 404
|
||||
assert hosted.post("/api/admin/users", json={"username": "x", "password": "y"}, headers=headers).status_code == 404
|
||||
assert hosted.put("/api/admin/users/1/password", json={"password": "z"}, headers=headers).status_code == 404
|
||||
assert hosted.delete("/api/admin/users/1", headers=headers).status_code == 404
|
||||
listed = hosted.get("/api/admin/users", headers=headers)
|
||||
assert listed.status_code == 200
|
||||
created = hosted.post("/api/admin/users", json={"username": "new", "password": "new-pass", "role": "admin"}, headers=headers)
|
||||
assert created.status_code == 200
|
||||
with session_scope() as session:
|
||||
created_user = session.scalar(select(User).where(User.username == "new"))
|
||||
assert created_user is not None and created_user.role == "user"
|
||||
|
||||
|
||||
def test_nqi_admin_name_goes_to_platform_when_hosted(hosted: TestClient, monkeypatch):
|
||||
@@ -272,15 +277,19 @@ def test_switch_to_local_needs_no_local_admin_when_hosted(hosted: TestClient):
|
||||
assert back.json()["loginMode"] == "local"
|
||||
|
||||
|
||||
def test_platform_user_runs_export(hosted: TestClient):
|
||||
from app.models import ExportTask
|
||||
def test_local_user_runs_export(hosted: TestClient):
|
||||
from app.models import ExportTask, User
|
||||
from app.security import hash_password
|
||||
|
||||
user_headers = identity("user")
|
||||
login = hosted.post("/api/auth/platform", headers=user_headers)
|
||||
headers = bearer(login.json()["token"], user_headers)
|
||||
with session_scope() as session:
|
||||
session.add(User(username="worker", password_hash=hash_password("worker-pass"), role="user"))
|
||||
session.add(User(username="other", password_hash=hash_password("other-pass"), role="user"))
|
||||
login = hosted.post("/api/auth/login", json={"username": "worker", "password": "worker-pass"})
|
||||
headers = bearer(login.json()["token"])
|
||||
created = hosted.post("/api/tasks", json={"format": "csv", "cellKey": ""}, headers=headers)
|
||||
assert created.status_code == 200
|
||||
task_id = created.json()["task"]["id"]
|
||||
listing = []
|
||||
for _ in range(30):
|
||||
listing = hosted.get("/api/tasks", headers=headers).json()["tasks"]
|
||||
if listing[0]["status"] == "done":
|
||||
@@ -289,12 +298,12 @@ def test_platform_user_runs_export(hosted: TestClient):
|
||||
assert listing[0]["status"] == "done"
|
||||
with session_scope() as session:
|
||||
task = session.get(ExportTask, task_id)
|
||||
assert (task.owner_kind, task.owner_id) == ("platform", "u-1")
|
||||
owner = session.scalar(select(User).where(User.username == "worker"))
|
||||
assert (task.owner_kind, task.owner_id) == ("local", str(owner.id))
|
||||
downloaded = hosted.get(f"/api/tasks/{task_id}/file", headers=headers)
|
||||
assert downloaded.status_code == 200
|
||||
other = identity("user", user_id="u-2")
|
||||
other_login = hosted.post("/api/auth/platform", headers=other)
|
||||
assert hosted.get(f"/api/tasks/{task_id}/file", headers=bearer(other_login.json()["token"], other)).status_code == 404
|
||||
other_login = hosted.post("/api/auth/login", json={"username": "other", "password": "other-pass"})
|
||||
assert hosted.get(f"/api/tasks/{task_id}/file", headers=bearer(other_login.json()["token"])).status_code == 404
|
||||
admin_headers = identity("admin", user_id="a-1")
|
||||
assert hosted.get("/api/tasks", headers=bearer(admin_token(hosted), admin_headers)).status_code == 403
|
||||
|
||||
|
||||
Reference in New Issue
Block a user