feat: record user in operation logs (#12596)

This commit is contained in:
ssongliu
2026-05-21 12:32:54 +08:00
committed by zhengkunwang223
parent 12f2d95265
commit db4f699b85
7 changed files with 27 additions and 0 deletions
+1
View File
@@ -7,6 +7,7 @@ import (
type OperationLog struct {
ID uint `json:"id"`
Source string `json:"source"`
User string `json:"user"`
Node string `json:"node"`
IP string `json:"ip"`
Path string `json:"path"`
+1
View File
@@ -7,6 +7,7 @@ import (
type OperationLog struct {
BaseModel
Source string `json:"source"`
User string `json:"user"`
IP string `json:"ip"`
Node string `json:"node"`
Path string `json:"path"`
+1
View File
@@ -39,6 +39,7 @@ func Init() {
migrations.AddDocSourceSetting,
migrations.AddAppStoreInstallAllowPortSetting,
migrations.AddUserManagementMenu,
migrations.AddOperationLogUser,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
+7
View File
@@ -1041,3 +1041,10 @@ var AddUserManagementMenu = &gormigrate.Migration{
return tx.Model(&model.Setting{}).Where("key = ?", "HideMenu").Update("value", string(updatedJSON)).Error
},
}
var AddOperationLogUser = &gormigrate.Migration{
ID: "20260424-add-operation-log-user",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.OperationLog{})
},
}
+15
View File
@@ -21,6 +21,7 @@ import (
"github.com/1Panel-dev/1Panel/core/cmd/server/docs"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
psessionUtils "github.com/1Panel-dev/1Panel/core/init/session/psession"
"github.com/gin-gonic/gin"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -107,6 +108,8 @@ func OperationLog() gin.HandlerFunc {
c.Next()
record.User = loadOperationUser(c)
if len(operationDic.BeforeFunctions) != 0 {
if needAgentResolve {
mergeResolvedData(writer.resolvedHeader, formatMap)
@@ -173,6 +176,18 @@ func OperationLog() gin.HandlerFunc {
}
}
func loadOperationUser(c *gin.Context) string {
sessionUser, ok := c.Get(psessionUtils.GinContextSessionUserKey)
if !ok {
return ""
}
psession, ok := sessionUser.(psessionUtils.SessionUser)
if !ok {
return ""
}
return psession.Name
}
func fillOperationDetail(operationDic *operationJson, formatMap map[string]interface{}) {
for key, value := range formatMap {
if !strings.Contains(operationDic.FormatEN, "["+key+"]") {
+1
View File
@@ -5,6 +5,7 @@ export namespace Log {
export interface OperationLog {
id: number;
source: string;
user: string;
action: string;
ip: string;
path: string;
@@ -60,6 +60,7 @@
</span>
</template>
</el-table-column>
<el-table-column :label="$t('commons.table.user')" prop="user" show-overflow-tooltip />
<el-table-column :label="$t('commons.table.operate')" min-width="150px" prop="detailZH">
<template #default="{ row }">
<span v-if="globalStore.language === 'zh' || globalStore.language === 'zh-Hant'">