mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-04 05:00:25 +08:00
增加操作日志查看界面
This commit is contained in:
@@ -200,6 +200,10 @@ func (this *RPCClient) RegionProvinceRPC() pb.RegionProvinceServiceClient {
|
||||
return pb.NewRegionProvinceServiceClient(this.pickConn())
|
||||
}
|
||||
|
||||
func (this *RPCClient) LogRPC() pb.LogServiceClient {
|
||||
return pb.NewLogServiceClient(this.pickConn())
|
||||
}
|
||||
|
||||
// 构造Admin上下文
|
||||
func (this *RPCClient) Context(adminId int64) context.Context {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/models"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"net/http"
|
||||
@@ -78,17 +78,7 @@ func (this *ParentAction) AdminId() int64 {
|
||||
}
|
||||
|
||||
func (this *ParentAction) CreateLog(level string, description string, args ...interface{}) {
|
||||
rpcClient, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(this.AdminId()), &pb.CreateAdminLogRequest{
|
||||
Level: level,
|
||||
Description: fmt.Sprintf(description, args...),
|
||||
Action: this.Request.URL.Path,
|
||||
Ip: this.RequestRemoteIP(),
|
||||
})
|
||||
err := models.SharedLogDAO.CreateAdminLog(this.AdminContext(), level, this.Request.URL.Path, fmt.Sprintf(description, args...), this.RequestRemoteIP())
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/models"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
@@ -93,12 +94,7 @@ func (this *IndexAction) RunPost(params struct {
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelError,
|
||||
Description: "登录时发生系统错误:" + err.Error(),
|
||||
Action: this.Request.URL.Path,
|
||||
Ip: this.RequestRemoteIP(),
|
||||
})
|
||||
err = models.SharedLogDAO.CreateAdminLog(rpcClient.Context(0), oplogs.LevelError, this.Request.URL.Path, "登录时发生系统错误:"+err.Error(), this.RequestRemoteIP())
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
@@ -107,12 +103,7 @@ func (this *IndexAction) RunPost(params struct {
|
||||
}
|
||||
|
||||
if !resp.IsOk {
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelWarn,
|
||||
Description: "登录失败,用户名:" + params.Username,
|
||||
Action: this.Request.URL.Path,
|
||||
Ip: this.RequestRemoteIP(),
|
||||
})
|
||||
err = models.SharedLogDAO.CreateAdminLog(rpcClient.Context(0), oplogs.LevelWarn, this.Request.URL.Path, "登录失败,用户名:"+params.Username, this.RequestRemoteIP())
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
@@ -120,16 +111,11 @@ func (this *IndexAction) RunPost(params struct {
|
||||
this.Fail("请输入正确的用户名密码")
|
||||
}
|
||||
|
||||
adminId := int(resp.AdminId)
|
||||
adminId := resp.AdminId
|
||||
params.Auth.StoreAdmin(adminId, params.Remember)
|
||||
|
||||
// 记录日志
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelInfo,
|
||||
Description: "成功登录系统,用户名:" + params.Username,
|
||||
Action: this.Request.URL.Path,
|
||||
Ip: this.RequestRemoteIP(),
|
||||
})
|
||||
err = models.SharedLogDAO.CreateAdminLog(rpcClient.Context(adminId), oplogs.LevelInfo, this.Request.URL.Path, "成功登录系统,用户名:"+params.Username, this.RequestRemoteIP())
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"net/http"
|
||||
)
|
||||
@@ -15,10 +14,4 @@ func (this *Helper) BeforeAction(action *actions.ActionObject) {
|
||||
}
|
||||
|
||||
action.Data["teaMenu"] = "log"
|
||||
|
||||
selectedTabbar, _ := action.Data["mainTab"]
|
||||
|
||||
tabbar := actionutils.NewTabbar()
|
||||
tabbar.Add("日志节点", "", "/log", "", selectedTabbar == "log")
|
||||
actionutils.SetTabbar(action, tabbar)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package log
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
@@ -11,5 +16,35 @@ func (this *IndexAction) Init() {
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().LogRPC().CountLogs(this.AdminContext(), &pb.CountLogRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
count := countResp.Count
|
||||
page := this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
logsResp, err := this.RPC().LogRPC().ListLogs(this.AdminContext(), &pb.ListLogsRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
logMaps := []maps.Map{}
|
||||
for _, log := range logsResp.Logs {
|
||||
logMaps = append(logMaps, maps.Map{
|
||||
"description": log.Description,
|
||||
"userName": log.Description,
|
||||
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
|
||||
"level": log.Level,
|
||||
"type": log.Type,
|
||||
"ip": log.Ip,
|
||||
})
|
||||
}
|
||||
this.Data["logs"] = logMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
@@ -114,6 +114,11 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
|
||||
"name": "系统设置",
|
||||
"icon": "setting",
|
||||
},
|
||||
{
|
||||
"code": "log",
|
||||
"name": "操作日志",
|
||||
"icon": "history",
|
||||
},
|
||||
}
|
||||
|
||||
action.Data["teaTitle"] = teaconst.ProductNameZH
|
||||
|
||||
@@ -2,9 +2,9 @@ package helpers
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type UserShouldAuth struct {
|
||||
@@ -25,7 +25,7 @@ func (this *UserShouldAuth) BeforeAction(actionPtr actions.ActionWrapper, paramN
|
||||
}
|
||||
|
||||
// 存储用户名到SESSION
|
||||
func (this *UserShouldAuth) StoreAdmin(adminId int, remember bool) {
|
||||
func (this *UserShouldAuth) StoreAdmin(adminId int64, remember bool) {
|
||||
// 修改sid的时间
|
||||
if remember {
|
||||
cookie := &http.Cookie{
|
||||
@@ -54,7 +54,7 @@ func (this *UserShouldAuth) StoreAdmin(adminId int, remember bool) {
|
||||
}
|
||||
this.action.AddCookie(cookie)
|
||||
}
|
||||
this.action.Session().Write("adminId", strconv.Itoa(adminId))
|
||||
this.action.Session().Write("adminId", numberutils.FormatInt64(adminId))
|
||||
}
|
||||
|
||||
func (this *UserShouldAuth) IsUser() bool {
|
||||
|
||||
30
internal/web/models/log_dao.go
Normal file
30
internal/web/models/log_dao.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
var SharedLogDAO = NewLogDAO()
|
||||
|
||||
type LogDAO struct {
|
||||
}
|
||||
|
||||
func NewLogDAO() *LogDAO {
|
||||
return &LogDAO{}
|
||||
}
|
||||
|
||||
func (this *LogDAO) CreateAdminLog(ctx context.Context, level string, action string, description string, ip string) error {
|
||||
client, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.LogRPC().CreateLog(ctx, &pb.CreateLogRequest{
|
||||
Level: level,
|
||||
Description: description,
|
||||
Action: action,
|
||||
Ip: ip,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -189,7 +189,7 @@ p.margin {
|
||||
font-size: 0.9em;
|
||||
}
|
||||
.main-menu .ui.menu .sub-items .item {
|
||||
padding-left: 2.8em !important;
|
||||
padding-left: 2.82em !important;
|
||||
}
|
||||
.main-menu .ui.menu .sub-items .item.active {
|
||||
background-color: #2185d0 !important;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -138,7 +138,7 @@ div.margin, p.margin {
|
||||
|
||||
.sub-items {
|
||||
.item {
|
||||
padding-left: 2.8em !important;
|
||||
padding-left: 2.82em !important;
|
||||
}
|
||||
|
||||
.item.active {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
{$template "menu"}
|
||||
|
||||
<form method="post" class="ui form" action="/clusters/cluster">
|
||||
<form class="ui form" action="/clusters/cluster">
|
||||
<input type="hidden" name="clusterId" :value="clusterId"/>
|
||||
<div class="ui fields inline">
|
||||
<div class="ui field" v-if="groups.length > 0" style="padding-right: 0">
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
{$layout}
|
||||
|
||||
<p class="ui message">此功能暂未开放,敬请期待。</p>
|
||||
<p class="comment" v-if="logs.length == 0">暂时还没有日志。</p>
|
||||
|
||||
<table class="ui table selectable" v-for="log in logs">
|
||||
<tr>
|
||||
<td>{{log.createdTime}} <span v-if="log.userName.length > 0">@ {{log.userName}}</span> @ {{log.ip}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>{{log.description}}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<div class="page" v-html="page"></div>
|
||||
Reference in New Issue
Block a user