Skip to content

Add DISABLE_ORGANIZATIONS_PAGE and DISABLE_CODE_PAGE settings for explore pages and fix an issue related to user search #32288

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

Merged
merged 17 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
18 changes: 18 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,24 @@ LEVEL = Info
;; Valid site url schemes for user profiles
;VALID_SITE_URL_SCHEMES=http,https

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;[service.explore]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Only allow signed in users to view the explore pages.
;REQUIRE_SIGNIN_VIEW = false
;;
;; Disable the users explore page.
;DISABLE_USERS_PAGE = false
;;
;; Disable the organizations explore page.
;DISABLE_ORGANIZATIONS_PAGE = false
;;
;; Disable the code explore page.
;DISABLE_CODE_PAGE = false
;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
20 changes: 20 additions & 0 deletions modules/setting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,21 @@ type RepositoryStruct struct {
OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
}

type ExplorePage struct {
RequireSigninView *config.Value[bool]
DisableUsersPage *config.Value[bool]
DisableOrganizationsPage *config.Value[bool]
DisableCodePage *config.Value[bool]
}

type ServiceStruct struct {
ExplorePage *ExplorePage
}

type ConfigStruct struct {
Picture *PictureStruct
Repository *RepositoryStruct
Service *ServiceStruct
}

var (
Expand All @@ -71,6 +83,14 @@ func initDefaultConfig() {
Repository: &RepositoryStruct{
OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
},
Service: &ServiceStruct{
ExplorePage: &ExplorePage{
RequireSigninView: config.ValueJSON[bool]("service.explore.require_signin_view").WithFileConfig(config.CfgSecKey{Sec: "service.explore", Key: "REQUIRE_SIGNIN_VIEW"}),
DisableUsersPage: config.ValueJSON[bool]("service.explore.disable_users_page").WithFileConfig(config.CfgSecKey{Sec: "service.explore", Key: "DISABLE_USERS_PAGE"}),
DisableOrganizationsPage: config.ValueJSON[bool]("service.explore.disable_organizations_page").WithFileConfig(config.CfgSecKey{Sec: "service.explore", Key: "DISABLE_ORGANIZATIONS_PAGE"}),
DisableCodePage: config.ValueJSON[bool]("service.explore.disable_code_page").WithFileConfig(config.CfgSecKey{Sec: "service.explore", Key: "DISABLE_CODE_PAGE"}),
},
},
}
}

Expand Down
8 changes: 0 additions & 8 deletions modules/setting/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ var Service = struct {
EnableOpenIDSignUp bool
OpenIDWhitelist []*regexp.Regexp
OpenIDBlacklist []*regexp.Regexp

// Explore page settings
Explore struct {
RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
} `ini:"service.explore"`
}{
AllowedUserVisibilityModesSlice: []bool{true, true, true},
}
Expand Down Expand Up @@ -234,8 +228,6 @@ func loadServiceFrom(rootCfg ConfigProvider) {
}
Service.ValidSiteURLSchemes = schemes

mustMapSetting(rootCfg, "service.explore", &Service.Explore)

loadOpenIDSetting(rootCfg)
}

Expand Down
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,10 @@ config.picture_service = Picture Service
config.disable_gravatar = Disable Gravatar
config.enable_federated_avatar = Enable Federated Avatars
config.open_with_editor_app_help = The "Open with" editors for the clone menu. If left empty, the default will be used. Expand to see the default.
config.explore_require_signin_view = Only signed-in users can view the explore pages
config.explore_disable_users_page = Disable users explore page
config.explore_disable_organizations_page = Disable organizations explore page
config.explore_disable_code_page = Disable code explore page

config.git_config = Git Configuration
config.git_disable_diff_highlight = Disable Diff Syntax Highlight
Expand Down
14 changes: 9 additions & 5 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ func reqToken() func(ctx *context.APIContext) {
}
}

func reqExploreSignIn() func(ctx *context.APIContext) {
func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) {
return func(ctx *context.APIContext) {
if setting.Service.Explore.RequireSigninView && !ctx.IsSigned {
if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) {
ctx.NotFound()
return
}
if setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx) && !ctx.IsSigned {
ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users")
}
}
Expand Down Expand Up @@ -955,16 +959,16 @@ func Routes() *web.Router {

// Users (requires user scope)
m.Group("/users", func() {
m.Get("/search", reqExploreSignIn(), user.Search)
m.Get("/search", reqExploreSignInAndUsersExploreEnabled(), user.Search)

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

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

m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignIn(), user.ListUserRepos)
m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignInAndUsersExploreEnabled(), user.ListUserRepos)
m.Group("/tokens", func() {
m.Combo("").Get(user.ListAccessTokens).
Post(bind(api.CreateAccessTokenOption{}), reqToken(), user.CreateAccessToken)
Expand Down
10 changes: 7 additions & 3 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,13 @@ func ChangeConfig(ctx *context.Context) {
return string(b), nil
}
marshallers := map[string]func(string) (string, error){
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
cfg.Service.ExplorePage.RequireSigninView.DynKey(): marshalBool,
cfg.Service.ExplorePage.DisableUsersPage.DynKey(): marshalBool,
cfg.Service.ExplorePage.DisableOrganizationsPage.DynKey(): marshalBool,
cfg.Service.ExplorePage.DisableCodePage.DynKey(): marshalBool,
}
marshaller, hasMarshaller := marshallers[key]
if !hasMarshaller {
Expand Down
7 changes: 6 additions & 1 deletion routers/web/explore/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ func Code(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/explore")
return
}
if setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) {
ctx.Redirect(setting.AppSubURL + "/explore/repos")
return
}

ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
ctx.Data["UsersIsDisabled"] = setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx)
ctx.Data["OrganizationsIsDisabled"] = setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx)
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
Expand Down
8 changes: 7 additions & 1 deletion routers/web/explore/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import (

// Organizations render explore organizations page
func Organizations(ctx *context.Context) {
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
if setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx) {
ctx.Redirect(setting.AppSubURL + "/explore/repos")
return
}

ctx.Data["UsersIsDisabled"] = setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx)
ctx.Data["CodeIsDisabled"] = setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to keep repeating assigning template variables.

There is global $.SystemConfig in all templates.

ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreOrganizations"] = true
Expand Down
4 changes: 3 additions & 1 deletion routers/web/explore/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {

// Repos render explore repositories page
func Repos(ctx *context.Context) {
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
ctx.Data["UsersIsDisabled"] = setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx)
ctx.Data["OrganizationsIsDisabled"] = setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx)
ctx.Data["CodeIsDisabled"] = setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx)
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true
Expand Down
4 changes: 3 additions & 1 deletion routers/web/explore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,

// Users render explore users page
func Users(ctx *context.Context) {
if setting.Service.Explore.DisableUsersPage {
if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) {
ctx.Redirect(setting.AppSubURL + "/explore/repos")
return
}
ctx.Data["OrganizationsIsDisabled"] = setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx)
ctx.Data["CodeIsDisabled"] = setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx)
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreUsers"] = true
Expand Down
2 changes: 1 addition & 1 deletion routers/web/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Home(ctx *context.Context) {
// HomeSitemap renders the main sitemap
func HomeSitemap(ctx *context.Context) {
m := sitemap.NewSitemapIndex()
if !setting.Service.Explore.DisableUsersPage {
if !setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) {
_, cnt, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
Type: user_model.UserTypeIndividual,
ListOptions: db.ListOptions{PageSize: 1},
Expand Down
4 changes: 3 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ func registerRoutes(m *web.Router) {
reqSignOut := verifyAuthWithOptions(&common.VerifyOptions{SignOutRequired: true})
// TODO: rename them to "optSignIn", which means that the "sign-in" could be optional, depends on the VerifyOptions (RequireSignInView)
ignSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView})
ignExploreSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView})
ignExploreSignIn := func(ctx *context.Context) {
verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx)})(ctx)
}

validation.AddBindingRules()

Expand Down
35 changes: 35 additions & 0 deletions templates/admin/config_settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,39 @@
</div>
</form>
</div>

<h4 class="ui top attached header">
{{ctx.Locale.Tr "admin.config.service_config"}}
</h4>
<div class="ui attached table segment">
<dl class="admin-dl-horizontal">
<dt>{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}</dt>
<dd>
<div class="ui toggle checkbox" data-tooltip-content="{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}">
<input type="checkbox" data-config-dyn-key="service.explore.require_signin_view" {{if .SystemConfig.Service.ExplorePage.RequireSigninView.Value ctx}}checked{{end}}><label></label>
</div>
</dd>
<div class="divider"></div>
<dt>{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}</dt>
<dd>
<div class="ui toggle checkbox" data-tooltip-content="{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}">
<input type="checkbox" data-config-dyn-key="service.explore.disable_users_page" {{if .SystemConfig.Service.ExplorePage.DisableUsersPage.Value ctx}}checked{{end}}><label></label>
</div>
</dd>
<div class="divider"></div>
<dt>{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}</dt>
<dd>
<div class="ui toggle checkbox" data-tooltip-content="{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}">
<input type="checkbox" data-config-dyn-key="service.explore.disable_organizations_page" {{if .SystemConfig.Service.ExplorePage.DisableOrganizationsPage.Value ctx}}checked{{end}}><label></label>
</div>
</dd>
<div class="divider"></div>
<dt>{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}</dt>
<dd>
<div class="ui toggle checkbox" data-tooltip-content="{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}">
<input type="checkbox" data-config-dyn-key="service.explore.disable_code_page" {{if .SystemConfig.Service.ExplorePage.DisableCodePage.Value ctx}}checked{{end}}><label></label>
</div>
</dd>
</dl>
</div>
{{template "admin/layout_footer" .}}
4 changes: 3 additions & 1 deletion templates/explore/navbar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
{{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}}
</a>
{{end}}
{{if not .OrganizationsIsDisabled}}
<a class="{{if .PageIsExploreOrganizations}}active {{end}}item" href="{{AppSubUrl}}/explore/organizations">
{{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}}
</a>
{{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled}}
{{end}}
{{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not .CodeIsDisabled))}}
<a class="{{if .PageIsExploreCode}}active {{end}}item" href="{{AppSubUrl}}/explore/code">
{{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}}
</a>
Expand Down
Loading