Skip to content

Commit 1b19a75

Browse files
authored
feat: record user in operation logs (#12596)
1 parent 32867e4 commit 1b19a75

7 files changed

Lines changed: 27 additions & 0 deletions

File tree

core/app/dto/logs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type OperationLog struct {
88
ID uint `json:"id"`
99
Source string `json:"source"`
10+
User string `json:"user"`
1011
Node string `json:"node"`
1112
IP string `json:"ip"`
1213
Path string `json:"path"`

core/app/model/logs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type OperationLog struct {
88
BaseModel
99
Source string `json:"source"`
10+
User string `json:"user"`
1011
IP string `json:"ip"`
1112
Node string `json:"node"`
1213
Path string `json:"path"`

core/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Init() {
3939
migrations.AddDocSourceSetting,
4040
migrations.AddAppStoreInstallAllowPortSetting,
4141
migrations.AddUserManagementMenu,
42+
migrations.AddOperationLogUser,
4243
})
4344
if err := m.Migrate(); err != nil {
4445
global.LOG.Error(err)

core/init/migration/migrations/init.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,10 @@ var AddUserManagementMenu = &gormigrate.Migration{
10411041
return tx.Model(&model.Setting{}).Where("key = ?", "HideMenu").Update("value", string(updatedJSON)).Error
10421042
},
10431043
}
1044+
1045+
var AddOperationLogUser = &gormigrate.Migration{
1046+
ID: "20260424-add-operation-log-user",
1047+
Migrate: func(tx *gorm.DB) error {
1048+
return tx.AutoMigrate(&model.OperationLog{})
1049+
},
1050+
}

core/middleware/operation.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/1Panel-dev/1Panel/core/cmd/server/docs"
2222
"github.com/1Panel-dev/1Panel/core/constant"
2323
"github.com/1Panel-dev/1Panel/core/global"
24+
psessionUtils "github.com/1Panel-dev/1Panel/core/init/session/psession"
2425
"github.com/gin-gonic/gin"
2526
"github.com/glebarez/sqlite"
2627
"gorm.io/gorm"
@@ -107,6 +108,8 @@ func OperationLog() gin.HandlerFunc {
107108

108109
c.Next()
109110

111+
record.User = loadOperationUser(c)
112+
110113
if len(operationDic.BeforeFunctions) != 0 {
111114
if needAgentResolve {
112115
mergeResolvedData(writer.resolvedHeader, formatMap)
@@ -173,6 +176,18 @@ func OperationLog() gin.HandlerFunc {
173176
}
174177
}
175178

179+
func loadOperationUser(c *gin.Context) string {
180+
sessionUser, ok := c.Get(psessionUtils.GinContextSessionUserKey)
181+
if !ok {
182+
return ""
183+
}
184+
psession, ok := sessionUser.(psessionUtils.SessionUser)
185+
if !ok {
186+
return ""
187+
}
188+
return psession.Name
189+
}
190+
176191
func fillOperationDetail(operationDic *operationJson, formatMap map[string]interface{}) {
177192
for key, value := range formatMap {
178193
if !strings.Contains(operationDic.FormatEN, "["+key+"]") {

frontend/src/api/interface/log.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export namespace Log {
55
export interface OperationLog {
66
id: number;
77
source: string;
8+
user: string;
89
action: string;
910
ip: string;
1011
path: string;

frontend/src/views/log/operation/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
</span>
6161
</template>
6262
</el-table-column>
63+
<el-table-column :label="$t('commons.table.user')" prop="user" show-overflow-tooltip />
6364
<el-table-column :label="$t('commons.table.operate')" min-width="150px" prop="detailZH">
6465
<template #default="{ row }">
6566
<span v-if="globalStore.language === 'zh' || globalStore.language === 'zh-Hant'">

0 commit comments

Comments
 (0)