[MM-52924] Implement ConfigurationWillBeSaved plugin hook (#23567)

* Implement ConfigurationWillBeSaved plugin hook

* Add comment

* Update comment

* Fix potential nil dereference if plugin environment is unset

* Address PR review
Этот коммит содержится в:
Claudio Costa
2023-06-12 18:23:02 -06:00
коммит произвёл GitHub
родитель d0ad46b496
Коммит 62a3ee8adc
10 изменённых файлов: 267 добавлений и 9 удалений

Просмотреть файл

@@ -18,6 +18,7 @@ import (
"strconv"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/utils"
"github.com/mattermost/mattermost/server/v8/channels/product"
@@ -74,6 +75,24 @@ func (ps *PlatformService) IsConfigReadOnly() bool {
// SaveConfig replaces the active configuration, optionally notifying cluster peers.
// It returns both the previous and current configs.
func (ps *PlatformService) SaveConfig(newCfg *model.Config, sendConfigChangeClusterMessage bool) (*model.Config, *model.Config, *model.AppError) {
if ps.pluginEnv != nil {
var hookErr error
ps.pluginEnv.RunMultiHook(func(hooks plugin.Hooks) bool {
var cfg *model.Config
cfg, hookErr = hooks.ConfigurationWillBeSaved(newCfg)
if hookErr == nil && cfg != nil {
newCfg = cfg
}
return hookErr == nil
}, plugin.ConfigurationWillBeSavedID)
if hookErr != nil {
if appErr, ok := hookErr.(*model.AppError); ok {
return nil, nil, appErr
}
return nil, nil, model.NewAppError("saveConfig", "app.save_config.plugin_hook_error", nil, "", http.StatusBadRequest).Wrap(hookErr)
}
}
oldCfg, newCfg, err := ps.configStore.Set(newCfg)
if errors.Is(err, config.ErrReadOnlyConfiguration) {
return nil, nil, model.NewAppError("saveConfig", "ent.cluster.save_config.error", nil, "", http.StatusForbidden).Wrap(err)