-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Only use supported sort order for "explore/users" page #29430
Changes from 5 commits
7c2cb4b
f6b68be
7b996ca
e0276a8
ef01b74
3277a6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"code.gitea.io/gitea/models/db" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/base" | ||
"code.gitea.io/gitea/modules/container" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
@@ -79,10 +80,16 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, | |
fallthrough | ||
default: | ||
// in case the sortType is not valid, we set it to recentupdate | ||
sortOrder = "recentupdate" | ||
ctx.Data["SortType"] = "recentupdate" | ||
orderBy = "`user`.updated_unix DESC" | ||
} | ||
|
||
if opts.SupportedSortOrders != nil && !opts.SupportedSortOrders.Contains(sortOrder) { | ||
ctx.NotFound("unsupported sort order", nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree to use it, but at the moment Gitea doesn't have the ability to render such a page ..... It needs other framework-level refacotring first. |
||
return | ||
} | ||
|
||
opts.Keyword = ctx.FormTrim("q") | ||
opts.OrderBy = orderBy | ||
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) { | ||
|
@@ -132,8 +139,16 @@ func Users(ctx *context.Context) { | |
ctx.Data["PageIsExploreUsers"] = true | ||
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled | ||
|
||
if ctx.FormString("sort") == "" { | ||
ctx.SetFormString("sort", setting.UI.ExploreDefaultSort) | ||
supportedSortOrders := container.SetOf( | ||
"newest", | ||
"oldest", | ||
"alphabetically", | ||
"reversealphabetically", | ||
) | ||
delvh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sortOrder := ctx.FormString("sort") | ||
if sortOrder == "" { | ||
sortOrder = "newest" | ||
ctx.SetFormString("sort", sortOrder) | ||
} | ||
|
||
RenderUserSearch(ctx, &user_model.SearchUserOptions{ | ||
|
@@ -142,5 +157,7 @@ func Users(ctx *context.Context) { | |
ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum}, | ||
IsActive: util.OptionalBoolTrue, | ||
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}, | ||
|
||
SupportedSortOrders: supportedSortOrders, | ||
}, tplExploreUsers) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package integration | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/tests" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExploreUser(t *testing.T) { | ||
defer tests.PrepareTestEnv(t)() | ||
|
||
cases := []struct{ sortOrder, expected string }{ | ||
{"", "/explore/users?sort=newest&q="}, | ||
{"newest", "/explore/users?sort=newest&q="}, | ||
{"oldest", "/explore/users?sort=oldest&q="}, | ||
{"alphabetically", "/explore/users?sort=alphabetically&q="}, | ||
{"reversealphabetically", "/explore/users?sort=reversealphabetically&q="}, | ||
} | ||
for _, c := range cases { | ||
req := NewRequest(t, "GET", "/explore/users?sort="+c.sortOrder) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
h := NewHTMLParser(t, resp.Body) | ||
href, _ := h.Find(`.ui.dropdown .menu a.active.item[href^="/explore/users"]`).Attr("href") | ||
assert.Equal(t, c.expected, href) | ||
} | ||
|
||
// these sort orders shouldn't be supported, to avoid leaking user activity | ||
cases404 := []string{ | ||
"/explore/users?sort=lastlogin", | ||
"/explore/users?sort=reverselastlogin", | ||
"/explore/users?sort=leastupdate", | ||
"/explore/users?sort=reverseleastupdate", | ||
} | ||
for _, c := range cases404 { | ||
req := NewRequest(t, "GET", c).SetHeader("Accept", "text/html") | ||
MakeRequest(t, req, http.StatusNotFound) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting.UI.ExploreDefaultSort is not respected anymore!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#32357