Skip to content

Commit 00e532e

Browse files
lunnypull[bot]
authored andcommitted
Allow options to disable user gpg keys configuration from the interface on app.ini (#29486)
Follow #29447 Fix #29454 Extract from #20549
1 parent 38d452e commit 00e532e

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

custom/conf/app.example.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,9 @@ LEVEL = Info
14801480
;;
14811481
;; Default configuration for email notifications for users (user configurable). Options: enabled, onmention, disabled
14821482
;DEFAULT_EMAIL_NOTIFICATIONS = enabled
1483-
;; Disabled features for users, could be "deletion", more features can be disabled in future
1483+
;; Disabled features for users, could be "deletion","manage_gpg_keys" more features can be disabled in future
14841484
;; - deletion: a user cannot delete their own account
1485+
;; - manage_gpg_keys: a user cannot configure gpg keys
14851486
;USER_DISABLED_FEATURES =
14861487

14871488
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/administration/config-cheat-sheet.en-us.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,9 @@ And the following unique queues:
518518

519519
- `DEFAULT_EMAIL_NOTIFICATIONS`: **enabled**: Default configuration for email notifications for users (user configurable). Options: enabled, onmention, disabled
520520
- `DISABLE_REGULAR_ORG_CREATION`: **false**: Disallow regular (non-admin) users from creating organizations.
521-
- `USER_DISABLED_FEATURES`: **_empty_** Disabled features for users, could be `deletion` and more features can be added in future.
521+
- `USER_DISABLED_FEATURES`: **_empty_** Disabled features for users, could be `deletion`, `manage_gpg_keys` and more features can be added in future.
522522
- `deletion`: User cannot delete their own account.
523+
- `manage_gpg_keys`: User cannot configure gpg keys
523524

524525
## Security (`security`)
525526

docs/content/administration/config-cheat-sheet.zh-cn.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ Gitea 创建以下非唯一队列:
497497

498498
- `DEFAULT_EMAIL_NOTIFICATIONS`: **enabled**:用户电子邮件通知的默认配置(用户可配置)。选项:enabled、onmention、disabled
499499
- `DISABLE_REGULAR_ORG_CREATION`: **false**:禁止普通(非管理员)用户创建组织。
500-
- `USER_DISABLED_FEATURES`:**_empty_** 禁用的用户特性,当前允许为空或者 `deletion`, 未来可以增加更多设置。
500+
- `USER_DISABLED_FEATURES`:**_empty_** 禁用的用户特性,当前允许为空或者 `deletion``manage_gpg_keys` 未来可以增加更多设置。
501501
- `deletion`: 用户不能通过界面或者API删除他自己。
502+
- `manage_gpg_keys`: 用户不能配置 GPG 密钥
502503

503504
## 安全性 (`security`)
504505

modules/setting/admin.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ func loadAdminFrom(rootCfg ConfigProvider) {
2020
}
2121

2222
const (
23-
UserFeatureDeletion = "deletion"
23+
UserFeatureDeletion = "deletion"
24+
UserFeatureManageGPGKeys = "manage_gpg_keys"
2425
)

routers/api/v1/user/gpg_key.go

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
asymkey_model "code.gitea.io/gitea/models/asymkey"
1212
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/modules/setting"
1314
api "code.gitea.io/gitea/modules/structs"
1415
"code.gitea.io/gitea/modules/web"
1516
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -132,6 +133,11 @@ func GetGPGKey(ctx *context.APIContext) {
132133

133134
// CreateUserGPGKey creates new GPG key to given user by ID.
134135
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
136+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageGPGKeys) {
137+
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
138+
return
139+
}
140+
135141
token := asymkey_model.VerificationToken(ctx.Doer, 1)
136142
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
137143

@@ -268,6 +274,11 @@ func DeleteGPGKey(ctx *context.APIContext) {
268274
// "404":
269275
// "$ref": "#/responses/notFound"
270276

277+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageGPGKeys) {
278+
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
279+
return
280+
}
281+
271282
if err := asymkey_model.DeleteGPGKey(ctx, ctx.Doer, ctx.ParamsInt64(":id")); err != nil {
272283
if asymkey_model.IsErrGPGKeyAccessDenied(err) {
273284
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")

routers/web/user/setting/keys.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package setting
66

77
import (
8+
"fmt"
89
"net/http"
910

1011
asymkey_model "code.gitea.io/gitea/models/asymkey"
@@ -77,6 +78,11 @@ func KeysPost(ctx *context.Context) {
7778
ctx.Flash.Success(ctx.Tr("settings.add_principal_success", form.Content))
7879
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
7980
case "gpg":
81+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageGPGKeys) {
82+
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
83+
return
84+
}
85+
8086
token := asymkey_model.VerificationToken(ctx.Doer, 1)
8187
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
8288

@@ -224,6 +230,10 @@ func KeysPost(ctx *context.Context) {
224230
func DeleteKey(ctx *context.Context) {
225231
switch ctx.FormString("type") {
226232
case "gpg":
233+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageGPGKeys) {
234+
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
235+
return
236+
}
227237
if err := asymkey_model.DeleteGPGKey(ctx, ctx.Doer, ctx.FormInt64("id")); err != nil {
228238
ctx.Flash.Error("DeleteGPGKey: " + err.Error())
229239
} else {

templates/user/settings/keys.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<div class="user-setting-content">
33
{{template "user/settings/keys_ssh" .}}
44
{{template "user/settings/keys_principal" .}}
5+
{{if not ($.UserDisabledFeatures.Contains "manage_gpg_keys")}}
56
{{template "user/settings/keys_gpg" .}}
7+
{{end}}
68
</div>
79
{{template "user/settings/layout_footer" .}}

0 commit comments

Comments
 (0)