Skip to content

Commit c8e5c79

Browse files
zeripath6543
andauthored
Add ui.explore settings to control view of explore pages (2) (#14094)
This is an alternative PR to #13687. Add `[ui.explore]` settings to allow restricting the explore pages to logged in users only and to disable the users explore page. The two proposed settings are: - `REQUIRE_SIGNIN_VIEW`: Only allows access to the explore pages if the user is signed in. Also restricts - `/api/v1/user/search` - `/api/v1/users/{username}` - `/api/v1/users/{username}/repos` - but does not restrict `/api/v1/users/{username}/heatmap` - `DISABLE_USERS_PAGE`: Disables the /explore/users page Fix #2908 Close #13687 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent bc423a1 commit c8e5c79

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,12 @@ relation to port exhaustion.
479479
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
480480
- `USER_DELETE_WITH_COMMENTS_MAX_TIME`: **0** Minimum amount of time a user must exist before comments are kept when the user is deleted.
481481

482+
### Service - Expore (`service.explore`)
483+
484+
- `REQUIRE_SIGNIN_VIEW`: **false**: Only allow signed in users to view the explore pages.
485+
- `DISABLE_USERS_PAGE`: **false**: Disable the users explore page.
486+
487+
482488
## SSH Minimum Key Sizes (`ssh.minimum_key_sizes`)
483489

484490
Define allowed algorithms and their minimum key length (use -1 to disable a type):

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ menu:
135135
- `ENABLE_REVERSE_PROXY_AUTO_REGISTRATION`: 允许通过反向认证做自动注册。
136136
- `ENABLE_CAPTCHA`: 注册时使用图片验证码。
137137

138+
### Service - Expore (`service.explore`)
139+
140+
- `REQUIRE_SIGNIN_VIEW`: **false**: 仅允许已登录的用户查看探索页面。
141+
- `DISABLE_USERS_PAGE`: **false**: 不显示用户探索页面。
142+
138143
## Webhook (`webhook`)
139144

140145
- `QUEUE_LENGTH`: 说明: Hook 任务队列长度。

modules/setting/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99
"time"
1010

11+
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/structs"
1213
)
1314

@@ -59,6 +60,12 @@ var Service struct {
5960
EnableOpenIDSignUp bool
6061
OpenIDWhitelist []*regexp.Regexp
6162
OpenIDBlacklist []*regexp.Regexp
63+
64+
// Explore page settings
65+
Explore struct {
66+
RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
67+
DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
68+
} `ini:"service.explore"`
6269
}
6370

6471
func newService() {
@@ -108,6 +115,10 @@ func newService() {
108115
Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
109116
Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_TIME").MustDuration(0)
110117

118+
if err := Cfg.Section("service.explore").MapTo(&Service.Explore); err != nil {
119+
log.Fatal("Failed to map service.explore settings: %v", err)
120+
}
121+
111122
sec = Cfg.Section("openid")
112123
Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock)
113124
Service.EnableOpenIDSignUp = sec.Key("ENABLE_OPENID_SIGNUP").MustBool(!Service.DisableRegistration && Service.EnableOpenIDSignIn)

routers/api/v1/api.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ func reqToken() func(ctx *context.APIContext) {
204204
}
205205
}
206206

207+
func reqExploreSignIn() func(ctx *context.APIContext) {
208+
return func(ctx *context.APIContext) {
209+
if setting.Service.Explore.RequireSigninView && !ctx.IsSigned {
210+
ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users")
211+
}
212+
}
213+
}
214+
207215
func reqBasicAuth() func(ctx *context.APIContext) {
208216
return func(ctx *context.APIContext) {
209217
if !ctx.Context.IsBasicAuth {
@@ -603,16 +611,16 @@ func Routes() *web.Route {
603611

604612
// Users
605613
m.Group("/users", func() {
606-
m.Get("/search", user.Search)
614+
m.Get("/search", reqExploreSignIn(), user.Search)
607615

608616
m.Group("/{username}", func() {
609-
m.Get("", user.GetInfo)
617+
m.Get("", reqExploreSignIn(), user.GetInfo)
610618

611619
if setting.Service.EnableUserHeatmap {
612620
m.Get("/heatmap", user.GetUserHeatmapData)
613621
}
614622

615-
m.Get("/repos", user.ListUserRepos)
623+
m.Get("/repos", reqExploreSignIn(), user.ListUserRepos)
616624
m.Group("/tokens", func() {
617625
m.Combo("").Get(user.ListAccessTokens).
618626
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)

routers/home.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
171171

172172
// ExploreRepos render explore repositories page
173173
func ExploreRepos(ctx *context.Context) {
174+
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
174175
ctx.Data["Title"] = ctx.Tr("explore")
175176
ctx.Data["PageIsExplore"] = true
176177
ctx.Data["PageIsExploreRepositories"] = true
@@ -247,6 +248,10 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
247248

248249
// ExploreUsers render explore users page
249250
func ExploreUsers(ctx *context.Context) {
251+
if setting.Service.Explore.DisableUsersPage {
252+
ctx.Redirect(setting.AppSubURL + "/explore/repos")
253+
return
254+
}
250255
ctx.Data["Title"] = ctx.Tr("explore")
251256
ctx.Data["PageIsExplore"] = true
252257
ctx.Data["PageIsExploreUsers"] = true
@@ -263,6 +268,7 @@ func ExploreUsers(ctx *context.Context) {
263268

264269
// ExploreOrganizations render explore organizations page
265270
func ExploreOrganizations(ctx *context.Context) {
271+
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
266272
ctx.Data["Title"] = ctx.Tr("explore")
267273
ctx.Data["PageIsExplore"] = true
268274
ctx.Data["PageIsExploreOrganizations"] = true
@@ -288,6 +294,7 @@ func ExploreCode(ctx *context.Context) {
288294
return
289295
}
290296

297+
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
291298
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
292299
ctx.Data["Title"] = ctx.Tr("explore")
293300
ctx.Data["PageIsExplore"] = true

routers/routes/web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ func goGet(ctx *context.Context) {
286286
func RegisterRoutes(m *web.Route) {
287287
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
288288
ignSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: setting.Service.RequireSignInView})
289+
ignExploreSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView})
289290
ignSignInAndCsrf := context.Toggle(&context.ToggleOptions{DisableCSRF: true})
290291
reqSignOut := context.Toggle(&context.ToggleOptions{SignOutRequired: true})
291292

@@ -335,7 +336,7 @@ func RegisterRoutes(m *web.Route) {
335336
m.Get("/users", routers.ExploreUsers)
336337
m.Get("/organizations", routers.ExploreOrganizations)
337338
m.Get("/code", routers.ExploreCode)
338-
}, ignSignIn)
339+
}, ignExploreSignIn)
339340
m.Get("/issues", reqSignIn, user.Issues)
340341
m.Get("/pulls", reqSignIn, user.Pulls)
341342
m.Get("/milestones", reqSignIn, reqMilestonesDashboardPageEnabled, user.Milestones)

templates/explore/navbar.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
<a class="{{if .PageIsExploreRepositories}}active{{end}} item" href="{{AppSubUrl}}/explore/repos">
33
{{svg "octicon-repo"}} {{.i18n.Tr "explore.repos"}}
44
</a>
5-
<a class="{{if .PageIsExploreUsers}}active{{end}} item" href="{{AppSubUrl}}/explore/users">
6-
{{svg "octicon-person"}} {{.i18n.Tr "explore.users"}}
7-
</a>
5+
{{if not .UsersIsDisabled}}
6+
<a class="{{if .PageIsExploreUsers}}active{{end}} item" href="{{AppSubUrl}}/explore/users">
7+
{{svg "octicon-person"}} {{.i18n.Tr "explore.users"}}
8+
</a>
9+
{{end}}
810
<a class="{{if .PageIsExploreOrganizations}}active{{end}} item" href="{{AppSubUrl}}/explore/organizations">
911
{{svg "octicon-organization"}} {{.i18n.Tr "explore.organizations"}}
1012
</a>

0 commit comments

Comments
 (0)