mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added savePreferencesAPI
This commit is contained in:
parent
e73e7aea56
commit
cf926134ef
@ -96,6 +96,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
|
||||
r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
|
||||
r.Get("/quotas", wrap(GetUserQuotas))
|
||||
r.Put("/prefs", bind(m.SavePreferenceCommand{}), wrap(SaveUserPreferences))
|
||||
})
|
||||
|
||||
// users (admin permission required)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
)
|
||||
|
||||
// GET /api/user (current authenticated user)
|
||||
@ -110,7 +111,10 @@ func UserSetUsingOrg(c *middleware.Context) Response {
|
||||
}
|
||||
|
||||
func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response {
|
||||
userQuery := m.GetUserByIdQuery{Id: c.UserId}
|
||||
|
||||
log.Info("%v", cmd)
|
||||
|
||||
userQuery := m.GetUserByIdQuery{Id: c.UserId}
|
||||
|
||||
if err := bus.Dispatch(&userQuery); err != nil {
|
||||
return ApiError(500, "Could not read user from database", err)
|
||||
@ -144,3 +148,18 @@ func SearchUsers(c *middleware.Context) Response {
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
||||
func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferenceCommand) Response {
|
||||
|
||||
log.Info("%v", cmd.PrefData)
|
||||
|
||||
cmd.PrefId = c.UserId
|
||||
cmd.PrefType = `user`
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return ApiError(500, "Failed to saved user preferences", err)
|
||||
}
|
||||
|
||||
return ApiSuccess("User preferences saved")
|
||||
|
||||
}
|
||||
|
28
pkg/models/preferences.go
Normal file
28
pkg/models/preferences.go
Normal file
@ -0,0 +1,28 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Typed errors
|
||||
var (
|
||||
ErrPreferenceNotFound = errors.New("Preference not found")
|
||||
)
|
||||
|
||||
type Preference struct {
|
||||
Id int64
|
||||
PrefId int64
|
||||
PrefType string
|
||||
PrefData map[string]interface{}
|
||||
}
|
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type SavePreferenceCommand struct {
|
||||
|
||||
PrefData map[string]interface{} `json:"prefData"`
|
||||
PrefId int64 `json:"-"`
|
||||
PrefType string `json:"-"`
|
||||
|
||||
}
|
@ -11,6 +11,7 @@ import (
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -27,6 +28,7 @@ func init() {
|
||||
bus.AddHandler("sql", DeleteUser)
|
||||
bus.AddHandler("sql", SetUsingOrg)
|
||||
bus.AddHandler("sql", UpdateUserPermissions)
|
||||
bus.AddHandler("sql", SaveUserPreferences)
|
||||
}
|
||||
|
||||
func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) {
|
||||
@ -346,3 +348,26 @@ func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error {
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func SaveUserPreferences(cmd *m.SavePreferenceCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
log.Info("%v", cmd)
|
||||
|
||||
pref := m.Preference{
|
||||
PrefId: cmd.PrefId,
|
||||
PrefType: cmd.PrefType,
|
||||
PrefData: cmd.PrefData,
|
||||
}
|
||||
|
||||
sess.Table("preferences").Where("pref_id", pref.PrefId).And("pref_type", pref.PrefType)
|
||||
|
||||
if _, err := sess.Update(&pref); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("%v", pref)
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user