Skip to content

Commit d56bb74

Browse files
add admin API email endpoints (#22792)
add email endpoint to admin API to ensure API parity with admin dashboard.
1 parent 03591f0 commit d56bb74

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

modules/structs/user_email.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2023 The Gitea Authors. All rights reserved.
23
// SPDX-License-Identifier: MIT
34

45
package structs
@@ -9,6 +10,8 @@ type Email struct {
910
Email string `json:"email"`
1011
Verified bool `json:"verified"`
1112
Primary bool `json:"primary"`
13+
UserID int64 `json:"user_id"`
14+
UserName string `json:"username"`
1215
}
1316

1417
// CreateEmailOption options when creating email addresses

routers/api/v1/admin/email.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package admin
5+
6+
import (
7+
"net/http"
8+
9+
user_model "code.gitea.io/gitea/models/user"
10+
"code.gitea.io/gitea/modules/context"
11+
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/routers/api/v1/utils"
13+
"code.gitea.io/gitea/services/convert"
14+
)
15+
16+
// GetAllEmails
17+
func GetAllEmails(ctx *context.APIContext) {
18+
// swagger:operation GET /admin/emails admin adminGetAllEmails
19+
// ---
20+
// summary: List all emails
21+
// produces:
22+
// - application/json
23+
// parameters:
24+
// - name: page
25+
// in: query
26+
// description: page number of results to return (1-based)
27+
// type: integer
28+
// - name: limit
29+
// in: query
30+
// description: page size of results
31+
// type: integer
32+
// responses:
33+
// "200":
34+
// "$ref": "#/responses/EmailList"
35+
// "403":
36+
// "$ref": "#/responses/forbidden"
37+
38+
listOptions := utils.GetListOptions(ctx)
39+
40+
emails, maxResults, err := user_model.SearchEmails(&user_model.SearchEmailOptions{
41+
Keyword: ctx.Params(":email"),
42+
ListOptions: listOptions,
43+
})
44+
if err != nil {
45+
ctx.Error(http.StatusInternalServerError, "GetAllEmails", err)
46+
return
47+
}
48+
49+
results := make([]*api.Email, len(emails))
50+
for i := range emails {
51+
results[i] = convert.ToEmailSearch(emails[i])
52+
}
53+
54+
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
55+
ctx.SetTotalCountHeader(maxResults)
56+
ctx.JSON(http.StatusOK, &results)
57+
}
58+
59+
// SearchEmail
60+
func SearchEmail(ctx *context.APIContext) {
61+
// swagger:operation GET /admin/emails/search admin adminSearchEmails
62+
// ---
63+
// summary: Search all emails
64+
// produces:
65+
// - application/json
66+
// parameters:
67+
// - name: q
68+
// in: query
69+
// description: keyword
70+
// type: string
71+
// - name: page
72+
// in: query
73+
// description: page number of results to return (1-based)
74+
// type: integer
75+
// - name: limit
76+
// in: query
77+
// description: page size of results
78+
// type: integer
79+
// responses:
80+
// "200":
81+
// "$ref": "#/responses/EmailList"
82+
// "403":
83+
// "$ref": "#/responses/forbidden"
84+
85+
ctx.SetParams(":email", ctx.FormTrim("q"))
86+
GetAllEmails(ctx)
87+
}

routers/api/v1/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,10 @@ func Routes(ctx gocontext.Context) *web.Route {
12601260
m.Post("/rename", bind(api.RenameUserOption{}), admin.RenameUser)
12611261
}, context_service.UserAssignmentAPI())
12621262
})
1263+
m.Group("/emails", func() {
1264+
m.Get("", admin.GetAllEmails)
1265+
m.Get("/search", admin.SearchEmail)
1266+
})
12631267
m.Group("/unadopted", func() {
12641268
m.Get("", admin.ListUnadoptedRepositories)
12651269
m.Post("/{username}/{reponame}", admin.AdoptRepository)

services/convert/convert.go

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
3838
}
3939
}
4040

41+
// ToEmail convert models.EmailAddress to api.Email
42+
func ToEmailSearch(email *user_model.SearchEmailResult) *api.Email {
43+
return &api.Email{
44+
Email: email.Email,
45+
Verified: email.IsActivated,
46+
Primary: email.IsPrimary,
47+
UserID: email.UID,
48+
UserName: email.Name,
49+
}
50+
}
51+
4152
// ToBranch convert a git.Commit and git.Branch to an api.Branch
4253
func ToBranch(ctx context.Context, repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
4354
if bp == nil {

templates/swagger/v1_json.tmpl

+83
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,80 @@
138138
}
139139
}
140140
},
141+
"/admin/emails": {
142+
"get": {
143+
"produces": [
144+
"application/json"
145+
],
146+
"tags": [
147+
"admin"
148+
],
149+
"summary": "List all emails",
150+
"operationId": "adminGetAllEmails",
151+
"parameters": [
152+
{
153+
"type": "integer",
154+
"description": "page number of results to return (1-based)",
155+
"name": "page",
156+
"in": "query"
157+
},
158+
{
159+
"type": "integer",
160+
"description": "page size of results",
161+
"name": "limit",
162+
"in": "query"
163+
}
164+
],
165+
"responses": {
166+
"200": {
167+
"$ref": "#/responses/EmailList"
168+
},
169+
"403": {
170+
"$ref": "#/responses/forbidden"
171+
}
172+
}
173+
}
174+
},
175+
"/admin/emails/search": {
176+
"get": {
177+
"produces": [
178+
"application/json"
179+
],
180+
"tags": [
181+
"admin"
182+
],
183+
"summary": "Search all emails",
184+
"operationId": "adminSearchEmails",
185+
"parameters": [
186+
{
187+
"type": "string",
188+
"description": "keyword",
189+
"name": "q",
190+
"in": "query"
191+
},
192+
{
193+
"type": "integer",
194+
"description": "page number of results to return (1-based)",
195+
"name": "page",
196+
"in": "query"
197+
},
198+
{
199+
"type": "integer",
200+
"description": "page size of results",
201+
"name": "limit",
202+
"in": "query"
203+
}
204+
],
205+
"responses": {
206+
"200": {
207+
"$ref": "#/responses/EmailList"
208+
},
209+
"403": {
210+
"$ref": "#/responses/forbidden"
211+
}
212+
}
213+
}
214+
},
141215
"/admin/hooks": {
142216
"get": {
143217
"produces": [
@@ -16999,6 +17073,15 @@
1699917073
"type": "boolean",
1700017074
"x-go-name": "Primary"
1700117075
},
17076+
"user_id": {
17077+
"type": "integer",
17078+
"format": "int64",
17079+
"x-go-name": "UserID"
17080+
},
17081+
"username": {
17082+
"type": "string",
17083+
"x-go-name": "UserName"
17084+
},
1700217085
"verified": {
1700317086
"type": "boolean",
1700417087
"x-go-name": "Verified"

0 commit comments

Comments
 (0)