@@ -5,58 +5,18 @@ package admin
55
66import (
77 "net/http"
8- "strings"
98
10- "code.gitea.io/gitea/models/db"
119 user_model "code.gitea.io/gitea/models/user"
1210 "code.gitea.io/gitea/modules/setting"
1311 "code.gitea.io/gitea/modules/templates"
12+ "code.gitea.io/gitea/modules/util"
1413 "code.gitea.io/gitea/services/context"
15-
16- "xorm.io/xorm"
1714)
1815
1916const (
2017 tplIPs templates.TplName = "admin/ips/list"
2118)
2219
23- // trimPortFromIP removes the client port from an IP address
24- // Handles both IPv4 and IPv6 addresses with ports
25- func trimPortFromIP (ip string ) string {
26- // Handle IPv6 with brackets: [IPv6]:port
27- if strings .HasPrefix (ip , "[" ) {
28- // If there's no port, return as is
29- if ! strings .Contains (ip , "]:" ) {
30- return ip
31- }
32- // Remove the port part after ]:
33- return strings .Split (ip , "]:" )[0 ] + "]"
34- }
35-
36- // Count colons to differentiate between IPv4 and IPv6
37- colonCount := strings .Count (ip , ":" )
38-
39- // Handle IPv4 with port (single colon)
40- if colonCount == 1 {
41- return strings .Split (ip , ":" )[0 ]
42- }
43-
44- return ip
45- }
46-
47- func buildIPQuery (ctx * context.Context , keyword string ) * xorm.Session {
48- query := db .GetEngine (ctx ).
49- Table ("user_setting" ).
50- Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
51- Where ("user_setting.setting_key = ?" , user_model .SignupIP )
52-
53- if len (keyword ) > 0 {
54- query = query .And ("(user.lower_name LIKE ? OR user.full_name LIKE ? OR user_setting.setting_value LIKE ?)" ,
55- "%" + strings .ToLower (keyword )+ "%" , "%" + keyword + "%" , "%" + keyword + "%" )
56- }
57- return query
58- }
59-
6020// IPs show all user signup IPs
6121func IPs (ctx * context.Context ) {
6222 ctx .Data ["Title" ] = ctx .Tr ("admin.ips.ip" )
@@ -107,15 +67,15 @@ func IPs(ctx *context.Context) {
10767 }
10868
10969 // Get the count and user IPs for pagination
110- query := buildIPQuery (ctx , keyword )
70+ query := user_model . BuildSignupIPQuery (ctx , keyword )
11171
11272 count , err = query .Count (new (user_model.Setting ))
11373 if err != nil {
11474 ctx .ServerError ("Count" , err )
11575 return
11676 }
11777
118- err = buildIPQuery (ctx , keyword ).
78+ err = user_model . BuildSignupIPQuery (ctx , keyword ).
11979 Select ("user.id as uid, user.name, user.full_name, user_setting.setting_value as ip" ).
12080 OrderBy (orderBy ).
12181 Limit (setting .UI .Admin .UserPagingNum , (page - 1 )* setting .UI .Admin .UserPagingNum ).
@@ -128,7 +88,7 @@ func IPs(ctx *context.Context) {
12888 for i := range userIPs {
12989 // Trim the port from the IP
13090 // FIXME: Maybe have a different helper for this?
131- userIPs [i ].IP = trimPortFromIP (userIPs [i ].IP )
91+ userIPs [i ].IP = util . TrimPortFromIP (userIPs [i ].IP )
13292 }
13393
13494 ctx .Data ["UserIPs" ] = userIPs
0 commit comments