RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
add audit hooks (#21774)
Jin Haic552a3aa776a
diff --git a/internal/handler/tenant.go b/internal/handler/tenant.goindex fbee111bf8..e04f257e0e 100644--- a/internal/handler/tenant.go+++ b/internal/handler/tenant.go@@ -18,12 +18,14 @@ package handlerimport ("crypto/tls"+ "errors""fmt""net/http""net/mail""net/smtp""ragflow/internal/server""strings"+ "time""github.com/gin-gonic/gin"@@ -495,25 +497,48 @@ func sendMailWithTLS(addr, host string, auth smtp.Auth, from string, to []string// @Param request body service.AddMemberRequest true "Invite request"// @Router /api/v1/tenants/{tenant_id}/users [post]func (h *TenantHandler) AddTenantMember(c *gin.Context) {+ startAt := time.Now()+ operationLog := &common.OperationLog{+ EventTime: startAt,+ Operation: "set_tenant_info",+ APIPath: c.FullPath(),+ HTTPMethod: c.Request.Method,+ IPAddress: c.ClientIP(),+ }+ defer func() {+ operationLog.DurationMS = time.Since(startAt).Milliseconds()+ common.Audit(operationLog)+ }()+user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)+ operationLog.Code = int(errorCode)+ operationLog.Error = errors.New(errorMessage)return}+ operationLog.UserID = user.ID+ operationLog.UserName = user.EmailtenantID := c.Param("tenant_id")if tenantID == "" {common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errors.New("tenant_id is required")return}if !matchesActiveTenant(c, tenantID) {common.ErrorWithCode(c, common.CodeForbidden, "Tenant does not match the active token context")+ operationLog.Code = int(common.CodeForbidden)+ operationLog.Error = errors.New("tenant does not match the active token context")return}var req service.AddMemberRequestif err := c.ShouldBindJSON(&req); err != nil {common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "invalid request body: "+err.Error())+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errors.New("invalid request body: " + err.Error())return}@@ -521,6 +546,8 @@ func (h *TenantHandler) AddTenantMember(c *gin.Context) {resp, code, err := h.tenantService.AddMember(ctx, user.ID, tenantID, &req)if err != nil {common.ResponseWithCodeData(c, code, nil, err.Error())+ operationLog.Code = int(code)+ operationLog.Error = errreturn}@@ -536,6 +563,8 @@ func (h *TenantHandler) AddTenantMember(c *gin.Context) {zap.String("tenant_id", tenantID),zap.String("email", req.Email),zap.Error(err))+ operationLog.Code = int(common.CodeServerError)+ operationLog.Error = err}}@@ -549,15 +578,33 @@ func (h *TenantHandler) AddTenantMember(c *gin.Context) {// @Param request body object true "Remove member request" SchemaExample({"user_id":"string"})// @Router /api/v1/tenants/{tenant_id}/users [delete]func (h *TenantHandler) RemoveTenantMember(c *gin.Context) {+ startAt := time.Now()+ operationLog := &common.OperationLog{+ EventTime: startAt,+ Operation: "set_tenant_info",+ APIPath: c.FullPath(),+ HTTPMethod: c.Request.Method,+ IPAddress: c.ClientIP(),+ }+ defer func() {+ operationLog.DurationMS = time.Since(startAt).Milliseconds()+ common.Audit(operationLog)+ }()user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)+ operationLog.Code = int(errorCode)+ operationLog.Error = errors.New(errorMessage)return}+ operationLog.UserID = user.ID+ operationLog.UserName = user.EmailtenantID := c.Param("tenant_id")if tenantID == "" {common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errors.New("tenant_id is required")return}var body struct {@@ -565,10 +612,14 @@ func (h *TenantHandler) RemoveTenantMember(c *gin.Context) {}if err := c.ShouldBindJSON(&body); err != nil || body.UserID == "" {common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "user_id is required")+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errors.New("user_id is required")return}if body.UserID != user.ID && !matchesActiveTenant(c, tenantID) {common.ErrorWithCode(c, common.CodeForbidden, "Tenant does not match the active token context")+ operationLog.Code = int(common.CodeForbidden)+ operationLog.Error = errors.New("tenant does not match the active token context")return}@@ -576,6 +627,8 @@ func (h *TenantHandler) RemoveTenantMember(c *gin.Context) {code, err := h.tenantService.RemoveMember(ctx, user.ID, tenantID, body.UserID)if err != nil {common.ResponseWithCodeData(c, code, nil, err.Error())+ operationLog.Code = int(code)+ operationLog.Error = errreturn}common.SuccessWithData(c, true, "success")@@ -588,21 +641,42 @@ func (h *TenantHandler) RemoveTenantMember(c *gin.Context) {// @Param tenant_id path string true "Tenant ID"// @Router /api/v1/tenants/{tenant_id} [patch]func (h *TenantHandler) AcceptTenantInvite(c *gin.Context) {+ startAt := time.Now()+ operationLog := &common.OperationLog{+ EventTime: startAt,+ Operation: "set_tenant_info",+ APIPath: c.FullPath(),+ HTTPMethod: c.Request.Method,+ IPAddress: c.ClientIP(),+ }+ defer func() {+ operationLog.DurationMS = time.Since(startAt).Milliseconds()+ common.Audit(operationLog)+ }()+user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)+ operationLog.Code = int(errorCode)+ operationLog.Error = errors.New(errorMessage)return}+ operationLog.UserID = user.ID+ operationLog.UserName = user.EmailtenantID := c.Param("tenant_id")if tenantID == "" {common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "tenant_id is required")+ operationLog.Code = int(common.CodeArgumentError)+ operationLog.Error = errors.New("tenant_id is required")return}ctx := c.Request.Context()code, err := h.tenantService.AcceptInvite(ctx, user.ID, tenantID)if err != nil {common.ResponseWithCodeData(c, code, nil, err.Error())+ operationLog.Code = int(code)+ operationLog.Error = errreturn}common.SuccessWithData(c, true, "success")diff --git a/internal/handler/user.go b/internal/handler/user.goindex 876bc7693b..a60100abf9 100644--- a/internal/handler/user.go+++ b/internal/handler/user.go@@ -478,11 +478,28 @@ func (h *UserHandler) ChangePassword(c *gin.Context) {// @Router /v1/user/set_tenant_info [post]func (h *UserHandler) SetTenantInfo(c *gin.Context) {ctx := c.Request.Context()+ startAt := time.Now()+ operationLog := &common.OperationLog{+ EventTime: startAt,+ Operation: "set_tenant_info",+ APIPath: c.FullPath(),+ HTTPMethod: c.Request.Method,+ IPAddress: c.ClientIP(),+ }+ defer func() {+ operationLog.DurationMS = time.Since(startAt).Milliseconds()+ common.Audit(operationLog)+ }()+user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)+ operationLog.Code = int(errorCode)+ operationLog.Error = errors.New(errorMessage)return}+ operationLog.UserID = user.ID+ operationLog.UserName = user.EmailrequiredKeys := []string{"tenant_id", "asr_id", "embd_id", "img2txt_id", "llm_id"}missingArgumentMessage := "required argument are missing: tenant_id,asr_id,embd_id,img2txt_id,llm_id; "@@ -490,6 +507,8 @@ func (h *UserHandler) SetTenantInfo(c *gin.Context) {var payload map[string]interface{}if err := c.ShouldBindBodyWith(&payload, binding.JSON); err != nil {common.ResponseWithCodeData(c, common.CodeArgumentError, nil, missingArgumentMessage)+ operationLog.Code = int(common.CodeArgumentError)+ operationLog.Error = errreturn}@@ -501,6 +520,8 @@ func (h *UserHandler) SetTenantInfo(c *gin.Context) {}if len(missing) > 0 {common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("required argument are missing: %s; ", joinStrings(missing)))+ operationLog.Code = int(common.CodeArgumentError)+ operationLog.Error = errors.New(fmt.Sprintf("required argument are missing: %s; ", joinStrings(missing)))return}@@ -530,6 +551,8 @@ func (h *UserHandler) SetTenantInfo(c *gin.Context) {code, err := h.userService.SetTenantInfo(ctx, user.ID, &req)if err != nil {common.ResponseWithCodeData(c, code, nil, err.Error())+ operationLog.Code = int(code)+ operationLog.Error = errreturn}