From ac8282cda1455802f52ebe6687eeeef6c950bf49 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Mon, 20 Mar 2017 13:37:34 +0100 Subject: Add updateConfig endpoint for apiV4 (#5706) --- api4/system.go | 27 +++++++++++++++++++++++++++ api4/system_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ config/config.json | 2 +- model/client4.go | 11 +++++++++++ model/config.go | 2 +- 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/api4/system.go b/api4/system.go index f12d802ef..5058b0e2f 100644 --- a/api4/system.go +++ b/api4/system.go @@ -18,6 +18,7 @@ func InitSystem() { BaseRoutes.System.Handle("/ping", ApiHandler(getSystemPing)).Methods("GET") BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(getConfig)).Methods("GET") BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") + BaseRoutes.ApiRoot.Handle("/config", ApiSessionRequired(updateConfig)).Methods("PUT") BaseRoutes.ApiRoot.Handle("/email/test", ApiSessionRequired(testEmail)).Methods("POST") BaseRoutes.ApiRoot.Handle("/database/recycle", ApiSessionRequired(databaseRecycle)).Methods("POST") BaseRoutes.ApiRoot.Handle("/caches/invalidate", ApiSessionRequired(invalidateCaches)).Methods("POST") @@ -69,6 +70,32 @@ func configReload(c *Context, w http.ResponseWriter, r *http.Request) { ReturnStatusOK(w) } +func updateConfig(c *Context, w http.ResponseWriter, r *http.Request) { + cfg := model.ConfigFromJson(r.Body) + if cfg == nil { + c.SetInvalidParam("config") + return + } + + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + err := app.SaveConfig(cfg) + if err != nil { + c.Err = err + return + } + + c.LogAudit("updateConfig") + + cfg = app.GetConfig() + + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + w.Write([]byte(cfg.ToJson())) +} + func databaseRecycle(c *Context, w http.ResponseWriter, r *http.Request) { if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { diff --git a/api4/system_test.go b/api4/system_test.go index 80b4996ca..658bb5881 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -5,6 +5,7 @@ import ( "testing" l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -88,6 +89,47 @@ func TestReloadConfig(t *testing.T) { *utils.Cfg.TeamSettings.EnableOpenServer = true } +func TestUpdateConfig(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + cfg := app.GetConfig() + + _, resp := Client.UpdateConfig(cfg) + CheckForbiddenStatus(t, resp) + + SiteName := utils.Cfg.TeamSettings.SiteName + + cfg.TeamSettings.SiteName = "MyFancyName" + cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) + CheckNoError(t, resp) + + if len(cfg.TeamSettings.SiteName) == 0 { + t.Fatal() + } else { + if cfg.TeamSettings.SiteName != "MyFancyName" { + t.Log("It should update the SiteName") + t.Fatal() + } + } + + //Revert the change + cfg.TeamSettings.SiteName = SiteName + cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) + CheckNoError(t, resp) + + if len(cfg.TeamSettings.SiteName) == 0 { + t.Fatal() + } else { + if cfg.TeamSettings.SiteName != SiteName { + t.Log("It should update the SiteName") + t.Fatal() + } + } + +} + func TestEmailTest(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() diff --git a/config/config.json b/config/config.json index 18594f60d..68810e4cc 100644 --- a/config/config.json +++ b/config/config.json @@ -260,4 +260,4 @@ "TurnUsername": "", "TurnSharedKey": "" } -} +} \ No newline at end of file diff --git a/model/client4.go b/model/client4.go index a2878df6f..307d52c55 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1191,6 +1191,7 @@ func (c *Client4) DatabaseRecycle() (bool, *Response) { } } +// InvalidateCaches will purge the cache and can affect the performance while is cleaning. func (c *Client4) InvalidateCaches() (bool, *Response) { if r, err := c.DoApiPost(c.GetCacheRoute()+"/invalidate", ""); err != nil { return false, &Response{StatusCode: r.StatusCode, Error: err} @@ -1200,6 +1201,16 @@ func (c *Client4) InvalidateCaches() (bool, *Response) { } } +// UpdateConfig will update the server configuration +func (c *Client4) UpdateConfig(config *Config) (*Config, *Response) { + if r, err := c.DoApiPut(c.GetConfigRoute(), config.ToJson()); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return ConfigFromJson(r.Body), BuildResponse(r) + } +} + // Webhooks Section // CreateIncomingWebhook creates an incoming webhook for a channel. diff --git a/model/config.go b/model/config.go index 86693b57e..8a2a5cc47 100644 --- a/model/config.go +++ b/model/config.go @@ -473,7 +473,7 @@ func (o *Config) SetDefaults() { *o.FileSettings.MaxFileSize = 52428800 // 50 MB } - if len(*o.FileSettings.PublicLinkSalt) == 0 { + if o.FileSettings.PublicLinkSalt == nil || len(*o.FileSettings.PublicLinkSalt) == 0 { o.FileSettings.PublicLinkSalt = new(string) *o.FileSettings.PublicLinkSalt = NewRandomString(32) } -- cgit v1.2.3-1-g7c22