Skip to content
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

Set disable_gravatar/enable_federated_avatar when offline mode is true (#22479) #22496

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions models/system/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ func Init() error {
if setting_module.OfflineMode {
disableGravatar = true
enableFederatedAvatar = false
if !GetSettingBool(KeyPictureDisableGravatar) {
if err := SetSettingNoVersion(KeyPictureDisableGravatar, "true"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureDisableGravatar, err)
}
}
if GetSettingBool(KeyPictureEnableFederatedAvatar) {
if err := SetSettingNoVersion(KeyPictureEnableFederatedAvatar, "false"); err != nil {
return fmt.Errorf("Failed to set setting %q: %w", KeyPictureEnableFederatedAvatar, err)
}
}
}

if enableFederatedAvatar || !disableGravatar {
Expand Down
27 changes: 27 additions & 0 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package admin

import (
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"

system_model "code.gitea.io/gitea/models/system"
Expand Down Expand Up @@ -202,6 +204,16 @@ func ChangeConfig(ctx *context.Context) {
value := ctx.FormString("value")
version := ctx.FormInt("version")

if check, ok := changeConfigChecks[key]; ok {
if err := check(ctx, value); err != nil {
log.Warn("refused to set setting: %v", err)
ctx.JSON(http.StatusOK, map[string]string{
"err": ctx.Tr("admin.config.set_setting_failed", key),
})
return
}
}

if err := system_model.SetSetting(&system_model.Setting{
SettingKey: key,
SettingValue: value,
Expand All @@ -218,3 +230,18 @@ func ChangeConfig(ctx *context.Context) {
"version": version + 1,
})
}

var changeConfigChecks = map[string]func(ctx *context.Context, newValue string) error{
system_model.KeyPictureDisableGravatar: func(_ *context.Context, newValue string) error {
if v, _ := strconv.ParseBool(newValue); setting.OfflineMode && !v {
return fmt.Errorf("%q should be true when OFFLINE_MODE is true", system_model.KeyPictureDisableGravatar)
}
return nil
},
system_model.KeyPictureEnableFederatedAvatar: func(_ *context.Context, newValue string) error {
if v, _ := strconv.ParseBool(newValue); setting.OfflineMode && v {
return fmt.Errorf("%q cannot be false when OFFLINE_MODE is true", system_model.KeyPictureEnableFederatedAvatar)
}
return nil
},
}