package api import ( "context" "errors" "net/http" "droboticflow/internal/domain" "droboticflow/internal/store" "github.com/go-chi/chi/v5" ) type acceptanceAccessKey struct{} type acceptanceAccess struct { user *domain.UserRecord enabled bool active bool projects []string err error } func (s *server) acceptanceAccessCache(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), acceptanceAccessKey{}, map[string]acceptanceAccess{}))) }) } func (s *server) acceptanceAccessFor(ctx context.Context, userID string) acceptanceAccess { cache, _ := ctx.Value(acceptanceAccessKey{}).(map[string]acceptanceAccess) if v, ok := cache[userID]; ok { return v } v := acceptanceAccess{} v.user, v.err = s.store.GetUser(ctx, userID) if v.err == nil && v.user != nil && v.user.Role == domain.UserRoleAcceptanceReviewer && v.user.Status != domain.UserStatusDisabled && v.user.DeletedAt == "" && !v.user.IsSuperAdmin { sp, err := s.store.GetSupplier(ctx, v.user.SupplierID) v.err = err if err == nil && sp != nil && sp.Status == "active" && tenantMatches(sp.OrganizationID, v.user.OrganizationID) { v.active = true grant, err := s.store.GetSupplierAcceptanceAuthorization(ctx, v.user.OrganizationID, v.user.SupplierID) v.err = err v.enabled = err == nil && grant.Enabled } } if v.err == nil && v.active && !v.enabled { v.projects, _, v.err = s.store.SharedAcceptanceInventory(ctx, v.user.OrganizationID, v.user.SupplierID, v.user.ID) } if cache != nil { cache[userID] = v } return v } // handled means legacy membership must not be used as a fallback. Only the // acceptance stage is expanded; evidence routes continue to bind to this item. func (s *server) sharedAcceptanceAccess(ctx context.Context, userID string, item domain.HumanItemRecord, claim bool) (allowed, handled bool) { a := s.acceptanceAccessFor(ctx, userID) if a.err != nil { return false, true } if a.user == nil || a.user.Role != domain.UserRoleAcceptanceReviewer { return false, false } if !a.active || !tenantMatches(a.user.OrganizationID, item.OrganizationID) { return false, true } batch, err := s.humanBatchForListRead(ctx, item.BatchID) if err != nil { return false, true } if domain.SharedAcceptanceItem(item, batch) { if a.enabled { return true, true } if !claim && item.AcceptanceClaim.Matches(item, *a.user) { return true, true } } return false, a.user.AcceptanceAccessMode == "managed" } func (s *server) handleSupplierAcceptanceAuthorization(w http.ResponseWriter, r *http.Request) { p, ok := currentPrincipal(r) id := chi.URLParam(r, "supplierId") if !ok || (!isOrganizationAdmin(p) && !(isSupplierManager(p) && p.User.SupplierID == id && r.Method == http.MethodGet)) { renderJSON(w, 403, map[string]string{"error": "仅平台管理员可修改最终验收授权"}) return } sp, err := s.store.GetSupplier(r.Context(), id) if err != nil { renderError(w, 500, err) return } if sp == nil || !tenantMatches(sp.OrganizationID, s.requestOrganizationID(r)) || sp.Status != "active" { renderJSON(w, 404, map[string]string{"error": "供应商不存在或不可用"}) return } var grant *domain.SupplierAcceptanceAuthorization if r.Method == http.MethodPut { var input struct { Enabled *bool `json:"enabled"` Version *int `json:"version"` } if !decodeJSON(w, r, &input) { return } if input.Enabled == nil || input.Version == nil || *input.Version < 0 { renderValidationError(w, validationIssue{Field: "authorization", Message: "请提供授权状态和当前版本"}) return } grant, err = s.store.SetSupplierAcceptanceAuthorization(r.Context(), sp.OrganizationID, id, *input.Enabled, *input.Version, p.User.ID) } else { grant, err = s.store.GetSupplierAcceptanceAuthorization(r.Context(), sp.OrganizationID, id) } if errors.Is(err, store.ErrAcceptanceAuthorizationConflict) { renderJSON(w, 409, map[string]string{"error": err.Error(), "code": "acceptance_authorization_changed"}) return } if err != nil { renderUserMutationError(w, err) return } _, count, err := s.store.SharedAcceptanceInventory(r.Context(), sp.OrganizationID, id, "") if err != nil { renderError(w, 500, err) return } renderJSON(w, 200, struct { *domain.SupplierAcceptanceAuthorization ClaimedCount int `json:"claimedCount"` }{grant, count}) } // Only project/workflow navigation lists use this expansion. Generic run, // artifact, annotation and QC endpoints retain their existing project guards. func (s *server) acceptanceProjectVisible(r *http.Request, org, project string) bool { p, ok := currentPrincipal(r) if !ok || p.User.Role != domain.UserRoleAcceptanceReviewer || !tenantMatches(org, s.requestOrganizationID(r)) { return false } a := s.acceptanceAccessFor(r.Context(), p.User.ID) if a.err != nil || !a.active { return false } if a.enabled { return true } for _, id := range a.projects { if id == project { return true } } return false } func (s *server) acceptanceAuthorizationDenied(w http.ResponseWriter, r *http.Request) bool { p, ok := currentPrincipal(r) if !ok || p.User.Role != domain.UserRoleAcceptanceReviewer { return false } a := s.acceptanceAccessFor(r.Context(), p.User.ID) if a.user != nil && a.user.AcceptanceAccessMode == "managed" && !a.enabled { renderJSON(w, http.StatusForbidden, map[string]string{"code": "acceptance_authorization_required", "error": "供应商尚未获得最终验收授权或授权已关闭;已领取任务仍可完成,请联系管理员。"}) return true } return false }