优化结构
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
integrationbiz "kra/internal/biz/integration"
|
||||
"strings"
|
||||
|
||||
"kra/internal/service/dto"
|
||||
)
|
||||
@@ -38,13 +39,13 @@ func (s *IntegrationConfigService) List(ctx context.Context, kind string) ([]*dt
|
||||
enabled = config.Enabled
|
||||
delete(byProvider, definition.Provider)
|
||||
}
|
||||
result = append(result, &dto.IntegrationConfigResponse{Kind: definition.Kind, Provider: definition.Provider, Name: definition.Name, Description: definition.Description, Enabled: enabled, Configured: configured, Config: values, Fields: integrationFieldsDTO(definition.Fields)})
|
||||
result = append(result, &dto.IntegrationConfigResponse{Kind: definition.Kind, Provider: definition.Provider, Name: definition.Name, Description: definition.Description, Enabled: enabled, Configured: configured, Config: redactIntegrationJSON(values), Fields: integrationFieldsDTO(definition.Fields)})
|
||||
}
|
||||
for _, config := range configs {
|
||||
if _, exists := byProvider[config.Provider]; !exists {
|
||||
continue
|
||||
}
|
||||
result = append(result, &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: config.Values})
|
||||
result = append(result, &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: redactIntegrationJSON(config.Values)})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -55,16 +56,44 @@ func (s *IntegrationConfigService) Find(ctx context.Context, kind, provider stri
|
||||
return nil, err
|
||||
}
|
||||
definition, found := integrationbiz.IntegrationDefinition(config.Kind, config.Provider)
|
||||
response := &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: config.Values}
|
||||
response := &dto.IntegrationConfigResponse{Kind: config.Kind, Provider: config.Provider, Name: config.Provider, Enabled: config.Enabled, Configured: true, Config: redactIntegrationJSON(config.Values)}
|
||||
if found {
|
||||
response.Name = definition.Name
|
||||
response.Description = definition.Description
|
||||
response.Fields = integrationFieldsDTO(definition.Fields)
|
||||
response.Config = mergeConfigJSON(definition.Defaults, config.Values)
|
||||
response.Config = redactIntegrationJSON(mergeConfigJSON(definition.Defaults, config.Values))
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func redactIntegrationJSON(raw json.RawMessage) json.RawMessage {
|
||||
var value any
|
||||
if json.Unmarshal(raw, &value) != nil {
|
||||
return raw
|
||||
}
|
||||
var walk func(any)
|
||||
walk = func(v any) {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
for k, item := range x {
|
||||
n := strings.ToLower(strings.ReplaceAll(k, "-", "_"))
|
||||
if strings.Contains(n, "secret") || strings.Contains(n, "password") || strings.Contains(n, "private") || strings.Contains(n, "credential") || strings.Contains(n, "token") || strings.HasSuffix(n, "_key") || strings.Contains(n, "cert") || strings.Contains(n, "p12") {
|
||||
x[k] = "******"
|
||||
} else {
|
||||
walk(item)
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range x {
|
||||
walk(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(value)
|
||||
out, _ := json.Marshal(value)
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *IntegrationConfigService) Save(ctx context.Context, kind, provider string, req *dto.IntegrationConfigRequest) error {
|
||||
if req == nil {
|
||||
return s.uc.Save(ctx, nil)
|
||||
|
||||
@@ -53,7 +53,13 @@ func (s *ExportService) IssueToken(ctx context.Context, templateID string, param
|
||||
}
|
||||
|
||||
func (s *ExportService) ConsumeToken(ctx context.Context, token string, expectBlank bool) (*ExportToken, error) {
|
||||
raw, ok, err := s.cache.Get(ctx, "export:"+token)
|
||||
key := "export:" + token
|
||||
raw, ok, err := s.cache.Get(ctx, key)
|
||||
if atomic, supported := s.cache.(interface {
|
||||
GetAndDelete(context.Context, string) (string, bool, error)
|
||||
}); supported {
|
||||
raw, ok, err = atomic.GetAndDelete(ctx, key)
|
||||
}
|
||||
if err != nil || !ok {
|
||||
return nil, ErrExportTokenInvalid
|
||||
}
|
||||
@@ -64,7 +70,11 @@ func (s *ExportService) ConsumeToken(ctx context.Context, token string, expectBl
|
||||
if value.Blank != expectBlank {
|
||||
return nil, ErrExportTokenType
|
||||
}
|
||||
_ = s.cache.Delete(ctx, "export:"+token)
|
||||
if _, supported := s.cache.(interface {
|
||||
GetAndDelete(context.Context, string) (string, bool, error)
|
||||
}); !supported {
|
||||
_ = s.cache.Delete(ctx, key)
|
||||
}
|
||||
return &ExportToken{TemplateID: value.TemplateID, Params: value.Params, Blank: value.Blank}, nil
|
||||
}
|
||||
func exportTemplateDomain(value *dto.ExportTemplateRequest) *system.ExportTemplate {
|
||||
|
||||
@@ -40,5 +40,5 @@ func convertUser(user *system.User) *dto.UserResponse {
|
||||
if user.Department != nil {
|
||||
department = departmentResponse(user.Department)
|
||||
}
|
||||
return &dto.UserResponse{ID: user.ID, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt, DeletedAt: nil, UUID: user.UUID, Username: user.Username, NickName: user.NickName, HeaderImg: user.HeaderImg, AuthorityID: user.AuthorityID, Authority: convertAuthority(user.Authority), Authorities: authorities, DeptID: user.DeptID, Department: department, Departments: departments, Positions: positions, Phone: user.Phone, Email: user.Email, Enable: user.Enable, OriginSetting: user.OriginSetting, PasswordUpdatedAt: user.PasswordUpdatedAt}
|
||||
return &dto.UserResponse{ID: user.ID, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt, UUID: user.UUID, Username: user.Username, NickName: user.NickName, HeaderImg: user.HeaderImg, AuthorityID: user.AuthorityID, Authority: convertAuthority(user.Authority), Authorities: authorities, DeptID: user.DeptID, Department: department, Departments: departments, Positions: positions, Phone: user.Phone, Email: user.Email, Enable: user.Enable, OriginSetting: user.OriginSetting, PasswordUpdatedAt: user.PasswordUpdatedAt}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user