--- production/wechat_rpa/knowledge_api.py +++ release/wechat_rpa/knowledge_api.py @@ -143,6 +143,19 @@ if not isinstance(scope, str) or scope == "*": raise HTTPException(400, "请先选择具体的客户端账号") return scope + + def item_tenant(principal, account_id, item_id): + """单条知识归哪个账号:选了具体账号就是它;「全部账号」下按知识自己的账号,并且必须在授权范围内。 + 不在范围内一律报"不存在",不透露别的账号有哪些知识。""" + scope = resolve_admin_tenant(principal, account_id) + if isinstance(scope, str) and scope != "*": + return scope + with database.connect() as db: + row = db.execute("SELECT tenant_id FROM knowledge_item WHERE id=?", (item_id,)).fetchone() + allowed = tenant_scope(scope) + if row is None or not (allowed.everything or row[0] in allowed.tenants): + raise HTTPException(404, "知识不存在或不在你的授权范围内") + return row[0] from knowledge_review import KnowledgeReviewStore reviews = KnowledgeReviewStore(store) @@ -320,18 +333,29 @@ def items(account_id: int | None = None, status: str = "", query: str = "", page: int = Query(default=1, ge=1), page_size: int = Query(default=30, ge=1, le=100), principal=Depends(require("knowledge:read"))): - return store.list_items(resolve_admin_tenant(principal, account_id), status, query, page, page_size) + result = store.list_items(resolve_admin_tenant(principal, account_id), status, query, page, page_size) + # 「全部账号」下列表混着多个账号的知识:每条带上所属账号,点开就能直接看、直接审 + with database.connect() as db: + labels = store._tenant_labels(db, {item["tenant_id"] for item in result["items"]}) + for item in result["items"]: + item.update(account_id=labels[item["tenant_id"]]["account_id"], account_label=labels[item["tenant_id"]]["label"]) + return result @app.get("/api/v2/knowledge/items/{item_id}") def detail(item_id: str, account_id: int | None = None, principal=Depends(require("knowledge:read"))): with errors(): - return store.detail(single(principal, account_id), item_id, sources="im:content:read" in principal.permissions) + tenant = item_tenant(principal, account_id, item_id) + item = store.detail(tenant, item_id, sources="im:content:read" in principal.permissions) + with database.connect() as db: + label = store._tenant_labels(db, [tenant])[tenant] + return {**item, "account_id": label["account_id"], "account_label": label["label"]} @app.put("/api/v2/knowledge/items/{item_id}") def edit(item_id: str, body: EditBody, request: Request, account_id: int | None = None, principal=Depends(require("knowledge:write", "im:content:read"))): with errors(): - return store.save(single(principal, account_id), item_id, body.model_dump(), principal.id, client_ip(request)) + return store.save(item_tenant(principal, account_id, item_id), item_id, body.model_dump(), principal.id, + client_ip(request)) @app.post("/api/v2/knowledge/items/{item_id}/{action}") def transition(item_id: str, action: str, body: ActionBody, request: Request, @@ -340,7 +364,7 @@ "knowledge:write" if action in {"restore", "delete"} else "knowledge:review") if permission not in principal.permissions or "im:content:read" not in principal.permissions: raise HTTPException(403, "缺少知识操作或来源正文查看权限") - tenant = single(principal, account_id) + tenant = item_tenant(principal, account_id, item_id) with errors(): if action == 'delete': if not body.confirmed: