Unify auth middleware and remove X-User-ID trust, add project_members authorization, sandbox workspaces via resolver, and admin-only reconcile by run_id. Introduce goose migrations with startup schema gate, fail closed when DB unavailable outside explicit ALLOW_IN_MEMORY, bundle web/dist in app Dockerfile, and remove frontend pseudo-login fallback.
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"chat-agent-system/internal/auth"
|
|
)
|
|
|
|
func (s *Server) assertTaskRunOwnedByUser(w http.ResponseWriter, ctx context.Context, runID string, user *auth.UserAccount) bool {
|
|
if user == nil {
|
|
writeJSONResponse(w, http.StatusUnauthorized, map[string]string{
|
|
"error_code": "ERR_UNAUTHENTICATED",
|
|
"message": "请先登录",
|
|
})
|
|
return false
|
|
}
|
|
rec, err := s.taskRuns.GetRun(runID)
|
|
if err != nil {
|
|
http.Error(w, "run not found", http.StatusNotFound)
|
|
return false
|
|
}
|
|
if isAdminUser(user) {
|
|
return true
|
|
}
|
|
if rec.UserID > 0 && rec.UserID == user.ID {
|
|
return true
|
|
}
|
|
if rec.ProjectID > 0 && s.authorizer != nil {
|
|
if err := s.authorizer.RequireProjectAccess(ctx, rec.ProjectID, user.ID); err == nil {
|
|
return true
|
|
}
|
|
}
|
|
writeJSONResponse(w, http.StatusForbidden, map[string]string{
|
|
"error_code": "ERR_FORBIDDEN",
|
|
"message": "无权访问该任务",
|
|
})
|
|
return false
|
|
}
|
|
|
|
func isAdminUser(user *auth.UserAccount) bool {
|
|
return user != nil && user.RoleID == "role-admin"
|
|
}
|