Exemplo n.º 1
0
func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error {
	return inTransaction2(func(sess *session) error {
		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,
				SecureJsonData: cmd.GetEncryptedJsonData(),
				Created:        time.Now(),
				Updated:        time.Now(),
			}
			_, err = sess.Insert(&pluginSetting)
			return err
		} else {
			for key, data := range cmd.SecureJsonData {
				pluginSetting.SecureJsonData[key] = util.Encrypt([]byte(data), setting.SecretKey)
			}
			pluginSetting.Updated = time.Now()
			pluginSetting.Enabled = cmd.Enabled
			pluginSetting.JsonData = cmd.JsonData
			pluginSetting.Pinned = cmd.Pinned
			_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
			return err
		}
	})
}
Exemplo n.º 2
0
func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error {
	return inTransaction2(func(sess *session) error {
		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,
				PluginVersion:  cmd.PluginVersion,
				SecureJsonData: cmd.GetEncryptedJsonData(),
				Created:        time.Now(),
				Updated:        time.Now(),
			}

			// add state change event on commit success
			sess.events = append(sess.events, &m.PluginStateChangedEvent{
				PluginId: cmd.PluginId,
				OrgId:    cmd.OrgId,
				Enabled:  cmd.Enabled,
			})

			_, err = sess.Insert(&pluginSetting)
			return err
		} else {
			for key, data := range cmd.SecureJsonData {
				pluginSetting.SecureJsonData[key] = util.Encrypt([]byte(data), setting.SecretKey)
			}

			// 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,
				})
			}

			pluginSetting.Updated = time.Now()
			pluginSetting.Enabled = cmd.Enabled
			pluginSetting.JsonData = cmd.JsonData
			pluginSetting.Pinned = cmd.Pinned
			pluginSetting.PluginVersion = cmd.PluginVersion

			_, err = sess.Id(pluginSetting.Id).Update(&pluginSetting)
			return err
		}
	})
}