Skip to content

Conversation

@BugsGuru
Copy link
Collaborator

@BugsGuru BugsGuru commented Jan 22, 2026

User description

关联的 issue

https://github.com/actiontech/sqle-ee/issues/2627

描述你的变更

  • 操作记录从sqle迁移至dms,并记录登入登出的操作

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 新增操作记录相关数据结构及 API 定义

  • 添加用户登录与登出操作记录功能调用

  • 集成权限校验和数据过滤功能

  • 更新 swagger 文档和路由配置


Diagram Walkthrough

flowchart LR
  A["控制器层(dms_controller)"] --> B["服务层(OperationRecordUsecase)"]
  B --> C["存储层(OperationRecordRepo)"]
  A --> D["API文档更新(swagger)"]
Loading

File Walkthrough

Relevant files
Enhancement
8 files
operation_record.go
添加操作记录数据结构和 API 定义                                                                             
+100/-0 
dms_controller.go
添加登录登出操作记录功能调用                                                                                     
+273/-0 
router.go
新增操作记录 API 路由配置                                                                                   
+5/-0     
operation_record.go
新增业务层操作记录逻辑接口                                                                                       
+54/-0   
operation_record_ce.go
添加 CE 版本操作记录占位实现                                                                                 
+28/-0   
service.go
集成 OperationRecordUsecase 注入服务                                                     
+4/-0     
model.go
新增数据库模型 OperationRecord                                                                   
+19/-0   
operation_record.go
添加操作记录存储及查询逻辑                                                                                       
+167/-0 
Documentation
2 files
swagger.json
更新 swagger 定义,新增操作记录接口                                                                     
+335/-1 
swagger.yaml
更新 swagger 定义,新增操作记录接口                                                                     
+235/-1 

- Introduced new API endpoints for adding, listing, and exporting operation records.
- Implemented data structures and validation for operation records.
- Added logging for user login and logout actions, capturing relevant details.
- Created repository and use case layers for operation record management.
- Integrated operation record functionality into the existing DMS service.
- Implemented user ID retrieval from context.
- Added admin/sys user permission checks before allowing operation record addition.
- Enhanced error handling for insufficient permissions.
- Added fields for global view permissions and accessible project names in ListOperationRecordOption.
- Implemented project filtering logic based on user permissions in ListOperationRecords and ExportOperationRecords methods.
- Ensured project administrators can only view records for their accessible projects unless global view permission is granted.
@github-actions
Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

缺少导入

在 AddSession、DelSession、oauth2Callback 和 BindOauth2User 中使用 fmt.Sprintf 格式化字符串时未导入 fmt 包,可能导致编译错误。建议检查所有使用 fmt.Sprintf 的地方并添加 fmt 的导入声明以确保代码可以正确编译。

// 记录登入操作
user, err := ctl.DMS.UserUsecase.GetUser(c.Request().Context(), reply.Data.UserUid)
if err == nil {
	recordReq := &aV1.AddOperationRecordReq{
		OperationRecord: &aV1.OperationRecord{
			OperationTime:        time.Now(),
			OperationUserName:    user.Name,
			OperationReqIP:       c.RealIP(),
			OperationUserAgent:   c.Request().UserAgent(),
			OperationTypeName:    "user",
			OperationAction:      "login",
			OperationProjectName:  "",
			OperationStatus:      "succeeded",
			OperationI18nContent: i18nPkg.ConvertStr2I18nAsDefaultLang(fmt.Sprintf("用户 %s 登入系统", user.Name)),
		},
	}
	_, err = ctl.DMS.AddOperationRecord(c.Request().Context(), recordReq)
	if err != nil {
		ctl.log.Errorf("failed to save login operation record: %v, operation_record: user_name=%s, ip=%s, user_agent=%s, action=login",
			err, user.Name, c.RealIP(), c.Request().UserAgent())
	}
}

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
提取日志记录函数

建议将记录操作日志的逻辑提取成一个独立函数,减少重复代码并便于维护。这样可以统一日志记录的方式,也能降低重复修改时引入错误的风险。

internal/apiserver/service/dms_controller.go [720-741]

-// 记录登入操作
-user, err := ctl.DMS.UserUsecase.GetUser(c.Request().Context(), reply.Data.UserUid)
-if err == nil {
+func recordUserOperation(ctl *DMSController, ctx context.Context, c echo.Context, userName, action, message string) {
     recordReq := &aV1.AddOperationRecordReq{
         OperationRecord: &aV1.OperationRecord{
             OperationTime:        time.Now(),
-            OperationUserName:    user.Name,
+            OperationUserName:    userName,
             OperationReqIP:       c.RealIP(),
             OperationUserAgent:   c.Request().UserAgent(),
             OperationTypeName:    "user",
-            OperationAction:      "login",
+            OperationAction:      action,
             OperationProjectName:  "",
             OperationStatus:      "succeeded",
-            OperationI18nContent: i18nPkg.ConvertStr2I18nAsDefaultLang(fmt.Sprintf("用户 %s 登入系统", user.Name)),
+            OperationI18nContent: i18nPkg.ConvertStr2I18nAsDefaultLang(message),
         },
     }
-    _, err = ctl.DMS.AddOperationRecord(c.Request().Context(), recordReq)
-    if err != nil {
-        ctl.log.Errorf("failed to save login operation record: %v, operation_record: user_name=%s, ip=%s, user_agent=%s, action=login",
-            err, user.Name, c.RealIP(), c.Request().UserAgent())
+    if _, err := ctl.DMS.AddOperationRecord(ctx, recordReq); err != nil {
+        ctl.log.Errorf("failed to record %s operation for %s: %v", action, userName, err)
     }
 }
 
+// 调用示例:
+user, err := ctl.DMS.UserUsecase.GetUser(c.Request().Context(), reply.Data.UserUid)
+if err == nil {
+    recordUserOperation(ctl, c.Request().Context(), c, user.Name, "login", fmt.Sprintf("用户 %s 登入系统", user.Name))
+}
+
Suggestion importance[1-10]: 6

__

Why: This suggestion refactors repeated logging logic into a separate function, which boosts maintainability and reduces duplication without altering functionality.

Low

OperationAction: "login",
OperationProjectName: "",
OperationStatus: "succeeded",
OperationI18nContent: i18nPkg.ConvertStr2I18nAsDefaultLang(fmt.Sprintf("用户 %s 通过OAuth2登入系统", user.Name)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要国际化吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前做国际化的那个版本之后就不强制要国际化了,这里的话中英文模式下都是显示默认的中文

@iwanghc iwanghc merged commit 4b505a1 into main Jan 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants