Skip to content

Commit 9206fbb

Browse files
Zettat123GiteaBotwxiaoguang
authored
Add DISABLE_ORGANIZATIONS_PAGE and DISABLE_CODE_PAGE settings for explore pages and fix an issue related to user search (#32288)
These settings can allow users to only display the repositories explore page. Thanks to yp05327 and wxiaoguang ! --------- Co-authored-by: Giteabot <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 3d6ccba commit 9206fbb

File tree

11 files changed

+74
-49
lines changed

11 files changed

+74
-49
lines changed

custom/conf/app.example.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,24 @@ LEVEL = Info
907907
;; Valid site url schemes for user profiles
908908
;VALID_SITE_URL_SCHEMES=http,https
909909

910+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
911+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
912+
;[service.explore]
913+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
914+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
915+
;;
916+
;; Only allow signed in users to view the explore pages.
917+
;REQUIRE_SIGNIN_VIEW = false
918+
;;
919+
;; Disable the users explore page.
920+
;DISABLE_USERS_PAGE = false
921+
;;
922+
;; Disable the organizations explore page.
923+
;DISABLE_ORGANIZATIONS_PAGE = false
924+
;;
925+
;; Disable the code explore page.
926+
;DISABLE_CODE_PAGE = false
927+
;;
910928

911929
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
912930
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

modules/setting/service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ var Service = struct {
9090

9191
// Explore page settings
9292
Explore struct {
93-
RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
94-
DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
93+
RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
94+
DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
95+
DisableOrganizationsPage bool `ini:"DISABLE_ORGANIZATIONS_PAGE"`
96+
DisableCodePage bool `ini:"DISABLE_CODE_PAGE"`
9597
} `ini:"service.explore"`
9698
}{
9799
AllowedUserVisibilityModesSlice: []bool{true, true, true},

routers/api/v1/api.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,20 @@ func reqToken() func(ctx *context.APIContext) {
356356

357357
func reqExploreSignIn() func(ctx *context.APIContext) {
358358
return func(ctx *context.APIContext) {
359-
if setting.Service.Explore.RequireSigninView && !ctx.IsSigned {
359+
if (setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView) && !ctx.IsSigned {
360360
ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users")
361361
}
362362
}
363363
}
364364

365+
func reqUsersExploreEnabled() func(ctx *context.APIContext) {
366+
return func(ctx *context.APIContext) {
367+
if setting.Service.Explore.DisableUsersPage {
368+
ctx.NotFound()
369+
}
370+
}
371+
}
372+
365373
func reqBasicOrRevProxyAuth() func(ctx *context.APIContext) {
366374
return func(ctx *context.APIContext) {
367375
if ctx.IsSigned && setting.Service.EnableReverseProxyAuthAPI && ctx.Data["AuthedMethod"].(string) == auth.ReverseProxyMethodName {
@@ -955,7 +963,7 @@ func Routes() *web.Router {
955963

956964
// Users (requires user scope)
957965
m.Group("/users", func() {
958-
m.Get("/search", reqExploreSignIn(), user.Search)
966+
m.Get("/search", reqExploreSignIn(), reqUsersExploreEnabled(), user.Search)
959967

960968
m.Group("/{username}", func() {
961969
m.Get("", reqExploreSignIn(), user.GetInfo)

routers/web/explore/code.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ const (
2121

2222
// Code render explore code page
2323
func Code(ctx *context.Context) {
24-
if !setting.Indexer.RepoIndexerEnabled {
24+
if !setting.Indexer.RepoIndexerEnabled || setting.Service.Explore.DisableCodePage {
2525
ctx.Redirect(setting.AppSubURL + "/explore")
2626
return
2727
}
2828

29-
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
29+
ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage
30+
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
3031
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
3132
ctx.Data["Title"] = ctx.Tr("explore")
3233
ctx.Data["PageIsExplore"] = true

routers/web/explore/org.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import (
1414

1515
// Organizations render explore organizations page
1616
func Organizations(ctx *context.Context) {
17-
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
17+
if setting.Service.Explore.DisableOrganizationsPage {
18+
ctx.Redirect(setting.AppSubURL + "/explore")
19+
return
20+
}
21+
22+
ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage
23+
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
1824
ctx.Data["Title"] = ctx.Tr("explore")
1925
ctx.Data["PageIsExplore"] = true
2026
ctx.Data["PageIsExploreOrganizations"] = true

routers/web/explore/repo.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
165165

166166
// Repos render explore repositories page
167167
func Repos(ctx *context.Context) {
168-
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
168+
ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage
169+
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
170+
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
169171
ctx.Data["Title"] = ctx.Tr("explore")
170172
ctx.Data["PageIsExplore"] = true
171173
ctx.Data["PageIsExploreRepositories"] = true

routers/web/explore/user.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
131131
// Users render explore users page
132132
func Users(ctx *context.Context) {
133133
if setting.Service.Explore.DisableUsersPage {
134-
ctx.Redirect(setting.AppSubURL + "/explore/repos")
134+
ctx.Redirect(setting.AppSubURL + "/explore")
135135
return
136136
}
137+
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
138+
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
137139
ctx.Data["Title"] = ctx.Tr("explore")
138140
ctx.Data["PageIsExplore"] = true
139141
ctx.Data["PageIsExploreUsers"] = true

routers/web/user/search.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,24 @@ import (
88

99
"code.gitea.io/gitea/models/db"
1010
user_model "code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/optional"
12+
"code.gitea.io/gitea/modules/setting"
1113
"code.gitea.io/gitea/services/context"
1214
"code.gitea.io/gitea/services/convert"
1315
)
1416

15-
// Search search users
16-
func Search(ctx *context.Context) {
17-
listOptions := db.ListOptions{
18-
Page: ctx.FormInt("page"),
19-
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
20-
}
21-
22-
users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
17+
// SearchCandidates searches candidate users for dropdown list
18+
func SearchCandidates(ctx *context.Context) {
19+
users, _, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
2320
Actor: ctx.Doer,
2421
Keyword: ctx.FormTrim("q"),
25-
UID: ctx.FormInt64("uid"),
2622
Type: user_model.UserTypeIndividual,
27-
IsActive: ctx.FormOptionalBool("active"),
28-
ListOptions: listOptions,
23+
IsActive: optional.Some(true),
24+
ListOptions: db.ListOptions{PageSize: setting.UI.MembersPagingNum},
2925
})
3026
if err != nil {
31-
ctx.JSON(http.StatusInternalServerError, map[string]any{
32-
"ok": false,
33-
"error": err.Error(),
34-
})
27+
ctx.ServerError("Unable to search users", err)
3528
return
3629
}
37-
38-
ctx.SetTotalCountHeader(maxResults)
39-
40-
ctx.JSON(http.StatusOK, map[string]any{
41-
"ok": true,
42-
"data": convert.ToUsers(ctx, ctx.Doer, users),
43-
})
30+
ctx.JSON(http.StatusOK, map[string]any{"data": convert.ToUsers(ctx, ctx.Doer, users)})
4431
}

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ func registerRoutes(m *web.Router) {
670670
m.Post("/forgot_password", auth.ForgotPasswdPost)
671671
m.Post("/logout", auth.SignOut)
672672
m.Get("/stopwatches", reqSignIn, user.GetStopwatches)
673-
m.Get("/search", ignExploreSignIn, user.Search)
673+
m.Get("/search_candidates", ignExploreSignIn, user.SearchCandidates)
674674
m.Group("/oauth2", func() {
675675
m.Get("/{provider}", auth.SignInOAuth)
676676
m.Get("/{provider}/callback", auth.SignInOAuthCallback)

templates/explore/navbar.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
<a class="{{if .PageIsExploreRepositories}}active {{end}}item" href="{{AppSubUrl}}/explore/repos">
44
{{svg "octicon-repo"}} {{ctx.Locale.Tr "explore.repos"}}
55
</a>
6-
{{if not .UsersIsDisabled}}
6+
{{if not .UsersPageIsDisabled}}
77
<a class="{{if .PageIsExploreUsers}}active {{end}}item" href="{{AppSubUrl}}/explore/users">
88
{{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}}
99
</a>
1010
{{end}}
11+
{{if not .OrganizationsPageIsDisabled}}
1112
<a class="{{if .PageIsExploreOrganizations}}active {{end}}item" href="{{AppSubUrl}}/explore/organizations">
1213
{{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}}
1314
</a>
14-
{{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled}}
15+
{{end}}
16+
{{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled (not .CodePageIsDisabled)}}
1517
<a class="{{if .PageIsExploreCode}}active {{end}}item" href="{{AppSubUrl}}/explore/code">
1618
{{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}}
1719
</a>

web_src/js/features/comp/SearchUserBox.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,38 @@ export function initCompSearchUserBox() {
88
const searchUserBox = document.querySelector('#search-user-box');
99
if (!searchUserBox) return;
1010

11-
const $searchUserBox = $(searchUserBox);
1211
const allowEmailInput = searchUserBox.getAttribute('data-allow-email') === 'true';
1312
const allowEmailDescription = searchUserBox.getAttribute('data-allow-email-description') ?? undefined;
14-
$searchUserBox.search({
13+
$(searchUserBox).search({
1514
minCharacters: 2,
1615
apiSettings: {
17-
url: `${appSubUrl}/user/search?active=1&q={query}`,
16+
url: `${appSubUrl}/user/search_candidates?q={query}`,
1817
onResponse(response) {
19-
const items = [];
20-
const searchQuery = $searchUserBox.find('input').val();
18+
const resultItems = [];
19+
const searchQuery = searchUserBox.querySelector('input').value;
2120
const searchQueryUppercase = searchQuery.toUpperCase();
22-
$.each(response.data, (_i, item) => {
21+
for (const item of response.data) {
2322
const resultItem = {
2423
title: item.login,
2524
image: item.avatar_url,
25+
description: htmlEscape(item.full_name),
2626
};
27-
if (item.full_name) {
28-
resultItem.description = htmlEscape(item.full_name);
29-
}
3027
if (searchQueryUppercase === item.login.toUpperCase()) {
31-
items.unshift(resultItem);
28+
resultItems.unshift(resultItem); // add the exact match to the top
3229
} else {
33-
items.push(resultItem);
30+
resultItems.push(resultItem);
3431
}
35-
});
32+
}
3633

37-
if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) {
34+
if (allowEmailInput && !resultItems.length && looksLikeEmailAddressCheck.test(searchQuery)) {
3835
const resultItem = {
3936
title: searchQuery,
4037
description: allowEmailDescription,
4138
};
42-
items.push(resultItem);
39+
resultItems.push(resultItem);
4340
}
4441

45-
return {results: items};
42+
return {results: resultItems};
4643
},
4744
},
4845
searchFields: ['login', 'full_name'],

0 commit comments

Comments
 (0)