66package admin
77
88import (
9+ "fmt"
910 "net/http"
1011 "net/url"
1112 "os"
13+ "strconv"
1214 "strings"
1315
1416 system_model "code.gitea.io/gitea/models/system"
@@ -202,6 +204,16 @@ func ChangeConfig(ctx *context.Context) {
202204 value := ctx .FormString ("value" )
203205 version := ctx .FormInt ("version" )
204206
207+ if check , ok := changeConfigChecks [key ]; ok {
208+ if err := check (ctx , value ); err != nil {
209+ log .Warn ("refused to set setting: %v" , err )
210+ ctx .JSON (http .StatusOK , map [string ]string {
211+ "err" : ctx .Tr ("admin.config.set_setting_failed" , key ),
212+ })
213+ return
214+ }
215+ }
216+
205217 if err := system_model .SetSetting (& system_model.Setting {
206218 SettingKey : key ,
207219 SettingValue : value ,
@@ -218,3 +230,18 @@ func ChangeConfig(ctx *context.Context) {
218230 "version" : version + 1 ,
219231 })
220232}
233+
234+ var changeConfigChecks = map [string ]func (ctx * context.Context , newValue string ) error {
235+ system_model .KeyPictureDisableGravatar : func (_ * context.Context , newValue string ) error {
236+ if v , _ := strconv .ParseBool (newValue ); setting .OfflineMode && ! v {
237+ return fmt .Errorf ("%q should be true when OFFLINE_MODE is true" , system_model .KeyPictureDisableGravatar )
238+ }
239+ return nil
240+ },
241+ system_model .KeyPictureEnableFederatedAvatar : func (_ * context.Context , newValue string ) error {
242+ if v , _ := strconv .ParseBool (newValue ); setting .OfflineMode && v {
243+ return fmt .Errorf ("%q cannot be false when OFFLINE_MODE is true" , system_model .KeyPictureEnableFederatedAvatar )
244+ }
245+ return nil
246+ },
247+ }
0 commit comments