@@ -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/") {
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user