move helper funcs

This commit is contained in:
techknowlogick committed 2025-04-10 14:52:11 -04:00
1 parent 2a49d34521
commit 03af3b88c4
5 files changed
+118 -45

No files matched your search

+4 -44
View File
@@ -5,58 +5,18 @@ package admin
import (
"net/http"
"strings"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/context"
"xorm.io/xorm"
)
const (
tplIPs templates.TplName = "admin/ips/list"
)
// trimPortFromIP removes the client port from an IP address
// Handles both IPv4 and IPv6 addresses with ports
func trimPortFromIP(ip string) string {
// Handle IPv6 with brackets: [IPv6]:port
if strings.HasPrefix(ip, "[") {
// If there's no port, return as is
if !strings.Contains(ip, "]:") {
return ip
}
// Remove the port part after ]:
return strings.Split(ip, "]:")[0] + "]"
}
// Count colons to differentiate between IPv4 and IPv6
colonCount := strings.Count(ip, ":")
// Handle IPv4 with port (single colon)
if colonCount == 1 {
return strings.Split(ip, ":")[0]
}
return ip
}
func buildIPQuery(ctx *context.Context, keyword string) *xorm.Session {
query := db.GetEngine(ctx).
Table("user_setting").
Join("INNER", "user", "user.id = user_setting.user_id").
Where("user_setting.setting_key = ?", user_model.SignupIP)
if len(keyword) > 0 {
query = query.And("(user.lower_name LIKE ? OR user.full_name LIKE ? OR user_setting.setting_value LIKE ?)",
"%"+strings.ToLower(keyword)+"%", "%"+keyword+"%", "%"+keyword+"%")
}
return query
}
// IPs show all user signup IPs
func IPs(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.ips.ip")
@@ -107,7 +67,7 @@ func IPs(ctx *context.Context) {
}
// Get the count and user IPs for pagination
query := buildIPQuery(ctx, keyword)
query := user_model.BuildSignupIPQuery(ctx, keyword)
count, err = query.Count(new(user_model.Setting))
if err != nil {
@@ -115,7 +75,7 @@ func IPs(ctx *context.Context) {
return
}
err = buildIPQuery(ctx, keyword).
err = user_model.BuildSignupIPQuery(ctx, keyword).
Select("user.id as uid, user.name, user.full_name, user_setting.setting_value as ip").
OrderBy(orderBy).
Limit(setting.UI.Admin.UserPagingNum, (page-1)*setting.UI.Admin.UserPagingNum).
@@ -128,7 +88,7 @@ func IPs(ctx *context.Context) {
for i := range userIPs {
// Trim the port from the IP
// FIXME: Maybe have a different helper for this?
userIPs[i].IP = trimPortFromIP(userIPs[i].IP)
userIPs[i].IP = util.TrimPortFromIP(userIPs[i].IP)
}
ctx.Data["UserIPs"] = userIPs
+1 -1
View File
@@ -298,7 +298,7 @@ func ViewUser(ctx *context.Context) {
signupIP, err := user_model.GetUserSetting(ctx, u.ID, user_model.SignupIP)
if err == nil && len(signupIP) > 0 {
ctx.Data["HasSignupIP"] = true
ctx.Data["SignupIP"] = trimPortFromIP(signupIP)
ctx.Data["SignupIP"] = util.TrimPortFromIP(signupIP)
} else {
ctx.Data["HasSignupIP"] = false
}