From d6470427f65b7e6c8725b15ad1c2962ba55a512b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 30 Jul 2022 13:27:29 +0800 Subject: [PATCH 1/8] Allow disalbe part user settings --- custom/conf/app.example.ini | 4 ++ modules/setting/setting.go | 2 + modules/setting/user.go | 34 +++++++++++ routers/web/user/setting/keys.go | 10 ++++ routers/web/web.go | 27 ++++++--- templates/user/settings/account.tmpl | 84 +++++++++++++++------------- templates/user/settings/keys.tmpl | 4 +- templates/user/settings/navbar.tmpl | 24 +++++--- 8 files changed, 130 insertions(+), 59 deletions(-) create mode 100644 modules/setting/user.go diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 03f004ee90196..1367f40b50ac0 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2481,3 +2481,7 @@ ROUTER = console ;PROXY_URL = ;; Comma separated list of host names requiring proxy. Glob patterns (*) are accepted; use ** to match all hosts. ;PROXY_HOSTS = + +;[user] +; Disabled modules from user settings, could be passwods, suicide, security, applications, gpg keys, organiztions +;USER_SETTING_DISABLED_MODULES = diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 9f2f0933d4eae..962c343933131 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1128,6 +1128,8 @@ func loadFromConf(allowEmpty bool, extraConfig string) { for _, emoji := range UI.CustomEmojis { UI.CustomEmojisMap[emoji] = ":" + emoji + ":" } + + newUserSetting() } func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) { diff --git a/modules/setting/user.go b/modules/setting/user.go new file mode 100644 index 0000000000000..1ea9ff1c79c3d --- /dev/null +++ b/modules/setting/user.go @@ -0,0 +1,34 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package setting + +import ( + "strings" + + "code.gitea.io/gitea/modules/log" +) + +// userSetting represents user settings +type userSetting struct { + SettingDisabledModules []string +} + +func (s *userSetting) Enabled(module string) bool { + for _, m := range s.SettingDisabledModules { + if strings.EqualFold(m, module) { + return false + } + } + return true +} + +var User userSetting + +func newUserSetting() { + sec := Cfg.Section("user") + if err := sec.MapTo(&User); err != nil { + log.Fatal("user setting mapping failed: %v", err) + } +} diff --git a/routers/web/user/setting/keys.go b/routers/web/user/setting/keys.go index 89be795599bda..98ad3c5dfebe9 100644 --- a/routers/web/user/setting/keys.go +++ b/routers/web/user/setting/keys.go @@ -5,6 +5,7 @@ package setting import ( + "fmt" "net/http" asymkey_model "code.gitea.io/gitea/models/asymkey" @@ -77,6 +78,11 @@ func KeysPost(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.add_principal_success", form.Content)) ctx.Redirect(setting.AppSubURL + "/user/settings/keys") case "gpg": + if !setting.User.Enabled("gpg keys") { + ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting are not allowed")) + return + } + token := asymkey_model.VerificationToken(ctx.Doer, 1) lastToken := asymkey_model.VerificationToken(ctx.Doer, 0) @@ -216,6 +222,10 @@ func KeysPost(ctx *context.Context) { func DeleteKey(ctx *context.Context) { switch ctx.FormString("type") { case "gpg": + if !setting.User.Enabled("gpg keys") { + ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting are not allowed")) + return + } if err := asymkey_model.DeleteGPGKey(ctx.Doer, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeleteGPGKey: " + err.Error()) } else { diff --git a/routers/web/web.go b/routers/web/web.go index 889a89f0d4ae4..ccdcb5c10b718 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -316,6 +316,14 @@ func RegisterRoutes(m *web.Route) { } } + userSettingModuleEnabled := func(module string) func(ctx *context.Context) { + return func(ctx *context.Context) { + if !setting.User.Enabled(module) { + ctx.Error(http.StatusNotFound) + } + } + } + // FIXME: not all routes need go through same middleware. // Especially some AJAX requests, we can reduce middleware number to improve performance. // Routers. @@ -406,15 +414,15 @@ func RegisterRoutes(m *web.Route) { m.Group("/user/settings", func() { m.Get("", user_setting.Profile) m.Post("", bindIgnErr(forms.UpdateProfileForm{}), user_setting.ProfilePost) - m.Get("/change_password", auth.MustChangePassword) - m.Post("/change_password", bindIgnErr(forms.MustChangePasswordForm{}), auth.MustChangePasswordPost) + m.Get("/change_password", userSettingModuleEnabled("password"), auth.MustChangePassword) + m.Post("/change_password", userSettingModuleEnabled("password"), bindIgnErr(forms.MustChangePasswordForm{}), auth.MustChangePasswordPost) m.Post("/avatar", bindIgnErr(forms.AvatarForm{}), user_setting.AvatarPost) m.Post("/avatar/delete", user_setting.DeleteAvatar) m.Group("/account", func() { m.Combo("").Get(user_setting.Account).Post(bindIgnErr(forms.ChangePasswordForm{}), user_setting.AccountPost) m.Post("/email", bindIgnErr(forms.AddEmailForm{}), user_setting.EmailPost) m.Post("/email/delete", user_setting.DeleteEmail) - m.Post("/delete", user_setting.DeleteAccount) + m.Post("/delete", userSettingModuleEnabled("suicide"), user_setting.DeleteAccount) }) m.Group("/appearance", func() { m.Get("", user_setting.Appearance) @@ -441,7 +449,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/toggle_visibility", security.ToggleOpenIDVisibility) }, openIDSignInEnabled) m.Post("/account_link", linkAccountEnabled, security.DeleteAccountLink) - }) + }, userSettingModuleEnabled("security")) m.Group("/applications/oauth2", func() { m.Get("/{id}", user_setting.OAuth2ApplicationShow) m.Post("/{id}", bindIgnErr(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsEdit) @@ -449,10 +457,10 @@ func RegisterRoutes(m *web.Route) { m.Post("", bindIgnErr(forms.EditOAuth2ApplicationForm{}), user_setting.OAuthApplicationsPost) m.Post("/{id}/delete", user_setting.DeleteOAuth2Application) m.Post("/{id}/revoke/{grantId}", user_setting.RevokeOAuth2Grant) - }) - m.Combo("/applications").Get(user_setting.Applications). - Post(bindIgnErr(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost) - m.Post("/applications/delete", user_setting.DeleteApplication) + }, userSettingModuleEnabled("applications")) + m.Combo("/applications").Get(userSettingModuleEnabled("applications"), user_setting.Applications). + Post(userSettingModuleEnabled("applications"), bindIgnErr(forms.NewAccessTokenForm{}), user_setting.ApplicationsPost) + m.Post("/applications/delete", userSettingModuleEnabled("applications"), user_setting.DeleteApplication) m.Combo("/keys").Get(user_setting.Keys). Post(bindIgnErr(forms.AddKeyForm{}), user_setting.KeysPost) m.Post("/keys/delete", user_setting.DeleteKey) @@ -470,13 +478,14 @@ func RegisterRoutes(m *web.Route) { }) }) }, packagesEnabled) - m.Get("/organization", user_setting.Organization) + m.Get("/organization", userSettingModuleEnabled("organizations"), user_setting.Organization) m.Get("/repos", user_setting.Repos) m.Post("/repos/unadopted", user_setting.AdoptOrDeleteRepository) }, reqSignIn, func(ctx *context.Context) { ctx.Data["PageIsUserSettings"] = true ctx.Data["AllThemes"] = setting.UI.Themes ctx.Data["EnablePackages"] = setting.Packages.Enabled + ctx.Data["UserModules"] = &setting.User }) m.Group("/user", func() { diff --git a/templates/user/settings/account.tmpl b/templates/user/settings/account.tmpl index 3070e8889c089..5eca448d04b63 100644 --- a/templates/user/settings/account.tmpl +++ b/templates/user/settings/account.tmpl @@ -3,40 +3,42 @@ {{template "user/settings/navbar" .}}
{{template "base/alert" .}} -

- {{.locale.Tr "settings.password"}} -

-
- {{if or (.SignedUser.IsLocal) (.SignedUser.IsOAuth2)}} -
- {{template "base/disable_form_autofill"}} - {{.CsrfTokenHtml}} - {{if .SignedUser.IsPasswordSet}} -
- - -
- {{end}} -
- - -
-
- - -
+ {{if $.UserModules.Enabled "password"}} +

+ {{.locale.Tr "settings.password"}} +

+
+ {{if or (.SignedUser.IsLocal) (.SignedUser.IsOAuth2)}} + + {{template "base/disable_form_autofill"}} + {{.CsrfTokenHtml}} + {{if .SignedUser.IsPasswordSet}} +
+ + +
+ {{end}} +
+ + +
+
+ + +
-
- - {{.locale.Tr "auth.forgot_password"}} +
+ + {{.locale.Tr "auth.forgot_password"}} +
+ + {{else}} +
+

{{$.locale.Tr "settings.password_change_disabled"}}

- - {{else}} -
-

{{$.locale.Tr "settings.password_change_disabled"}}

+ {{end}}
- {{end}} -
+ {{end}}

{{.locale.Tr "settings.manage_emails"}} @@ -172,15 +174,17 @@ {{template "base/delete_modal_actions" .}}

-
diff --git a/templates/user/settings/navbar.tmpl b/templates/user/settings/navbar.tmpl index 8fd869dc40c32..a9798f4a19cda 100644 --- a/templates/user/settings/navbar.tmpl +++ b/templates/user/settings/navbar.tmpl @@ -9,12 +9,16 @@ {{.locale.Tr "settings.appearance"}} - - {{.locale.Tr "settings.security"}} - - - {{.locale.Tr "settings.applications"}} - + {{if $.UserModules.Enabled "security"}} + + {{.locale.Tr "settings.security"}} + + {{end}} + {{if $.UserModules.Enabled "applications"}} + + {{.locale.Tr "settings.applications"}} + + {{end}} {{.locale.Tr "settings.ssh_gpg_keys"}} @@ -23,9 +27,11 @@ {{.locale.Tr "packages.title"}} {{end}} - - {{.locale.Tr "settings.organization"}} - + {{if $.UserModules.Enabled "organizations"}} + + {{.locale.Tr "settings.organization"}} + + {{end}} {{.locale.Tr "settings.repos"}} From 72959dc3960f00f02eadc14f9645e016facf9e95 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 31 Jul 2022 11:54:04 +0800 Subject: [PATCH 2/8] Use a suitable word for deletion of user data --- custom/conf/app.example.ini | 2 +- docs/content/doc/advanced/config-cheat-sheet.en-us.md | 4 ++++ routers/web/web.go | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 1367f40b50ac0..5a00f4b39cf09 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2483,5 +2483,5 @@ ROUTER = console ;PROXY_HOSTS = ;[user] -; Disabled modules from user settings, could be passwods, suicide, security, applications, gpg keys, organiztions +; Disabled modules from user settings, could be passwods, deletion, security, applications, gpg keys, organiztions ;USER_SETTING_DISABLED_MODULES = diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 37f03b42ea724..700475f238b19 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -1284,6 +1284,10 @@ PROXY_URL = socks://127.0.0.1:1080 PROXY_HOSTS = *.github.com ``` +## User (`user`) + +- `USER_SETTING_DISABLED_MODULES`:**** Disabled modules from user settings, could be passwods, deletion, security, applications, gpg keys, organiztions + ## Other (`other`) - `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer. diff --git a/routers/web/web.go b/routers/web/web.go index ccdcb5c10b718..8b8b8c5ce2651 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -422,7 +422,7 @@ func RegisterRoutes(m *web.Route) { m.Combo("").Get(user_setting.Account).Post(bindIgnErr(forms.ChangePasswordForm{}), user_setting.AccountPost) m.Post("/email", bindIgnErr(forms.AddEmailForm{}), user_setting.EmailPost) m.Post("/email/delete", user_setting.DeleteEmail) - m.Post("/delete", userSettingModuleEnabled("suicide"), user_setting.DeleteAccount) + m.Post("/delete", userSettingModuleEnabled("deletion"), user_setting.DeleteAccount) }) m.Group("/appearance", func() { m.Get("", user_setting.Appearance) From e160a0647b76cbd646779d076e65c12ab2f9a9b4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 31 Jul 2022 11:55:46 +0800 Subject: [PATCH 3/8] Fix missed word --- templates/user/settings/account.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/user/settings/account.tmpl b/templates/user/settings/account.tmpl index 5eca448d04b63..b59310bd39b19 100644 --- a/templates/user/settings/account.tmpl +++ b/templates/user/settings/account.tmpl @@ -174,7 +174,7 @@ {{template "base/delete_modal_actions" .}}
-{{if $.UserModules.Enabled "suicide"}} +{{if $.UserModules.Enabled "deletion"}} -{{if $.UserModules.Enabled "deletion"}} -