feat(settings): 汇总全部数据源配置操作审计
build / build (push) Successful in 1m13s

This commit is contained in:
姚章浩
2026-10-27 10:42:30 +08:00
parent f0b12412a1
commit 7448d130ec
17 changed files with 453 additions and 56 deletions
+3
View File
@@ -99,6 +99,9 @@ func isAuthEndpoint(method, path string) bool {
// classify maps a request to the minimum role required.
func classifyRole(method, path string) string {
if path == "/api/v1/source-connections/audit" {
return roleAdmin
}
// The member roster exposes account identities and roles: admin-only for
// every method, checked before the read shortcut below.
if strings.HasPrefix(path, "/api/v1/users") || strings.HasPrefix(path, "/api/v1/qa-ops/") {
+2 -2
View File
@@ -141,7 +141,7 @@ func registerDingtalkSpace(w http.ResponseWriter, r *http.Request, config Config
return
}
connectionID, err := store.RegisterDingtalkSpace(r.Context(), postgresstore.RegisterDingtalkSpaceInput{
RootKey: resolved.Key, Name: resolved.Name, SpaceID: spaceID, RootID: resolved.Root, Now: config.Now(),
RootKey: resolved.Key, Name: resolved.Name, SpaceID: spaceID, RootID: resolved.Root, Now: config.Now(), Actor: actorFor(r),
})
switch {
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
@@ -286,7 +286,7 @@ func registerSpaceThroughDWS(w http.ResponseWriter, r *http.Request, config Conf
return true, false
}
connectionID, err := store.RegisterDingtalkSpace(ctx, postgresstore.RegisterDingtalkSpaceInput{
RootKey: rootKey, Name: name, SpaceID: spaceID, RootID: rootID, Now: config.Now(),
RootKey: rootKey, Name: name, SpaceID: spaceID, RootID: rootID, Now: config.Now(), Actor: actorFor(r),
})
switch {
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
+1
View File
@@ -419,6 +419,7 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("DELETE /api/v1/filename-filters", func(w http.ResponseWriter, r *http.Request) {
mutateFilenameFilter(w, r, config, true)
})
mux.HandleFunc("GET /api/v1/source-connections/audit", func(w http.ResponseWriter, r *http.Request) { listSourceAudit(w, r, config) })
mux.HandleFunc("GET /api/v1/source-connections", func(w http.ResponseWriter, r *http.Request) {
listSourceConnections(w, r, config.Store)
})
+45
View File
@@ -0,0 +1,45 @@
package httpapi
import (
"context"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http"
"strconv"
)
type sourceAuditReader interface {
ListSourceAudit(context.Context, int, int) (postgresstore.SourceAuditPage, error)
}
func listSourceAudit(w http.ResponseWriter, r *http.Request, config Config) {
if _, ok := requireSessionIdentity(w, r); !ok {
return
}
limit, offset := 100, 0
for key, target := range map[string]*int{"limit": &limit, "offset": &offset} {
if value := r.URL.Query().Get(key); value != "" {
n, err := strconv.Atoi(value)
if err != nil {
writeJSON(w, 400, map[string]string{"error": "invalid pagination"})
return
}
*target = n
}
}
if limit < 1 || limit > 100 || offset < 0 {
writeJSON(w, 400, map[string]string{"error": "invalid pagination"})
return
}
reader, ok := config.Store.(sourceAuditReader)
if !ok {
writeJSON(w, 501, map[string]string{"error": "audit not configured"})
return
}
page, err := reader.ListSourceAudit(r.Context(), limit, offset)
if err != nil {
writeJSON(w, 500, map[string]string{"error": "无法读取数据源操作记录"})
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, map[string]any{"data": page})
}
+12
View File
@@ -0,0 +1,12 @@
package httpapi
import (
"net/http"
"testing"
)
func TestSourceAuditRequiresAdmin(t *testing.T) {
if got := classifyRole(http.MethodGet, "/api/v1/source-connections/audit"); got != roleAdmin {
t.Fatalf("audit exposes actors: required role %s", got)
}
}