From 0b048285cb2c279c07e8134ffc575e9e2dd1c8f3 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 18 Oct 2024 09:45:52 +0800 Subject: [PATCH 01/15] add DISABLE_ORGANIZATIONS_PAGE and DISABLE_CODE_PAGE settings --- custom/conf/app.example.ini | 18 ++++++++++++++++++ modules/setting/service.go | 6 ++++-- routers/web/explore/code.go | 5 +++++ routers/web/explore/org.go | 6 ++++++ routers/web/explore/repo.go | 2 ++ routers/web/explore/user.go | 2 ++ templates/explore/navbar.tmpl | 4 +++- 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 7c7a43944f099..0c76bbc6cdeef 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -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 +;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/modules/setting/service.go b/modules/setting/service.go index 3ea1501236dfd..c858f803549d5 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -90,8 +90,10 @@ var Service = struct { // Explore page settings Explore struct { - RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"` - DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"` + RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"` + DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"` + DisableOrganizationsPage bool `ini:"DISABLE_ORGANIZATIONS_PAGE"` + DisableCodePage bool `ini:"DISABLE_CODE_PAGE"` } `ini:"service.explore"` }{ AllowedUserVisibilityModesSlice: []bool{true, true, true}, diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index ecd7c33e016f9..f4bd197f45744 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -25,8 +25,13 @@ func Code(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore") return } + if setting.Service.Explore.DisableCodePage { + ctx.Redirect(setting.AppSubURL + "/explore/repos") + return + } ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index f8fd6ec38efb6..504ab85ef4e3a 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -14,7 +14,13 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { + if setting.Service.Explore.DisableOrganizationsPage { + ctx.Redirect(setting.AppSubURL + "/explore/repos") + return + } + ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 62090e5bf4dba..dae9fca6e11d2 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -166,6 +166,8 @@ 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["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreRepositories"] = true diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 18337aff48677..d06cb664478bc 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -134,6 +134,8 @@ func Users(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } + ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreUsers"] = true diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index a157cd4b75952..f8a1510da66df 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -8,10 +8,12 @@ {{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}} {{end}} + {{if not .OrganizationsIsDisabled}} {{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}} - {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled}} + {{end}} + {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not .CodeIsDisabled))}} {{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}} From b88875bcf8d20ca76a60c83e8ae603d5ac3e76d0 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 18 Oct 2024 11:12:30 +0800 Subject: [PATCH 02/15] fix api middleware --- routers/api/v1/api.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 883e694e44b75..a1690c963ec34 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -354,8 +354,12 @@ 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.DisableUsersPage { + ctx.NotFound() + return + } if setting.Service.Explore.RequireSigninView && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } @@ -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) From c3a3bab57091b18ade9578609f0160be7dd958fe Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Sun, 20 Oct 2024 16:19:09 +0800 Subject: [PATCH 03/15] use db settings --- modules/setting/config.go | 20 ++++++++++++++++ modules/setting/service.go | 10 -------- options/locale/locale_en-US.ini | 4 ++++ routers/api/v1/api.go | 4 ++-- routers/web/admin/config.go | 10 +++++--- routers/web/explore/code.go | 6 ++--- routers/web/explore/org.go | 6 ++--- routers/web/explore/repo.go | 6 ++--- routers/web/explore/user.go | 6 ++--- routers/web/home.go | 2 +- routers/web/web.go | 4 +++- templates/admin/config_settings.tmpl | 35 ++++++++++++++++++++++++++++ 12 files changed, 84 insertions(+), 29 deletions(-) diff --git a/modules/setting/config.go b/modules/setting/config.go index 03558574c2110..d3efaa3caef9f 100644 --- a/modules/setting/config.go +++ b/modules/setting/config.go @@ -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 ( @@ -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"}), + }, + }, } } diff --git a/modules/setting/service.go b/modules/setting/service.go index c858f803549d5..622d270c6f952 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -87,14 +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"` - DisableOrganizationsPage bool `ini:"DISABLE_ORGANIZATIONS_PAGE"` - DisableCodePage bool `ini:"DISABLE_CODE_PAGE"` - } `ini:"service.explore"` }{ AllowedUserVisibilityModesSlice: []bool{true, true, true}, } @@ -236,8 +228,6 @@ func loadServiceFrom(rootCfg ConfigProvider) { } Service.ValidSiteURLSchemes = schemes - mustMapSetting(rootCfg, "service.explore", &Service.Explore) - loadOpenIDSetting(rootCfg) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a02d939b79eda..cc5177f9428e9 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -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 diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index a1690c963ec34..e7f7b973b10d1 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -356,11 +356,11 @@ func reqToken() func(ctx *context.APIContext) { func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Service.Explore.DisableUsersPage { + if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { ctx.NotFound() return } - if setting.Service.Explore.RequireSigninView && !ctx.IsSigned { + if setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx) && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index d067250a5b6b0..6d1b100b6c6dc 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -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 { diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index f4bd197f45744..a93459eacb110 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -25,13 +25,13 @@ func Code(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore") return } - if setting.Service.Explore.DisableCodePage { + if setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage - ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + 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 diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index 504ab85ef4e3a..b88c3993e8c88 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -14,13 +14,13 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { - if setting.Service.Explore.DisableOrganizationsPage { + if setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + ctx.Data["UsersIsDisabled"] = setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) + ctx.Data["CodeIsDisabled"] = setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index dae9fca6e11d2..93f1aa41356e1 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -165,9 +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["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + 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 diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index d06cb664478bc..3862404a87c12 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -130,12 +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.Service.Explore.DisableOrganizationsPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + 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 diff --git a/routers/web/home.go b/routers/web/home.go index d4be0931e850d..eadb611ec2cb5 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -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}, diff --git a/routers/web/web.go b/routers/web/web.go index f28ec82c8f0b8..2375eaeda7d71 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -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() diff --git a/templates/admin/config_settings.tmpl b/templates/admin/config_settings.tmpl index 6b9bb8275cca5..ed7b4d79dbef2 100644 --- a/templates/admin/config_settings.tmpl +++ b/templates/admin/config_settings.tmpl @@ -39,4 +39,39 @@ + +

+ {{ctx.Locale.Tr "admin.config.service_config"}} +

+
+
+
{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}
+
+
+ +
+
+
+
{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}
+
+
+ +
+
+
+
{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}
+
+
+ +
+
+
+
{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}
+
+
+ +
+
+
+
{{template "admin/layout_footer" .}} From efe0df8c2548dd852b796faeadcd0694a4a7101e Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 11:41:23 +0800 Subject: [PATCH 04/15] use .SystemConfig --- modules/setting/config.go | 6 +++--- routers/api/v1/api.go | 4 ++-- routers/web/admin/config.go | 14 +++++++------- routers/web/explore/code.go | 4 +--- routers/web/explore/org.go | 4 +--- routers/web/explore/repo.go | 3 --- routers/web/explore/user.go | 5 ++--- routers/web/home.go | 2 +- routers/web/web.go | 2 +- templates/admin/config_settings.tmpl | 8 ++++---- templates/explore/navbar.tmpl | 6 +++--- 11 files changed, 25 insertions(+), 33 deletions(-) diff --git a/modules/setting/config.go b/modules/setting/config.go index d3efaa3caef9f..88cd19eb25c9e 100644 --- a/modules/setting/config.go +++ b/modules/setting/config.go @@ -51,7 +51,7 @@ type RepositoryStruct struct { OpenWithEditorApps *config.Value[OpenWithEditorAppsType] } -type ExplorePage struct { +type Explore struct { RequireSigninView *config.Value[bool] DisableUsersPage *config.Value[bool] DisableOrganizationsPage *config.Value[bool] @@ -59,7 +59,7 @@ type ExplorePage struct { } type ServiceStruct struct { - ExplorePage *ExplorePage + Explore *Explore } type ConfigStruct struct { @@ -84,7 +84,7 @@ func initDefaultConfig() { OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"), }, Service: &ServiceStruct{ - ExplorePage: &ExplorePage{ + Explore: &Explore{ 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"}), diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index e7f7b973b10d1..0b6e37f2818b7 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -356,11 +356,11 @@ func reqToken() func(ctx *context.APIContext) { func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if setting.Config().Service.Explore.DisableUsersPage.Value(ctx) { ctx.NotFound() return } - if setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx) && !ctx.IsSigned { + if setting.Config().Service.Explore.RequireSigninView.Value(ctx) && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index 6d1b100b6c6dc..27b14d315c80d 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -231,13 +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.Service.ExplorePage.RequireSigninView.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableUsersPage.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableOrganizationsPage.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableCodePage.DynKey(): marshalBool, + cfg.Picture.DisableGravatar.DynKey(): marshalBool, + cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool, + cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps, + cfg.Service.Explore.RequireSigninView.DynKey(): marshalBool, + cfg.Service.Explore.DisableUsersPage.DynKey(): marshalBool, + cfg.Service.Explore.DisableOrganizationsPage.DynKey(): marshalBool, + cfg.Service.Explore.DisableCodePage.DynKey(): marshalBool, } marshaller, hasMarshaller := marshallers[key] if !hasMarshaller { diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index a93459eacb110..e8d87d951a423 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -25,13 +25,11 @@ func Code(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore") return } - if setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) { + if setting.Config().Service.Explore.DisableCodePage.Value(ctx) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - 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 diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index b88c3993e8c88..a655b68055b51 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -14,13 +14,11 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { - if setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx) { + if setting.Config().Service.Explore.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) ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 93f1aa41356e1..1ad958fa1152a 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -165,9 +165,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { // Repos render explore repositories page func Repos(ctx *context.Context) { - 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 diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 3862404a87c12..26b030cbcb95b 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -130,12 +130,11 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, // Users render explore users page func Users(ctx *context.Context) { - if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if setting.Config().Service.Explore.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 diff --git a/routers/web/home.go b/routers/web/home.go index eadb611ec2cb5..976429f3d710d 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -67,7 +67,7 @@ func Home(ctx *context.Context) { // HomeSitemap renders the main sitemap func HomeSitemap(ctx *context.Context) { m := sitemap.NewSitemapIndex() - if !setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if !setting.Config().Service.Explore.DisableUsersPage.Value(ctx) { _, cnt, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{ Type: user_model.UserTypeIndividual, ListOptions: db.ListOptions{PageSize: 1}, diff --git a/routers/web/web.go b/routers/web/web.go index 2375eaeda7d71..ba3acd60b7352 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -301,7 +301,7 @@ func registerRoutes(m *web.Router) { // 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 := func(ctx *context.Context) { - verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx)})(ctx) + verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.Explore.RequireSigninView.Value(ctx)})(ctx) } validation.AddBindingRules() diff --git a/templates/admin/config_settings.tmpl b/templates/admin/config_settings.tmpl index ed7b4d79dbef2..a6b035c594fcd 100644 --- a/templates/admin/config_settings.tmpl +++ b/templates/admin/config_settings.tmpl @@ -48,28 +48,28 @@
{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}
- +
diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index f8a1510da66df..4543bbb10eab5 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -3,17 +3,17 @@ {{svg "octicon-repo"}} {{ctx.Locale.Tr "explore.repos"}} - {{if not .UsersIsDisabled}} + {{if not (.SystemConfig.Service.Explore.DisableUsersPage.Value ctx)}} {{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}} {{end}} - {{if not .OrganizationsIsDisabled}} + {{if not (.SystemConfig.Service.Explore.DisableOrganizationsPage.Value ctx)}} {{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}} {{end}} - {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not .CodeIsDisabled))}} + {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not (.SystemConfig.Service.Explore.DisableCodePage.Value ctx)))}} {{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}} From b793c6bb6800f60c92a94160bbc1777bd4a4dffb Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 12:26:14 +0800 Subject: [PATCH 05/15] format --- routers/web/explore/user.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 26b030cbcb95b..2d21b7b9fdae9 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -134,7 +134,6 @@ func Users(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreUsers"] = true From 2d1ce03b8b13a1645bb007b09cd57966c485566c Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 14:24:01 +0800 Subject: [PATCH 06/15] Revert "format" This reverts commit b793c6bb6800f60c92a94160bbc1777bd4a4dffb. --- routers/web/explore/user.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 2d21b7b9fdae9..26b030cbcb95b 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -134,6 +134,7 @@ func Users(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } + ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreUsers"] = true From 6c4673dd1defa9c9271f1495056cbe403652f841 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 14:24:11 +0800 Subject: [PATCH 07/15] Revert "use .SystemConfig" This reverts commit efe0df8c2548dd852b796faeadcd0694a4a7101e. --- modules/setting/config.go | 6 +++--- routers/api/v1/api.go | 4 ++-- routers/web/admin/config.go | 14 +++++++------- routers/web/explore/code.go | 4 +++- routers/web/explore/org.go | 4 +++- routers/web/explore/repo.go | 3 +++ routers/web/explore/user.go | 5 +++-- routers/web/home.go | 2 +- routers/web/web.go | 2 +- templates/admin/config_settings.tmpl | 8 ++++---- templates/explore/navbar.tmpl | 6 +++--- 11 files changed, 33 insertions(+), 25 deletions(-) diff --git a/modules/setting/config.go b/modules/setting/config.go index 88cd19eb25c9e..d3efaa3caef9f 100644 --- a/modules/setting/config.go +++ b/modules/setting/config.go @@ -51,7 +51,7 @@ type RepositoryStruct struct { OpenWithEditorApps *config.Value[OpenWithEditorAppsType] } -type Explore struct { +type ExplorePage struct { RequireSigninView *config.Value[bool] DisableUsersPage *config.Value[bool] DisableOrganizationsPage *config.Value[bool] @@ -59,7 +59,7 @@ type Explore struct { } type ServiceStruct struct { - Explore *Explore + ExplorePage *ExplorePage } type ConfigStruct struct { @@ -84,7 +84,7 @@ func initDefaultConfig() { OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"), }, Service: &ServiceStruct{ - Explore: &Explore{ + 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"}), diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0b6e37f2818b7..e7f7b973b10d1 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -356,11 +356,11 @@ func reqToken() func(ctx *context.APIContext) { func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Config().Service.Explore.DisableUsersPage.Value(ctx) { + if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { ctx.NotFound() return } - if setting.Config().Service.Explore.RequireSigninView.Value(ctx) && !ctx.IsSigned { + if setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx) && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index 27b14d315c80d..6d1b100b6c6dc 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -231,13 +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.Service.Explore.RequireSigninView.DynKey(): marshalBool, - cfg.Service.Explore.DisableUsersPage.DynKey(): marshalBool, - cfg.Service.Explore.DisableOrganizationsPage.DynKey(): marshalBool, - cfg.Service.Explore.DisableCodePage.DynKey(): marshalBool, + 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 { diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index e8d87d951a423..a93459eacb110 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -25,11 +25,13 @@ func Code(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore") return } - if setting.Config().Service.Explore.DisableCodePage.Value(ctx) { + if setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } + 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 diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index a655b68055b51..b88c3993e8c88 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -14,11 +14,13 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { - if setting.Config().Service.Explore.DisableOrganizationsPage.Value(ctx) { + 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) ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 1ad958fa1152a..93f1aa41356e1 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -165,6 +165,9 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { // Repos render explore repositories page func Repos(ctx *context.Context) { + 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 diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 26b030cbcb95b..3862404a87c12 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -130,11 +130,12 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, // Users render explore users page func Users(ctx *context.Context) { - if setting.Config().Service.Explore.DisableUsersPage.Value(ctx) { + 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 diff --git a/routers/web/home.go b/routers/web/home.go index 976429f3d710d..eadb611ec2cb5 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -67,7 +67,7 @@ func Home(ctx *context.Context) { // HomeSitemap renders the main sitemap func HomeSitemap(ctx *context.Context) { m := sitemap.NewSitemapIndex() - if !setting.Config().Service.Explore.DisableUsersPage.Value(ctx) { + 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}, diff --git a/routers/web/web.go b/routers/web/web.go index ba3acd60b7352..2375eaeda7d71 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -301,7 +301,7 @@ func registerRoutes(m *web.Router) { // 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 := func(ctx *context.Context) { - verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.Explore.RequireSigninView.Value(ctx)})(ctx) + verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx)})(ctx) } validation.AddBindingRules() diff --git a/templates/admin/config_settings.tmpl b/templates/admin/config_settings.tmpl index a6b035c594fcd..ed7b4d79dbef2 100644 --- a/templates/admin/config_settings.tmpl +++ b/templates/admin/config_settings.tmpl @@ -48,28 +48,28 @@
{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}
- +
{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}
- +
diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index 4543bbb10eab5..f8a1510da66df 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -3,17 +3,17 @@ {{svg "octicon-repo"}} {{ctx.Locale.Tr "explore.repos"}} - {{if not (.SystemConfig.Service.Explore.DisableUsersPage.Value ctx)}} + {{if not .UsersIsDisabled}} {{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}} {{end}} - {{if not (.SystemConfig.Service.Explore.DisableOrganizationsPage.Value ctx)}} + {{if not .OrganizationsIsDisabled}} {{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}} {{end}} - {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not (.SystemConfig.Service.Explore.DisableCodePage.Value ctx)))}} + {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not .CodeIsDisabled))}} {{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}} From ce6eeabd0264e09ec25ff3bfbe1f473056dcf6b2 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 14:24:20 +0800 Subject: [PATCH 08/15] Revert "use db settings" This reverts commit c3a3bab57091b18ade9578609f0160be7dd958fe. --- modules/setting/config.go | 20 ---------------- modules/setting/service.go | 10 ++++++++ options/locale/locale_en-US.ini | 4 ---- routers/api/v1/api.go | 4 ++-- routers/web/admin/config.go | 10 +++----- routers/web/explore/code.go | 6 ++--- routers/web/explore/org.go | 6 ++--- routers/web/explore/repo.go | 6 ++--- routers/web/explore/user.go | 6 ++--- routers/web/home.go | 2 +- routers/web/web.go | 4 +--- templates/admin/config_settings.tmpl | 35 ---------------------------- 12 files changed, 29 insertions(+), 84 deletions(-) diff --git a/modules/setting/config.go b/modules/setting/config.go index d3efaa3caef9f..03558574c2110 100644 --- a/modules/setting/config.go +++ b/modules/setting/config.go @@ -51,21 +51,9 @@ 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 ( @@ -83,14 +71,6 @@ 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"}), - }, - }, } } diff --git a/modules/setting/service.go b/modules/setting/service.go index 622d270c6f952..c858f803549d5 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -87,6 +87,14 @@ 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"` + DisableOrganizationsPage bool `ini:"DISABLE_ORGANIZATIONS_PAGE"` + DisableCodePage bool `ini:"DISABLE_CODE_PAGE"` + } `ini:"service.explore"` }{ AllowedUserVisibilityModesSlice: []bool{true, true, true}, } @@ -228,6 +236,8 @@ func loadServiceFrom(rootCfg ConfigProvider) { } Service.ValidSiteURLSchemes = schemes + mustMapSetting(rootCfg, "service.explore", &Service.Explore) + loadOpenIDSetting(rootCfg) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index cc5177f9428e9..a02d939b79eda 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3300,10 +3300,6 @@ 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 diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index e7f7b973b10d1..a1690c963ec34 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -356,11 +356,11 @@ func reqToken() func(ctx *context.APIContext) { func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if setting.Service.Explore.DisableUsersPage { ctx.NotFound() return } - if setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx) && !ctx.IsSigned { + if setting.Service.Explore.RequireSigninView && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index 6d1b100b6c6dc..d067250a5b6b0 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -231,13 +231,9 @@ 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.Service.ExplorePage.RequireSigninView.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableUsersPage.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableOrganizationsPage.DynKey(): marshalBool, - cfg.Service.ExplorePage.DisableCodePage.DynKey(): marshalBool, + cfg.Picture.DisableGravatar.DynKey(): marshalBool, + cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool, + cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps, } marshaller, hasMarshaller := marshallers[key] if !hasMarshaller { diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index a93459eacb110..f4bd197f45744 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -25,13 +25,13 @@ func Code(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore") return } - if setting.Config().Service.ExplorePage.DisableCodePage.Value(ctx) { + if setting.Service.Explore.DisableCodePage { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - ctx.Data["UsersIsDisabled"] = setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) - ctx.Data["OrganizationsIsDisabled"] = setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx) + ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index b88c3993e8c88..504ab85ef4e3a 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -14,13 +14,13 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { - if setting.Config().Service.ExplorePage.DisableOrganizationsPage.Value(ctx) { + if setting.Service.Explore.DisableOrganizationsPage { 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) + ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 93f1aa41356e1..dae9fca6e11d2 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -165,9 +165,9 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { // Repos render explore repositories page func Repos(ctx *context.Context) { - 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["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreRepositories"] = true diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 3862404a87c12..d06cb664478bc 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -130,12 +130,12 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, // Users render explore users page func Users(ctx *context.Context) { - if setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if setting.Service.Explore.DisableUsersPage { 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["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreUsers"] = true diff --git a/routers/web/home.go b/routers/web/home.go index eadb611ec2cb5..d4be0931e850d 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -67,7 +67,7 @@ func Home(ctx *context.Context) { // HomeSitemap renders the main sitemap func HomeSitemap(ctx *context.Context) { m := sitemap.NewSitemapIndex() - if !setting.Config().Service.ExplorePage.DisableUsersPage.Value(ctx) { + if !setting.Service.Explore.DisableUsersPage { _, cnt, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{ Type: user_model.UserTypeIndividual, ListOptions: db.ListOptions{PageSize: 1}, diff --git a/routers/web/web.go b/routers/web/web.go index 2375eaeda7d71..f28ec82c8f0b8 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -300,9 +300,7 @@ 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 := func(ctx *context.Context) { - verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Config().Service.ExplorePage.RequireSigninView.Value(ctx)})(ctx) - } + ignExploreSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView}) validation.AddBindingRules() diff --git a/templates/admin/config_settings.tmpl b/templates/admin/config_settings.tmpl index ed7b4d79dbef2..6b9bb8275cca5 100644 --- a/templates/admin/config_settings.tmpl +++ b/templates/admin/config_settings.tmpl @@ -39,39 +39,4 @@ - -

- {{ctx.Locale.Tr "admin.config.service_config"}} -

-
-
-
{{ctx.Locale.Tr "admin.config.explore_require_signin_view"}}
-
-
- -
-
-
-
{{ctx.Locale.Tr "admin.config.explore_disable_users_page"}}
-
-
- -
-
-
-
{{ctx.Locale.Tr "admin.config.explore_disable_organizations_page"}}
-
-
- -
-
-
-
{{ctx.Locale.Tr "admin.config.explore_disable_code_page"}}
-
-
- -
-
-
-
{{template "admin/layout_footer" .}} From 7314a2788fd302873ad61c8cae58721fb529a4bc Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 15:16:54 +0800 Subject: [PATCH 09/15] fix reqExploreSignIn --- routers/api/v1/api.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index a1690c963ec34..ab582c9fc201f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -354,18 +354,22 @@ func reqToken() func(ctx *context.APIContext) { } } -func reqExploreSignInAndUsersExploreEnabled() func(ctx *context.APIContext) { +func reqExploreSignIn() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Service.Explore.DisableUsersPage { - ctx.NotFound() - return - } if setting.Service.Explore.RequireSigninView && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } } +func reqUsersExploreEnabled() func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + if setting.Service.Explore.DisableUsersPage { + ctx.NotFound() + } + } +} + func reqBasicOrRevProxyAuth() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { if ctx.IsSigned && setting.Service.EnableReverseProxyAuthAPI && ctx.Data["AuthedMethod"].(string) == auth.ReverseProxyMethodName { @@ -959,16 +963,16 @@ func Routes() *web.Router { // Users (requires user scope) m.Group("/users", func() { - m.Get("/search", reqExploreSignInAndUsersExploreEnabled(), user.Search) + m.Get("/search", reqExploreSignIn(), reqUsersExploreEnabled(), user.Search) m.Group("/{username}", func() { - m.Get("", reqExploreSignInAndUsersExploreEnabled(), user.GetInfo) + m.Get("", reqExploreSignIn(), user.GetInfo) if setting.Service.EnableUserHeatmap { m.Get("/heatmap", user.GetUserHeatmapData) } - m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignInAndUsersExploreEnabled(), user.ListUserRepos) + m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignIn(), user.ListUserRepos) m.Group("/tokens", func() { m.Combo("").Get(user.ListAccessTokens). Post(bind(api.CreateAccessTokenOption{}), reqToken(), user.CreateAccessToken) From 0658fa66986a10631b8f10d1495731d377fd7846 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 15:24:10 +0800 Subject: [PATCH 10/15] fix and --- templates/explore/navbar.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index f8a1510da66df..e30f61e778be1 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -13,7 +13,7 @@ {{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}} {{end}} - {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) (and .IsRepoIndexerEnabled (not .CodeIsDisabled))}} + {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled (not .CodeIsDisabled)}} {{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}} From 23364515f5232939b91c025fc80303bbbf8f1f6c Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 15:34:31 +0800 Subject: [PATCH 11/15] improve var names --- routers/web/explore/code.go | 4 ++-- routers/web/explore/org.go | 4 ++-- routers/web/explore/repo.go | 6 +++--- routers/web/explore/user.go | 4 ++-- templates/explore/navbar.tmpl | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index f4bd197f45744..c10852a7fc8df 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -30,8 +30,8 @@ func Code(ctx *context.Context) { return } - ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage - ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index 504ab85ef4e3a..aa8d7498568a7 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -19,8 +19,8 @@ func Organizations(ctx *context.Context) { return } - ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreOrganizations"] = true diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index dae9fca6e11d2..5b6f612e72201 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -165,9 +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["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage + ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreRepositories"] = true diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index d06cb664478bc..200b488b6ca21 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -134,8 +134,8 @@ func Users(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") return } - ctx.Data["OrganizationsIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage - ctx.Data["CodeIsDisabled"] = setting.Service.Explore.DisableCodePage + ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage + ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage ctx.Data["Title"] = ctx.Tr("explore") ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExploreUsers"] = true diff --git a/templates/explore/navbar.tmpl b/templates/explore/navbar.tmpl index e30f61e778be1..6b595af63acd4 100644 --- a/templates/explore/navbar.tmpl +++ b/templates/explore/navbar.tmpl @@ -3,17 +3,17 @@ {{svg "octicon-repo"}} {{ctx.Locale.Tr "explore.repos"}} - {{if not .UsersIsDisabled}} + {{if not .UsersPageIsDisabled}} {{svg "octicon-person"}} {{ctx.Locale.Tr "explore.users"}} {{end}} - {{if not .OrganizationsIsDisabled}} + {{if not .OrganizationsPageIsDisabled}} {{svg "octicon-organization"}} {{ctx.Locale.Tr "explore.organizations"}} {{end}} - {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled (not .CodeIsDisabled)}} + {{if and (not ctx.Consts.RepoUnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled (not .CodePageIsDisabled)}} {{svg "octicon-code"}} {{ctx.Locale.Tr "explore.code"}} From b1b8e3e2afe4002bcc672c5905827bfc85b5031e Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 21 Oct 2024 15:42:58 +0800 Subject: [PATCH 12/15] fix redirect --- routers/web/explore/code.go | 6 +----- routers/web/explore/org.go | 2 +- routers/web/explore/user.go | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index c10852a7fc8df..48f890332bd33 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -21,14 +21,10 @@ const ( // Code render explore code page func Code(ctx *context.Context) { - if !setting.Indexer.RepoIndexerEnabled { + if !setting.Indexer.RepoIndexerEnabled || setting.Service.Explore.DisableCodePage { ctx.Redirect(setting.AppSubURL + "/explore") return } - if setting.Service.Explore.DisableCodePage { - ctx.Redirect(setting.AppSubURL + "/explore/repos") - return - } ctx.Data["UsersPageIsDisabled"] = setting.Service.Explore.DisableUsersPage ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index aa8d7498568a7..7178630b64e95 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -15,7 +15,7 @@ import ( // Organizations render explore organizations page func Organizations(ctx *context.Context) { if setting.Service.Explore.DisableOrganizationsPage { - ctx.Redirect(setting.AppSubURL + "/explore/repos") + ctx.Redirect(setting.AppSubURL + "/explore") return } diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 200b488b6ca21..5f6b8fcee6103 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -131,7 +131,7 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, // Users render explore users page func Users(ctx *context.Context) { if setting.Service.Explore.DisableUsersPage { - ctx.Redirect(setting.AppSubURL + "/explore/repos") + ctx.Redirect(setting.AppSubURL + "/explore") return } ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage From 03292a8912a87bb9eab1229fa44b284d2f22acce Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 22 Oct 2024 09:19:01 +0800 Subject: [PATCH 13/15] fix reqExploreSignIn --- routers/api/v1/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ab582c9fc201f..bfc601c8356ae 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -356,7 +356,7 @@ func reqToken() func(ctx *context.APIContext) { func reqExploreSignIn() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { - if setting.Service.Explore.RequireSigninView && !ctx.IsSigned { + if (setting.Service.RequireSignInView || setting.Service.Explore.RequireSigninView) && !ctx.IsSigned { ctx.Error(http.StatusUnauthorized, "reqExploreSignIn", "you must be signed in to search for users") } } From ea0277867db5ea996f4faea9018ddb5d10b1493b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 22 Oct 2024 12:32:07 +0800 Subject: [PATCH 14/15] fix /user/search --- routers/web/user/search.go | 31 +++++++---------------- routers/web/web.go | 2 +- web_src/js/features/comp/SearchUserBox.ts | 27 +++++++++----------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/routers/web/user/search.go b/routers/web/user/search.go index fb7729bbe1419..5aa46c12ec6f8 100644 --- a/routers/web/user/search.go +++ b/routers/web/user/search.go @@ -4,6 +4,8 @@ package user import ( + "code.gitea.io/gitea/modules/optional" + "code.gitea.io/gitea/modules/setting" "net/http" "code.gitea.io/gitea/models/db" @@ -12,33 +14,18 @@ import ( "code.gitea.io/gitea/services/convert" ) -// Search search users -func Search(ctx *context.Context) { - listOptions := db.ListOptions{ - Page: ctx.FormInt("page"), - PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), - } - - users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{ +// SearchCandidates searches candidate users for dropdown list +func SearchCandidates(ctx *context.Context) { + users, _, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{ Actor: ctx.Doer, Keyword: ctx.FormTrim("q"), - UID: ctx.FormInt64("uid"), Type: user_model.UserTypeIndividual, - IsActive: ctx.FormOptionalBool("active"), - ListOptions: listOptions, + IsActive: optional.Some(true), + ListOptions: db.ListOptions{PageSize: setting.UI.MembersPagingNum}, }) if err != nil { - ctx.JSON(http.StatusInternalServerError, map[string]any{ - "ok": false, - "error": err.Error(), - }) + ctx.ServerError("Unable to search users", err) return } - - ctx.SetTotalCountHeader(maxResults) - - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - "data": convert.ToUsers(ctx, ctx.Doer, users), - }) + ctx.JSON(http.StatusOK, map[string]any{"data": convert.ToUsers(ctx, ctx.Doer, users)}) } diff --git a/routers/web/web.go b/routers/web/web.go index f28ec82c8f0b8..a6ccb7a7924e9 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -670,7 +670,7 @@ func registerRoutes(m *web.Router) { m.Post("/forgot_password", auth.ForgotPasswdPost) m.Post("/logout", auth.SignOut) m.Get("/stopwatches", reqSignIn, user.GetStopwatches) - m.Get("/search", ignExploreSignIn, user.Search) + m.Get("/search_candidates", ignExploreSignIn, user.SearchCandidates) m.Group("/oauth2", func() { m.Get("/{provider}", auth.SignInOAuth) m.Get("/{provider}/callback", auth.SignInOAuthCallback) diff --git a/web_src/js/features/comp/SearchUserBox.ts b/web_src/js/features/comp/SearchUserBox.ts index 7ef23fe4b02c8..ceb756b557beb 100644 --- a/web_src/js/features/comp/SearchUserBox.ts +++ b/web_src/js/features/comp/SearchUserBox.ts @@ -8,41 +8,38 @@ export function initCompSearchUserBox() { const searchUserBox = document.querySelector('#search-user-box'); if (!searchUserBox) return; - const $searchUserBox = $(searchUserBox); const allowEmailInput = searchUserBox.getAttribute('data-allow-email') === 'true'; const allowEmailDescription = searchUserBox.getAttribute('data-allow-email-description') ?? undefined; - $searchUserBox.search({ + $(searchUserBox).search({ minCharacters: 2, apiSettings: { - url: `${appSubUrl}/user/search?active=1&q={query}`, + url: `${appSubUrl}/user/search_candidates?q={query}`, onResponse(response) { - const items = []; - const searchQuery = $searchUserBox.find('input').val(); + const resultItems = []; + const searchQuery = searchUserBox.querySelector('input').value; const searchQueryUppercase = searchQuery.toUpperCase(); - $.each(response.data, (_i, item) => { + for (const item of response.data) { const resultItem = { title: item.login, image: item.avatar_url, + description: htmlEscape(item.full_name), }; - if (item.full_name) { - resultItem.description = htmlEscape(item.full_name); - } if (searchQueryUppercase === item.login.toUpperCase()) { - items.unshift(resultItem); + resultItems.unshift(resultItem); // add the exact match to the top } else { - items.push(resultItem); + resultItems.push(resultItem); } - }); + } - if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) { + if (allowEmailInput && !resultItems.length && looksLikeEmailAddressCheck.test(searchQuery)) { const resultItem = { title: searchQuery, description: allowEmailDescription, }; - items.push(resultItem); + resultItems.push(resultItem); } - return {results: items}; + return {results: resultItems}; }, }, searchFields: ['login', 'full_name'], From fa79343322d0e433b2cc382676e5e2813bbef7fa Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 22 Oct 2024 12:37:18 +0800 Subject: [PATCH 15/15] fix lint --- routers/web/user/search.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/user/search.go b/routers/web/user/search.go index 5aa46c12ec6f8..be5eee90a971a 100644 --- a/routers/web/user/search.go +++ b/routers/web/user/search.go @@ -4,12 +4,12 @@ package user import ( - "code.gitea.io/gitea/modules/optional" - "code.gitea.io/gitea/modules/setting" "net/http" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/optional" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/convert" )