diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index adf0e51..6b21bd9 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -110,7 +110,8 @@ app shell / routes - 已完成阶段 2 第二个切片:新增 `GetOwnerID`、原子 `GetValueOwned`,将 server 的账号所有权检查和订单凭证读取迁移到窄查询,纯所有权 handler 不再解密完整账号详情,并保留 404/403 行为回归; - 已完成阶段 2 第三个切片:账号列表、运行状态、非敏感详情和单账号详情迁移到 `ListOwnedIDs`、`ListSummaries`、`GetSummaryOwned`,刷新资料先做窄所有权过滤,消除列表批量解密和详情 N+1 完整凭证查询; - 已完成阶段 2 第四个切片:聊天账号校验、商品列表、关键词回复列表、卡券关联校验及管理员删除用户的账号停止流程迁移到 `ListOwnedIDs` 或 `ExistsOwned`,商品凭证读取迁移到 `GetValueOwned`; -- 下一最小工作项:拆分 `internal/server/order_handlers.go` 中订单刷新、手动发货和订单导入的账号列表与凭证读取,分别使用账号 ID 列表、所有权判断和单值凭证接口; +- 已完成阶段 2 第五个切片:订单刷新使用 `ListOwnedIDs` 和 `cookieOwnedByUser` 筛选账号,凭证仍在账号锁内按需读取,刷新后的凭证不再写回批量账号 map; +- 下一最小工作项:迁移手动发货流程的订单账号所有权判断,再迁移订单导入的默认账号和所有权判断; - 随后工作项:再处理明确需要平台凭证的业务流程,统一使用按用户和账号 ID 过滤的单值凭证接口; - 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。 @@ -205,8 +206,8 @@ app shell / routes `GetOwnerID` 只返回所有者 ID;`GetSummaryOwned` 返回指定用户的单个非敏感摘要;`GetValueOwned` 在同一条带 user_id 过滤的查询中读取并解密单个 Cookie,避免 所有权检查与凭证读取之间的竞态窗口。测试使用故意无效的密文值验证摘要查询和所有权检查不会触发解密, 并使用正常加密值覆盖单值凭证读取。账号列表与详情 handler 已不再通过 `AllForUser` 或完整 `GetDetails` 读取敏感字段, -目前 server 生产代码仅剩订单刷新、手动发货和订单导入的 3 处 `AllForUser`,这些流程同时编排凭证和订单业务, -将作为独立切片拆分,避免在一次变更中混合多个订单行为。 +目前 server 生产代码仅剩手动发货和订单导入的 2 处 `AllForUser`;订单刷新已经只读取账号 ID 列表, +其余两个流程将继续按所有权判断和单值凭证边界逐个拆分。 逐步替换使用 `AllForUser` 进行所有权检查以及使用 `GetDetails` 获取非敏感字段的调用。 diff --git a/internal/server/order_handlers.go b/internal/server/order_handlers.go index 688ef50..ea279cb 100644 --- a/internal/server/order_handlers.go +++ b/internal/server/order_handlers.go @@ -171,7 +171,7 @@ func (s *Server) getOrder(w http.ResponseWriter, r *http.Request) { func (s *Server) refreshOrders(w http.ResponseWriter, r *http.Request) { sess := auth.SessionFromContext(r.Context()) - all, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID) + cookieIDs, err := s.Store.Cookies.ListOwnedIDs(r.Context(), sess.UserID) // cookieIDs 和 err 是账号 ID 列表及查询错误。 if err != nil { writeErr(w, http.StatusInternalServerError, "查询账号失败") return @@ -179,12 +179,12 @@ func (s *Server) refreshOrders(w http.ResponseWriter, r *http.Request) { cookieID := r.FormValue("cookie_id") status := r.FormValue("status") if cookieID != "" { - value, ok := all[cookieID] - if !ok { + if !s.cookieOwnedByUser(r.Context(), sess.UserID, cookieID) { writeErr(w, http.StatusForbidden, "Cookie不存在或无权访问") return } - all = map[string]string{cookieID: value} + // 单账号刷新只保留当前用户已通过所有权校验的账号 ID。 + cookieIDs = []string{cookieID} } discovered, listUpdated, softDeleted, failed := 0, 0, 0, 0 @@ -192,7 +192,7 @@ func (s *Server) refreshOrders(w http.ResponseWriter, r *http.Request) { newOrderIDs := make(map[string]struct{}) sessionExpiredAccounts := make(map[string]struct{}) if fetcher, ok := s.mtopClient().(mtop.SoldOrderFetcher); ok { - for cid := range all { + for _, cid := range cookieIDs { // cid 是当前待刷新的账号 ID。 credentialUnlock := s.Store.LockAccountCredentials(cid) latest, latestErr := s.Store.Cookies.GetDetails(r.Context(), cid) if latestErr != nil || latest == nil || latest.UserID != sess.UserID || !hasStoredCookieCredential(latest) { @@ -206,15 +206,15 @@ func (s *Server) refreshOrders(w http.ResponseWriter, r *http.Request) { }) continue } - all[cid] = latest.Value + // latest.Value 仅用于当前账号的凭证调用,不需要写回账号列表。 mtopCtx, cookieSession := withMTopCookieSnapshot(r.Context(), latest) accountDiscovered, accountUpdated, accountNewIDs, accountRemoteIDs, discoveryErr := s.discoverSoldOrders(mtopCtx, fetcher, cid, latest.Value) value, valueChanged, _, persistErr := s.persistMTopCookieSessionLocked(r.Context(), latest, cookieSession) if persistErr != nil { discoveryErr = errors.Join(discoveryErr, fmt.Errorf("保存订单列表响应 Cookie Jar: %w", persistErr)) - } else if valueChanged { - all[cid] = value } + // value 通过运行时更新路径继续传递,不需要写回账号列表。 + // 账号筛选列表只保存 ID,不承载刷新后的凭证值。 credentialUnlock() if persistErr == nil && valueChanged { s.updateRunningCookie(r.Context(), cid, value) @@ -254,7 +254,7 @@ func (s *Server) refreshOrders(w http.ResponseWriter, r *http.Request) { } ordersByCookie := map[string][]refreshTarget{} - for cid := range all { + for _, cid := range cookieIDs { // cid 是当前需要补充订单的账号 ID。 if _, blocked := sessionExpiredAccounts[cid]; blocked { continue }