@@ -12,6 +12,7 @@ import (
1212 "code.gitea.io/gitea/modules/setting"
1313 "code.gitea.io/gitea/modules/templates"
1414 "code.gitea.io/gitea/services/context"
15+ "xorm.io/xorm"
1516)
1617
1718const (
@@ -42,6 +43,19 @@ func trimPortFromIP(ip string) string {
4243 return ip
4344}
4445
46+ func buildIPQuery (ctx * context.Context , keyword string ) * xorm.Session {
47+ query := db .GetEngine (ctx ).
48+ Table ("user_setting" ).
49+ Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
50+ Where ("user_setting.setting_key = ?" , user_model .SignupIP )
51+
52+ if len (keyword ) > 0 {
53+ query = query .And ("(user.lower_name LIKE ? OR user.full_name LIKE ? OR user_setting.setting_value LIKE ?)" ,
54+ "%" + strings .ToLower (keyword )+ "%" , "%" + keyword + "%" , "%" + keyword + "%" )
55+ }
56+ return query
57+ }
58+
4559// IPs show all user signup IPs
4660func IPs (ctx * context.Context ) {
4761 ctx .Data ["Title" ] = ctx .Tr ("admin.ips" )
@@ -94,59 +108,24 @@ func IPs(ctx *context.Context) {
94108 }
95109
96110 // Get the count and user IPs for pagination
97- if len (keyword ) == 0 {
98- // Simple count without keyword
99- count , err = db .GetEngine (ctx ).
100- Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
101- Where ("user_setting.setting_key = ?" , user_model .SignupIP ).
102- Count (new (user_model.Setting ))
103- if err != nil {
104- ctx .ServerError ("Count" , err )
105- return
106- }
111+ query := buildIPQuery (ctx , keyword )
107112
108- // Get the user IPs
109- err = db .GetEngine (ctx ).
110- Table ("user_setting" ).
111- Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
112- Where ("user_setting.setting_key = ?" , user_model .SignupIP ).
113- Select ("user.id as uid, user.name, user.full_name, user_setting.setting_value as ip, '' as user_agent" ).
114- OrderBy (orderBy ).
115- Limit (setting .UI .Admin .UserPagingNum , (page - 1 )* setting .UI .Admin .UserPagingNum ).
116- Find (& userIPs )
117- if err != nil {
118- ctx .ServerError ("Find" , err )
119- return
120- }
121- } else {
122- // Count with keyword filter
123- count , err = db .GetEngine (ctx ).
124- Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
125- Where ("user_setting.setting_key = ?" , user_model .SignupIP ).
126- And ("(user.lower_name LIKE ? OR user.full_name LIKE ? OR user_setting.setting_value LIKE ?)" ,
127- "%" + strings .ToLower (keyword )+ "%" , "%" + keyword + "%" , "%" + keyword + "%" ).
128- Count (new (user_model.Setting ))
129- if err != nil {
130- ctx .ServerError ("Count" , err )
131- return
132- }
113+ count , err = query .Count (new (user_model.Setting ))
114+ if err != nil {
115+ ctx .ServerError ("Count" , err )
116+ return
117+ }
133118
134- // Get the user IPs with keyword filter
135- err = db .GetEngine (ctx ).
136- Table ("user_setting" ).
137- Join ("INNER" , "user" , "user.id = user_setting.user_id" ).
138- Where ("user_setting.setting_key = ?" , user_model .SignupIP ).
139- And ("(user.lower_name LIKE ? OR user.full_name LIKE ? OR user_setting.setting_value LIKE ?)" ,
140- "%" + strings .ToLower (keyword )+ "%" , "%" + keyword + "%" , "%" + keyword + "%" ).
141- Select ("user.id as uid, user.name, user.full_name, user_setting.setting_value as ip, '' as user_agent" ).
142- OrderBy (orderBy ).
143- Limit (setting .UI .Admin .UserPagingNum , (page - 1 )* setting .UI .Admin .UserPagingNum ).
144- Find (& userIPs )
145- if err != nil {
146- ctx .ServerError ("Find" , err )
147- return
148- }
119+ err = buildIPQuery (ctx , keyword ).
120+ Select ("user.id as uid, user.name, user.full_name, user_setting.setting_value as ip" ).
121+ OrderBy (orderBy ).
122+ Limit (setting .UI .Admin .UserPagingNum , (page - 1 )* setting .UI .Admin .UserPagingNum ).
123+ Find (& userIPs )
124+ if err != nil {
125+ ctx .ServerError ("Find" , err )
126+ return
149127 }
128+
150129 for i := range userIPs {
151130 // Trim the port from the IP
152131 // FIXME: Maybe have a different helper for this?
0 commit comments