2016-02-25 07:55:31 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", GetPluginSettings)
|
|
|
|
bus.AddHandler("sql", GetPluginSettingById)
|
|
|
|
bus.AddHandler("sql", UpdatePluginSetting)
|
2016-07-07 11:11:03 -05:00
|
|
|
bus.AddHandler("sql", UpdatePluginSettingVersion)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPluginSettings(query *m.GetPluginSettingsQuery) error {
|
2016-07-07 11:11:03 -05:00
|
|
|
sql := `SELECT org_id, plugin_id, enabled, pinned, plugin_version
|
|
|
|
FROM plugin_setting `
|
|
|
|
params := make([]interface{}, 0)
|
2016-02-25 07:55:31 -06:00
|
|
|
|
2016-07-07 11:11:03 -05:00
|
|
|
if query.OrgId != 0 {
|
|
|
|
sql += "WHERE org_id=?"
|
|
|
|
params = append(params, query.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := x.Sql(sql, params...)
|
2016-03-08 11:17:47 -06:00
|
|
|
query.Result = make([]*m.PluginSettingInfoDTO, 0)
|
2016-02-25 07:55:31 -06:00
|
|
|
return sess.Find(&query.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPluginSettingById(query *m.GetPluginSettingByIdQuery) error {
|
|
|
|
pluginSetting := m.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
|
|
|
|
has, err := x.Get(&pluginSetting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrPluginSettingNotFound
|
|
|
|
}
|
|
|
|
query.Result = &pluginSetting
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-02-25 07:55:31 -06:00
|
|
|
var pluginSetting m.PluginSetting
|
|
|
|
|
|
|
|
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
|
|
|
|
sess.UseBool("enabled")
|
|
|
|
sess.UseBool("pinned")
|
|
|
|
if !exists {
|
|
|
|
pluginSetting = m.PluginSetting{
|
|
|
|
PluginId: cmd.PluginId,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Enabled: cmd.Enabled,
|
|
|
|
Pinned: cmd.Pinned,
|
|
|
|
JsonData: cmd.JsonData,
|
2016-07-07 11:11:03 -05:00
|
|
|
PluginVersion: cmd.PluginVersion,
|
2016-02-25 07:55:31 -06:00
|
|
|
SecureJsonData: cmd.GetEncryptedJsonData(),
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
2016-07-08 05:26:51 -05:00
|
|
|
|
|
|
|
// add state change event on commit success
|
|
|
|
sess.events = append(sess.events, &m.PluginStateChangedEvent{
|
|
|
|
PluginId: cmd.PluginId,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Enabled: cmd.Enabled,
|
|
|
|
})
|
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
_, err = sess.Insert(&pluginSetting)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
for key, data := range cmd.SecureJsonData {
|
2017-04-25 02:14:29 -05:00
|
|
|
encryptedData, err := util.Encrypt([]byte(data), setting.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginSetting.SecureJsonData[key] = encryptedData
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2016-07-08 05:26:51 -05:00
|
|
|
|
|
|
|
// add state change event on commit success
|
|
|
|
if pluginSetting.Enabled != cmd.Enabled {
|
|
|
|
sess.events = append(sess.events, &m.PluginStateChangedEvent{
|
|
|
|
PluginId: cmd.PluginId,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Enabled: cmd.Enabled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
pluginSetting.Updated = time.Now()
|
|
|
|
pluginSetting.Enabled = cmd.Enabled
|
|
|
|
pluginSetting.JsonData = cmd.JsonData
|
|
|
|
pluginSetting.Pinned = cmd.Pinned
|
2016-07-07 11:11:03 -05:00
|
|
|
pluginSetting.PluginVersion = cmd.PluginVersion
|
2016-07-08 05:26:51 -05:00
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-07-07 11:11:03 -05:00
|
|
|
|
|
|
|
func UpdatePluginSettingVersion(cmd *m.UpdatePluginSettingVersionCmd) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-07-07 11:11:03 -05:00
|
|
|
|
|
|
|
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
|
|
|
|
return err
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|