Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable system users search via the API (#28013) #28018

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions routers/api/v1/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,33 @@

listOptions := utils.GetListOptions(ctx)

users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
Keyword: ctx.FormTrim("q"),
UID: ctx.FormInt64("uid"),
Type: user_model.UserTypeIndividual,
ListOptions: listOptions,
})
if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]any{
"ok": false,
"error": err.Error(),
uid := ctx.FormInt64("uid")
var users []*user_model.User
var maxResults int64
var err error

switch uid {
case user_model.GhostUserID:

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-sqlite

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-mysql5

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: user_model.GhostUserID (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-unit

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-mssql

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / test-mysql8

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / checks-backend

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / backend

undefined: user_model.GhostUserID

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: user_model.GhostUserID (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: user_model.GhostUserID (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: user_model.GhostUserID) (typecheck)

Check failure on line 63 in routers/api/v1/user/user.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: user_model.GhostUserID) (typecheck)
maxResults = 1
users = []*user_model.User{user_model.NewGhostUser()}
case user_model.ActionsUserID:
maxResults = 1
users = []*user_model.User{user_model.NewActionsUser()}
default:
users, maxResults, err = user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Actor: ctx.Doer,
Keyword: ctx.FormTrim("q"),
UID: uid,
Type: user_model.UserTypeIndividual,
ListOptions: listOptions,
})
return
if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]any{
"ok": false,
"error": err.Error(),
})
return
}
}

ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/api_user_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) {
}
}

func TestAPIUserSearchSystemUsers(t *testing.T) {
defer tests.PrepareTestEnv(t)()
for _, systemUser := range []*user_model.User{
user_model.NewGhostUser(),
user_model.NewActionsUser(),
} {
t.Run(systemUser.Name, func(t *testing.T) {
req := NewRequestf(t, "GET", "/api/v1/users/search?uid=%d", systemUser.ID)
resp := MakeRequest(t, req, http.StatusOK)

var results SearchResults
DecodeJSON(t, resp, &results)
assert.NotEmpty(t, results.Data)
if assert.EqualValues(t, 1, len(results.Data)) {
user := results.Data[0]
assert.EqualValues(t, user.UserName, systemUser.Name)
assert.EqualValues(t, user.ID, systemUser.ID)
}
})
}
}

func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
defer tests.PrepareTestEnv(t)()
adminUsername := "user1"
Expand Down
Loading