-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathips.go
104 lines (87 loc) · 2.51 KB
/
ips.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package admin
import (
"net/http"
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"
)
const (
tplIPs templates.TplName = "admin/ips/list"
)
// IPs show all user signup IPs
func IPs(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.ips.ip")
ctx.Data["PageIsAdminIPs"] = true
ctx.Data["RecordUserSignupMetadata"] = setting.RecordUserSignupMetadata
// If record user signup metadata is disabled, don't show the page
if !setting.RecordUserSignupMetadata {
ctx.Redirect(setting.AppSubURL + "/-/admin")
return
}
page := ctx.FormInt("page")
if page <= 1 {
page = 1
}
// Define the user IP result struct
type UserIPResult struct {
UID int64
Name string
FullName string
IP string
}
var (
userIPs []UserIPResult
count int64
err error
orderBy string
keyword = ctx.FormTrim("q")
sortType = ctx.FormString("sort")
)
ctx.Data["SortType"] = sortType
switch sortType {
case "ip":
orderBy = "user_setting.setting_value ASC, user.id ASC"
case "reverseip":
orderBy = "user_setting.setting_value DESC, user.id DESC"
case "username":
orderBy = "user.lower_name ASC, user.id ASC"
case "reverseusername":
orderBy = "user.lower_name DESC, user.id DESC"
default:
ctx.Data["SortType"] = "ip"
orderBy = "user_setting.setting_value ASC, user.id ASC"
}
// Get the count and user IPs for pagination
query := user_model.BuildSignupIPQuery(ctx, keyword)
count, err = query.Count(new(user_model.Setting))
if err != nil {
ctx.ServerError("Count", err)
return
}
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).
Find(&userIPs)
if err != nil {
ctx.ServerError("Find", err)
return
}
for i := range userIPs {
// Trim the port from the IP
// FIXME: Maybe have a different helper for this?
userIPs[i].IP = util.TrimPortFromIP(userIPs[i].IP)
}
ctx.Data["UserIPs"] = userIPs
ctx.Data["Total"] = count
ctx.Data["Keyword"] = keyword
// Setup pagination
pager := context.NewPagination(int(count), setting.UI.Admin.UserPagingNum, page, 5)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplIPs)
}