Skip to content

Commit

Permalink
add admin API email endpoints (#22792)
Browse files Browse the repository at this point in the history
add email endpoint to admin API to ensure API parity with admin
dashboard.
  • Loading branch information
techknowlogick authored Mar 14, 2023
1 parent 03591f0 commit d56bb74
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/structs/user_email.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package structs
Expand All @@ -9,6 +10,8 @@ type Email struct {
Email string `json:"email"`
Verified bool `json:"verified"`
Primary bool `json:"primary"`
UserID int64 `json:"user_id"`
UserName string `json:"username"`
}

// CreateEmailOption options when creating email addresses
Expand Down
87 changes: 87 additions & 0 deletions routers/api/v1/admin/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2023 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/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/convert"
)

// GetAllEmails
func GetAllEmails(ctx *context.APIContext) {
// swagger:operation GET /admin/emails admin adminGetAllEmails
// ---
// summary: List all emails
// produces:
// - application/json
// parameters:
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/EmailList"
// "403":
// "$ref": "#/responses/forbidden"

listOptions := utils.GetListOptions(ctx)

emails, maxResults, err := user_model.SearchEmails(&user_model.SearchEmailOptions{
Keyword: ctx.Params(":email"),
ListOptions: listOptions,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetAllEmails", err)
return
}

results := make([]*api.Email, len(emails))
for i := range emails {
results[i] = convert.ToEmailSearch(emails[i])
}

ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.SetTotalCountHeader(maxResults)
ctx.JSON(http.StatusOK, &results)
}

// SearchEmail
func SearchEmail(ctx *context.APIContext) {
// swagger:operation GET /admin/emails/search admin adminSearchEmails
// ---
// summary: Search all emails
// produces:
// - application/json
// parameters:
// - name: q
// in: query
// description: keyword
// type: string
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/EmailList"
// "403":
// "$ref": "#/responses/forbidden"

ctx.SetParams(":email", ctx.FormTrim("q"))
GetAllEmails(ctx)
}
4 changes: 4 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Post("/rename", bind(api.RenameUserOption{}), admin.RenameUser)
}, context_service.UserAssignmentAPI())
})
m.Group("/emails", func() {
m.Get("", admin.GetAllEmails)
m.Get("/search", admin.SearchEmail)
})
m.Group("/unadopted", func() {
m.Get("", admin.ListUnadoptedRepositories)
m.Post("/{username}/{reponame}", admin.AdoptRepository)
Expand Down
11 changes: 11 additions & 0 deletions services/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
}
}

// ToEmail convert models.EmailAddress to api.Email
func ToEmailSearch(email *user_model.SearchEmailResult) *api.Email {
return &api.Email{
Email: email.Email,
Verified: email.IsActivated,
Primary: email.IsPrimary,
UserID: email.UID,
UserName: email.Name,
}
}

// ToBranch convert a git.Commit and git.Branch to an api.Branch
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) {
if bp == nil {
Expand Down
83 changes: 83 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,80 @@
}
}
},
"/admin/emails": {
"get": {
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "List all emails",
"operationId": "adminGetAllEmails",
"parameters": [
{
"type": "integer",
"description": "page number of results to return (1-based)",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "page size of results",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"$ref": "#/responses/EmailList"
},
"403": {
"$ref": "#/responses/forbidden"
}
}
}
},
"/admin/emails/search": {
"get": {
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Search all emails",
"operationId": "adminSearchEmails",
"parameters": [
{
"type": "string",
"description": "keyword",
"name": "q",
"in": "query"
},
{
"type": "integer",
"description": "page number of results to return (1-based)",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "page size of results",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"$ref": "#/responses/EmailList"
},
"403": {
"$ref": "#/responses/forbidden"
}
}
}
},
"/admin/hooks": {
"get": {
"produces": [
Expand Down Expand Up @@ -16999,6 +17073,15 @@
"type": "boolean",
"x-go-name": "Primary"
},
"user_id": {
"type": "integer",
"format": "int64",
"x-go-name": "UserID"
},
"username": {
"type": "string",
"x-go-name": "UserName"
},
"verified": {
"type": "boolean",
"x-go-name": "Verified"
Expand Down

0 comments on commit d56bb74

Please sign in to comment.