Skip to content

Commit

Permalink
feat: Optimize Location Information Display in System Login Logs (#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 authored Dec 20, 2024
1 parent e83d07e commit 69ad016
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
7 changes: 0 additions & 7 deletions backend/app/api/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/captcha"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -157,11 +155,6 @@ func saveLoginLogs(c *gin.Context, err error) {
logs.Status = constant.StatusSuccess
}
logs.IP = c.ClientIP()
address, err := geo.GetIPLocation(logs.IP, common.GetLang(c))
if err != nil {
global.LOG.Errorf("get ip location failed: %s", err)
}
logs.Agent = c.GetHeader("User-Agent")
logs.Address = address
_ = logService.CreateLoginLog(logs)
}
2 changes: 1 addition & 1 deletion backend/app/api/v1/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (b *BaseApi) GetLoginLogs(c *gin.Context) {
return
}

total, list, err := logService.PageLoginLog(req)
total, list, err := logService.PageLoginLog(c, req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
Expand Down
8 changes: 6 additions & 2 deletions backend/app/service/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package service

import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin"
"os"
"path"
"path/filepath"
Expand All @@ -26,7 +29,7 @@ const logs = "https://resource.fit2cloud.com/installation-log.sh"
type ILogService interface {
ListSystemLogFile() ([]string, error)
CreateLoginLog(operation model.LoginLog) error
PageLoginLog(search dto.SearchLgLogWithPage) (int64, interface{}, error)
PageLoginLog(c *gin.Context, search dto.SearchLgLogWithPage) (int64, interface{}, error)

CreateOperationLog(operation model.OperationLog) error
PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error)
Expand Down Expand Up @@ -77,7 +80,7 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
return files, nil
}

func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) {
func (u *LogService) PageLoginLog(c *gin.Context, req dto.SearchLgLogWithPage) (int64, interface{}, error) {
total, ops, err := logRepo.PageLoginLog(
req.Page,
req.PageSize,
Expand All @@ -91,6 +94,7 @@ func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface
if err := copier.Copy(&item, &op); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
item.Address, _ = geo.GetIPLocation(item.IP, common.GetLang(c))
dtoOps = append(dtoOps, item)
}
return total, dtoOps, err
Expand Down
6 changes: 3 additions & 3 deletions backend/utils/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func GetIPLocation(ip, lang string) (string, error) {
if err != nil {
return "", err
}
if lang == "en" {
return geoLocation.Country.En + " " + geoLocation.Province.En, nil
if lang == "zh" {
return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil
}
return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil
return geoLocation.Country.En + " " + geoLocation.Province.En, nil
}

0 comments on commit 69ad016

Please sign in to comment.