Skip to content

Commit 6f9cc61

Browse files
Add login name and source id for admin user searching API (#23376)
As title. --------- Co-authored-by: techknowlogick <[email protected]>
1 parent 27494ed commit 6f9cc61

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

models/user/search.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type SearchUserOptions struct {
2222
Keyword string
2323
Type UserType
2424
UID int64
25+
LoginName string // this option should be used only for admin user
26+
SourceID int64 // this option should be used only for admin user
2527
OrderBy db.SearchOrderBy
2628
Visible []structs.VisibleType
2729
Actor *User // The user doing the search
@@ -62,6 +64,13 @@ func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session {
6264
cond = cond.And(builder.Eq{"id": opts.UID})
6365
}
6466

67+
if opts.SourceID > 0 {
68+
cond = cond.And(builder.Eq{"login_source": opts.SourceID})
69+
}
70+
if opts.LoginName != "" {
71+
cond = cond.And(builder.Eq{"login_name": opts.LoginName})
72+
}
73+
6574
if !opts.IsActive.IsNone() {
6675
cond = cond.And(builder.Eq{"is_active": opts.IsActive.IsTrue()})
6776
}

routers/api/v1/admin/user.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,23 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
417417
ctx.Status(http.StatusNoContent)
418418
}
419419

420-
// GetAllUsers API for getting information of all the users
421-
func GetAllUsers(ctx *context.APIContext) {
422-
// swagger:operation GET /admin/users admin adminGetAllUsers
420+
// SearchUsers API for getting information of the users according the filter conditions
421+
func SearchUsers(ctx *context.APIContext) {
422+
// swagger:operation GET /admin/users admin adminSearchUsers
423423
// ---
424-
// summary: List all users
424+
// summary: Search users according filter conditions
425425
// produces:
426426
// - application/json
427427
// parameters:
428+
// - name: source_id
429+
// in: query
430+
// description: ID of the user's login source to search for
431+
// type: integer
432+
// format: int64
433+
// - name: login_name
434+
// in: query
435+
// description: user's login name to search for
436+
// type: string
428437
// - name: page
429438
// in: query
430439
// description: page number of results to return (1-based)
@@ -444,11 +453,13 @@ func GetAllUsers(ctx *context.APIContext) {
444453
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{
445454
Actor: ctx.Doer,
446455
Type: user_model.UserTypeIndividual,
456+
LoginName: ctx.FormTrim("login_name"),
457+
SourceID: ctx.FormInt64("source_id"),
447458
OrderBy: db.SearchOrderByAlphabetically,
448459
ListOptions: listOptions,
449460
})
450461
if err != nil {
451-
ctx.Error(http.StatusInternalServerError, "GetAllUsers", err)
462+
ctx.Error(http.StatusInternalServerError, "SearchUsers", err)
452463
return
453464
}
454465

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ func Routes(ctx gocontext.Context) *web.Route {
12451245
})
12461246
m.Get("/orgs", admin.GetAllOrgs)
12471247
m.Group("/users", func() {
1248-
m.Get("", admin.GetAllUsers)
1248+
m.Get("", admin.SearchUsers)
12491249
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
12501250
m.Group("/{username}", func() {
12511251
m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).

templates/swagger/v1_json.tmpl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,22 @@
488488
"tags": [
489489
"admin"
490490
],
491-
"summary": "List all users",
492-
"operationId": "adminGetAllUsers",
491+
"summary": "Search users according filter conditions",
492+
"operationId": "adminSearchUsers",
493493
"parameters": [
494+
{
495+
"type": "integer",
496+
"format": "int64",
497+
"description": "ID of the user's login source to search for",
498+
"name": "source_id",
499+
"in": "query"
500+
},
501+
{
502+
"type": "string",
503+
"description": "user's login name to search for",
504+
"name": "login_name",
505+
"in": "query"
506+
},
494507
{
495508
"type": "integer",
496509
"description": "page number of results to return (1-based)",

0 commit comments

Comments
 (0)