Add login name and source id for admin user searching API (#23376)

As title.

---------

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
Lunny Xiao 2023-03-15 19:53:01 +08:00 committed by GitHub
parent 27494ed20d
commit 6f9cc617fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 8 deletions

View file

@ -22,6 +22,8 @@ type SearchUserOptions struct {
Keyword string Keyword string
Type UserType Type UserType
UID int64 UID int64
LoginName string // this option should be used only for admin user
SourceID int64 // this option should be used only for admin user
OrderBy db.SearchOrderBy OrderBy db.SearchOrderBy
Visible []structs.VisibleType Visible []structs.VisibleType
Actor *User // The user doing the search Actor *User // The user doing the search
@ -62,6 +64,13 @@ func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session {
cond = cond.And(builder.Eq{"id": opts.UID}) cond = cond.And(builder.Eq{"id": opts.UID})
} }
if opts.SourceID > 0 {
cond = cond.And(builder.Eq{"login_source": opts.SourceID})
}
if opts.LoginName != "" {
cond = cond.And(builder.Eq{"login_name": opts.LoginName})
}
if !opts.IsActive.IsNone() { if !opts.IsActive.IsNone() {
cond = cond.And(builder.Eq{"is_active": opts.IsActive.IsTrue()}) cond = cond.And(builder.Eq{"is_active": opts.IsActive.IsTrue()})
} }

View file

@ -417,14 +417,23 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }
// GetAllUsers API for getting information of all the users // SearchUsers API for getting information of the users according the filter conditions
func GetAllUsers(ctx *context.APIContext) { func SearchUsers(ctx *context.APIContext) {
// swagger:operation GET /admin/users admin adminGetAllUsers // swagger:operation GET /admin/users admin adminSearchUsers
// --- // ---
// summary: List all users // summary: Search users according filter conditions
// produces: // produces:
// - application/json // - application/json
// parameters: // parameters:
// - name: source_id
// in: query
// description: ID of the user's login source to search for
// type: integer
// format: int64
// - name: login_name
// in: query
// description: user's login name to search for
// type: string
// - name: page // - name: page
// in: query // in: query
// description: page number of results to return (1-based) // description: page number of results to return (1-based)
@ -444,11 +453,13 @@ func GetAllUsers(ctx *context.APIContext) {
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
Actor: ctx.Doer, Actor: ctx.Doer,
Type: user_model.UserTypeIndividual, Type: user_model.UserTypeIndividual,
LoginName: ctx.FormTrim("login_name"),
SourceID: ctx.FormInt64("source_id"),
OrderBy: db.SearchOrderByAlphabetically, OrderBy: db.SearchOrderByAlphabetically,
ListOptions: listOptions, ListOptions: listOptions,
}) })
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetAllUsers", err) ctx.Error(http.StatusInternalServerError, "SearchUsers", err)
return return
} }

View file

@ -1245,7 +1245,7 @@ func Routes(ctx gocontext.Context) *web.Route {
}) })
m.Get("/orgs", admin.GetAllOrgs) m.Get("/orgs", admin.GetAllOrgs)
m.Group("/users", func() { m.Group("/users", func() {
m.Get("", admin.GetAllUsers) m.Get("", admin.SearchUsers)
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser) m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
m.Group("/{username}", func() { m.Group("/{username}", func() {
m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser). m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).

View file

@ -488,9 +488,22 @@
"tags": [ "tags": [
"admin" "admin"
], ],
"summary": "List all users", "summary": "Search users according filter conditions",
"operationId": "adminGetAllUsers", "operationId": "adminSearchUsers",
"parameters": [ "parameters": [
{
"type": "integer",
"format": "int64",
"description": "ID of the user's login source to search for",
"name": "source_id",
"in": "query"
},
{
"type": "string",
"description": "user's login name to search for",
"name": "login_name",
"in": "query"
},
{ {
"type": "integer", "type": "integer",
"description": "page number of results to return (1-based)", "description": "page number of results to return (1-based)",