feat(web): login gate, session identity UI and member management

- main.tsx 挂 AuthProvider 门控:未登录→登录页;must_change_password→强制改密页
- 登录页/改密页/顶栏身份(头像+姓名+角色徽章+退出)按 A·Data-console token 样式
- 全局 fetch 拦截器:任意业务请求 401 自动回登录页(会话过期/被撤销不留死表单)
- 系统设置新增「成员与角色」分区(仅 admin 可见):名单、角色调整、启停、重置临时密码、新增成员
- 消灭全部手填操作人:审核/封批/确认计划集/范围规则/来源调度/UI 设置/开始处理/批量重试/
  目录差异确认共 9 处输入框删除,请求体不再携带 actor 字段(服务端从会话注入)
- 测试断言方向翻转:显式验证请求体不含 operator_ref/created_by 等字段
- 附带修复:登录失败计数 SQL 参数类型无法推断导致 500(pgx 扩展协议),显式 cast 解决

前端门禁:tsc 0 错、125 用例全绿、build 通过
This commit is contained in:
2026-09-13 16:27:11 +08:00
committed by 姚章浩
parent fbe80ad784
commit f6cb90cd70
30 changed files with 637 additions and 158 deletions
+10 -11
View File
@@ -799,7 +799,7 @@ type scopeRuleInput struct {
SourceItemID string `json:"source_item_id"`
Visibility governance.Visibility `json:"visibility"`
Reason string `json:"reason"`
Reason string `json:"reason"`
}
func listScopeRules(w http.ResponseWriter, r *http.Request, store Store) {
@@ -874,10 +874,10 @@ type sourceSettingsRevisionRequest struct {
Schedule struct {
RefreshIntervalSeconds *int `json:"refresh_interval_seconds"`
} `json:"schedule"`
Operator string `json:"operator"`
}
func (req sourceSettingsRevisionRequest) toDraft() postgresstore.SourceSettingsDraft {
// toDraft stamps the actor server-side; the browser never names the operator.
func (req sourceSettingsRevisionRequest) toDraft(actor string) postgresstore.SourceSettingsDraft {
schedule := "{}"
if req.Schedule.RefreshIntervalSeconds != nil {
schedule = fmt.Sprintf(`{"refresh_interval_seconds":%d}`, *req.Schedule.RefreshIntervalSeconds)
@@ -885,7 +885,7 @@ func (req sourceSettingsRevisionRequest) toDraft() postgresstore.SourceSettingsD
return postgresstore.SourceSettingsDraft{
SourceType: req.SourceType, Name: req.Name, BaseURL: req.BaseURL,
SecretRef: req.SecretRef, Enabled: req.Enabled,
Schedule: json.RawMessage(schedule), Operator: req.Operator,
Schedule: json.RawMessage(schedule), Operator: actor,
}
}
@@ -922,7 +922,7 @@ func createSourceSettingsRevision(w http.ResponseWriter, r *http.Request, config
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid source settings revision"})
return
}
revisionID, replayed, err := config.Store.CreateSourceSettingsRevisionWithOperation(r.Context(), connectionID, request.toDraft(), operationID, config.Now().UTC())
revisionID, replayed, err := config.Store.CreateSourceSettingsRevisionWithOperation(r.Context(), connectionID, request.toDraft(actorFor(r)), operationID, config.Now().UTC())
if errors.Is(err, postgresstore.ErrOperationRequestConflict) {
writeJSON(w, http.StatusConflict, map[string]string{"error": "Idempotency-Key conflicts with a different request"})
return
@@ -1135,11 +1135,10 @@ func reviewPublication(w http.ResponseWriter, r *http.Request, config Config) {
}
type createUISettingsCandidateRequest struct {
DocumentRefreshSeconds int `json:"document_refresh_seconds"`
ResourceRefreshSeconds int `json:"resource_refresh_seconds"`
StalledAfterSeconds int `json:"stalled_after_seconds"`
FinalizingAfterSeconds int `json:"finalizing_after_seconds"`
UpdatedBy string `json:"updated_by"`
DocumentRefreshSeconds int `json:"document_refresh_seconds"`
ResourceRefreshSeconds int `json:"resource_refresh_seconds"`
StalledAfterSeconds int `json:"stalled_after_seconds"`
FinalizingAfterSeconds int `json:"finalizing_after_seconds"`
}
func getActiveUISettings(w http.ResponseWriter, r *http.Request, store Store) {
@@ -1170,7 +1169,7 @@ func createUISettingsCandidate(w http.ResponseWriter, r *http.Request, config Co
ResourceRefreshSeconds: request.ResourceRefreshSeconds,
StalledAfterSeconds: request.StalledAfterSeconds,
FinalizingAfterSeconds: request.FinalizingAfterSeconds,
UpdatedBy: request.UpdatedBy,
UpdatedBy: actorFor(r),
Now: config.Now(),
}, operationID)
if errors.Is(err, postgresstore.ErrOperationRequestConflict) {
+10 -10
View File
@@ -442,13 +442,13 @@ func TestHandlerSourceSettingsRevisionEndpoints(t *testing.T) {
}
forbidden := httptest.NewRecorder()
handler.ServeHTTP(forbidden, httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"n","operator":"o","schedule":{}}`)))
handler.ServeHTTP(forbidden, httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"n","schedule":{}}`)))
if forbidden.Code != http.StatusForbidden {
t.Fatalf("intent header gate = %d, want 403", forbidden.Code)
}
missingKey := httptest.NewRecorder()
request := httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"国内工业AList","operator":"zherom","enabled":true,"schedule":{"refresh_interval_seconds":3600}}`))
request := httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"国内工业AList","enabled":true,"schedule":{"refresh_interval_seconds":3600}}`))
request.Header.Set("X-Governance-Action", "source-settings")
handler.ServeHTTP(missingKey, request)
if missingKey.Code != http.StatusBadRequest {
@@ -456,14 +456,14 @@ func TestHandlerSourceSettingsRevisionEndpoints(t *testing.T) {
}
created := httptest.NewRecorder()
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"国内工业AList","operator":"zherom","enabled":true,"schedule":{"refresh_interval_seconds":3600}}`))
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"国内工业AList","enabled":true,"schedule":{"refresh_interval_seconds":3600}}`))
request.Header.Set("X-Governance-Action", "source-settings")
request.Header.Set("Idempotency-Key", "create-v1")
handler.ServeHTTP(created, request)
if created.Code != http.StatusCreated {
t.Fatalf("create = %d body=%s", created.Code, created.Body.String())
}
if store.createSettingsConnectionID != "conn-1" || store.createSettingsOperationID != "create-v1" || store.createSettingsDraft.Name != "国内工业AList" || store.createSettingsDraft.Operator != "zherom" {
if store.createSettingsConnectionID != "conn-1" || store.createSettingsOperationID != "create-v1" || store.createSettingsDraft.Name != "国内工业AList" || store.createSettingsDraft.Operator != "system" {
t.Fatalf("create request = %s %+v op=%s", store.createSettingsConnectionID, store.createSettingsDraft, store.createSettingsOperationID)
}
if !strings.Contains(created.Body.String(), "scr-fake") || !strings.Contains(created.Body.String(), "created") {
@@ -472,7 +472,7 @@ func TestHandlerSourceSettingsRevisionEndpoints(t *testing.T) {
store.createSettingsErr = postgresstore.ErrInvalidSourceSettings
rejected := httptest.NewRecorder()
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"ftp","name":"x","operator":"o","schedule":{}}`))
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"ftp","name":"x","schedule":{}}`))
request.Header.Set("X-Governance-Action", "source-settings")
request.Header.Set("Idempotency-Key", "create-v2")
handler.ServeHTTP(rejected, request)
@@ -482,7 +482,7 @@ func TestHandlerSourceSettingsRevisionEndpoints(t *testing.T) {
store.createSettingsErr = postgresstore.ErrOperationRequestConflict
conflicted := httptest.NewRecorder()
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"x","operator":"o","schedule":{}}`))
request = httptest.NewRequest("POST", "/api/v1/source-connections/conn-1/settings/revisions", strings.NewReader(`{"source_type":"alist","name":"x","schedule":{}}`))
request.Header.Set("X-Governance-Action", "source-settings")
request.Header.Set("Idempotency-Key", "create-v3")
handler.ServeHTTP(conflicted, request)
@@ -952,20 +952,20 @@ func TestHandlerVersionsAndActivatesUISettings(t *testing.T) {
t.Fatalf("get status=%d body=%s", get.Code, get.Body.String())
}
noIntent := httptest.NewRecorder()
handler.ServeHTTP(noIntent, httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400,"updated_by":"engineer-1"}`)))
handler.ServeHTTP(noIntent, httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400}`)))
if noIntent.Code != http.StatusForbidden {
t.Fatalf("missing intent status=%d body=%s", noIntent.Code, noIntent.Body.String())
}
create := httptest.NewRecorder()
createRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400,"updated_by":"engineer-1"}`))
createRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400}`))
createRequest.Header.Set("X-Governance-Action", "ui-settings")
createRequest.Header.Set("Idempotency-Key", "op-uis-create")
handler.ServeHTTP(create, createRequest)
if create.Code != http.StatusCreated || store.createUISettingsRequest.Now != now || store.createUISettingsRequest.UpdatedBy != "engineer-1" || !strings.Contains(create.Body.String(), `"operation":"created"`) {
if create.Code != http.StatusCreated || store.createUISettingsRequest.Now != now || store.createUISettingsRequest.UpdatedBy != "system" || !strings.Contains(create.Body.String(), `"operation":"created"`) {
t.Fatalf("create status=%d request=%#v body=%s", create.Code, store.createUISettingsRequest, create.Body.String())
}
unknown := httptest.NewRecorder()
unknownRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400,"updated_by":"engineer-1","secret":"must-reject"}`))
unknownRequest := httptest.NewRequest(http.MethodPost, "/api/v1/ui-settings/candidates", bytes.NewBufferString(`{"document_refresh_seconds":30,"resource_refresh_seconds":60,"stalled_after_seconds":3600,"finalizing_after_seconds":14400,"secret":"must-reject"}`))
unknownRequest.Header.Set("X-Governance-Action", "ui-settings")
handler.ServeHTTP(unknown, unknownRequest)
if unknown.Code != http.StatusBadRequest {