2016-03-06 05:47:39 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-02 07:41:45 -05:00
|
|
|
"context"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2021-11-02 07:41:45 -05:00
|
|
|
|
2016-04-01 19:34:30 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-04-21 08:03:17 -05:00
|
|
|
pref "github.com/grafana/grafana/pkg/services/preference"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-03-06 05:47:39 -06:00
|
|
|
)
|
|
|
|
|
2021-08-24 01:41:15 -05:00
|
|
|
const (
|
|
|
|
defaultTheme string = ""
|
|
|
|
darkTheme string = "dark"
|
|
|
|
lightTheme string = "light"
|
|
|
|
)
|
|
|
|
|
2016-03-17 01:22:27 -05:00
|
|
|
// POST /api/preferences/set-home-dash
|
2022-02-03 02:20:20 -06:00
|
|
|
func (hs *HTTPServer) SetHomeDashboard(c *models.ReqContext) response.Response {
|
2022-04-21 08:03:17 -05:00
|
|
|
cmd := pref.SavePreferenceCommand{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd.UserID = c.UserID
|
|
|
|
cmd.OrgID = c.OrgID
|
2016-03-17 01:22:27 -05:00
|
|
|
|
2022-04-29 07:37:33 -05:00
|
|
|
// the default value of HomeDashboardID is taken from input, when HomeDashboardID is set also,
|
|
|
|
// UID is used in preference to identify dashboard
|
|
|
|
dashboardID := cmd.HomeDashboardID
|
|
|
|
if cmd.HomeDashboardUID != nil {
|
|
|
|
query := models.GetDashboardQuery{Uid: *cmd.HomeDashboardUID}
|
2022-08-26 11:40:40 -05:00
|
|
|
if query.Uid == "" {
|
|
|
|
dashboardID = 0 // clear the value
|
|
|
|
} else {
|
|
|
|
err := hs.DashboardService.GetDashboard(c.Req.Context(), &query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(404, "Dashboard not found", err)
|
|
|
|
}
|
|
|
|
dashboardID = query.Result.Id
|
2022-04-29 07:37:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.HomeDashboardID = dashboardID
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
if err := hs.preferenceService.Save(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to set home dashboard", err)
|
2016-03-17 01:35:06 -05:00
|
|
|
}
|
2016-03-17 01:22:27 -05:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Home dashboard set")
|
2016-03-17 01:22:27 -05:00
|
|
|
}
|
2016-04-01 19:34:30 -05:00
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /user/preferences user_preferences getUserPreferences
|
|
|
|
//
|
|
|
|
// Get user preferences.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getPreferencesResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 500: internalServerError
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) GetUserPreferences(c *models.ReqContext) response.Response {
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.getPreferencesFor(c.Req.Context(), c.OrgID, c.UserID, 0)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) getPreferencesFor(ctx context.Context, orgID, userID, teamID int64) response.Response {
|
2022-04-21 08:03:17 -05:00
|
|
|
prefsQuery := pref.GetPreferenceQuery{UserID: userID, OrgID: orgID, TeamID: teamID}
|
2016-04-01 19:34:30 -05:00
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
preference, err := hs.preferenceService.Get(ctx, &prefsQuery)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get preferences", err)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2022-04-29 07:37:33 -05:00
|
|
|
var dashboardUID string
|
|
|
|
|
|
|
|
// when homedashboardID is 0, that means it is the default home dashboard, no UID would be returned in the response
|
|
|
|
if preference.HomeDashboardID != 0 {
|
|
|
|
query := models.GetDashboardQuery{Id: preference.HomeDashboardID, OrgId: orgID}
|
2022-07-06 13:42:39 -05:00
|
|
|
err = hs.DashboardService.GetDashboard(ctx, &query)
|
2022-04-29 07:37:33 -05:00
|
|
|
if err == nil {
|
|
|
|
dashboardUID = query.Result.Uid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
dto := dtos.Prefs{
|
2022-04-29 07:37:33 -05:00
|
|
|
Theme: preference.Theme,
|
|
|
|
HomeDashboardID: preference.HomeDashboardID,
|
|
|
|
HomeDashboardUID: dashboardUID,
|
|
|
|
Timezone: preference.Timezone,
|
|
|
|
WeekStart: preference.WeekStart,
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
if preference.JSONData != nil {
|
2022-06-14 06:53:51 -05:00
|
|
|
dto.Locale = preference.JSONData.Locale
|
2022-04-21 08:03:17 -05:00
|
|
|
dto.Navbar = preference.JSONData.Navbar
|
|
|
|
dto.QueryHistory = preference.JSONData.QueryHistory
|
2022-03-17 07:07:20 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &dto)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PUT /user/preferences user_preferences updateUserPreferences
|
|
|
|
//
|
|
|
|
// Update user preferences.
|
|
|
|
//
|
|
|
|
// Omitting a key (`theme`, `homeDashboardId`, `timezone`) will cause the current value to be replaced with the system default value.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 500: internalServerError
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateUserPreferences(c *models.ReqContext) response.Response {
|
|
|
|
dtoCmd := dtos.UpdatePrefsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &dtoCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.updatePreferencesFor(c.Req.Context(), c.OrgID, c.UserID, 0, &dtoCmd)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) response.Response {
|
2021-08-24 01:41:15 -05:00
|
|
|
if dtoCmd.Theme != lightTheme && dtoCmd.Theme != darkTheme && dtoCmd.Theme != defaultTheme {
|
|
|
|
return response.Error(400, "Invalid theme", nil)
|
|
|
|
}
|
2022-04-29 07:37:33 -05:00
|
|
|
|
|
|
|
dashboardID := dtoCmd.HomeDashboardID
|
|
|
|
if dtoCmd.HomeDashboardUID != nil {
|
|
|
|
query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID}
|
2022-08-26 11:40:40 -05:00
|
|
|
if query.Uid == "" {
|
|
|
|
// clear the value
|
|
|
|
dashboardID = 0
|
|
|
|
} else {
|
|
|
|
err := hs.DashboardService.GetDashboard(ctx, &query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(404, "Dashboard not found", err)
|
|
|
|
}
|
|
|
|
dashboardID = query.Result.Id
|
2022-04-29 07:37:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dtoCmd.HomeDashboardID = dashboardID
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
saveCmd := pref.SavePreferenceCommand{
|
|
|
|
UserID: userID,
|
|
|
|
OrgID: orgID,
|
|
|
|
TeamID: teamId,
|
2016-04-01 19:34:30 -05:00
|
|
|
Theme: dtoCmd.Theme,
|
2022-06-14 06:53:51 -05:00
|
|
|
Locale: dtoCmd.Locale,
|
2016-04-01 19:34:30 -05:00
|
|
|
Timezone: dtoCmd.Timezone,
|
2021-10-18 08:27:14 -05:00
|
|
|
WeekStart: dtoCmd.WeekStart,
|
2022-04-21 08:03:17 -05:00
|
|
|
HomeDashboardID: dtoCmd.HomeDashboardID,
|
2022-05-26 07:39:57 -05:00
|
|
|
QueryHistory: dtoCmd.QueryHistory,
|
|
|
|
Navbar: dtoCmd.Navbar,
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
if err := hs.preferenceService.Save(ctx, &saveCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save preferences", err)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Preferences updated")
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PATCH /user/preferences user_preferences patchUserPreferences
|
|
|
|
//
|
|
|
|
// Patch user preferences.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 500: internalServerError
|
2022-03-17 07:07:20 -05:00
|
|
|
func (hs *HTTPServer) PatchUserPreferences(c *models.ReqContext) response.Response {
|
|
|
|
dtoCmd := dtos.PatchPrefsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &dtoCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.patchPreferencesFor(c.Req.Context(), c.OrgID, c.UserID, 0, &dtoCmd)
|
2022-03-17 07:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) patchPreferencesFor(ctx context.Context, orgID, userID, teamId int64, dtoCmd *dtos.PatchPrefsCmd) response.Response {
|
|
|
|
if dtoCmd.Theme != nil && *dtoCmd.Theme != lightTheme && *dtoCmd.Theme != darkTheme && *dtoCmd.Theme != defaultTheme {
|
|
|
|
return response.Error(400, "Invalid theme", nil)
|
|
|
|
}
|
2022-04-29 07:37:33 -05:00
|
|
|
|
|
|
|
// convert dashboard UID to ID in order to store internally if it exists in the query, otherwise take the id from query
|
|
|
|
dashboardID := dtoCmd.HomeDashboardID
|
|
|
|
if dtoCmd.HomeDashboardUID != nil {
|
|
|
|
query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID}
|
2022-08-26 11:40:40 -05:00
|
|
|
if query.Uid == "" {
|
|
|
|
// clear the value
|
|
|
|
defaultDash := int64(0)
|
|
|
|
dashboardID = &defaultDash
|
|
|
|
} else {
|
|
|
|
err := hs.DashboardService.GetDashboard(ctx, &query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(404, "Dashboard not found", err)
|
|
|
|
}
|
|
|
|
dashboardID = &query.Result.Id
|
2022-04-29 07:37:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dtoCmd.HomeDashboardID = dashboardID
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
patchCmd := pref.PatchPreferenceCommand{
|
|
|
|
UserID: userID,
|
|
|
|
OrgID: orgID,
|
|
|
|
TeamID: teamId,
|
2022-03-17 07:07:20 -05:00
|
|
|
Theme: dtoCmd.Theme,
|
|
|
|
Timezone: dtoCmd.Timezone,
|
|
|
|
WeekStart: dtoCmd.WeekStart,
|
2022-04-21 08:03:17 -05:00
|
|
|
HomeDashboardID: dtoCmd.HomeDashboardID,
|
2022-06-14 06:53:51 -05:00
|
|
|
Locale: dtoCmd.Locale,
|
2022-03-17 07:07:20 -05:00
|
|
|
Navbar: dtoCmd.Navbar,
|
2022-04-12 04:42:42 -05:00
|
|
|
QueryHistory: dtoCmd.QueryHistory,
|
2022-03-17 07:07:20 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:03:17 -05:00
|
|
|
if err := hs.preferenceService.Patch(ctx, &patchCmd); err != nil {
|
2022-03-17 07:07:20 -05:00
|
|
|
return response.Error(500, "Failed to save preferences", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Success("Preferences updated")
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /org/preferences org_preferences getOrgPreferences
|
|
|
|
//
|
|
|
|
// Get Current Org Prefs.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getPreferencesResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) GetOrgPreferences(c *models.ReqContext) response.Response {
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.getPreferencesFor(c.Req.Context(), c.OrgID, 0, 0)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PUT /org/preferences org_preferences updateOrgPreferences
|
|
|
|
//
|
|
|
|
// Update Current Org Prefs.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateOrgPreferences(c *models.ReqContext) response.Response {
|
|
|
|
dtoCmd := dtos.UpdatePrefsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &dtoCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-04-29 07:37:33 -05:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.updatePreferencesFor(c.Req.Context(), c.OrgID, 0, 0, &dtoCmd)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
2022-03-17 07:07:20 -05:00
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PATCH /org/preferences org_preferences patchOrgPreferences
|
|
|
|
//
|
|
|
|
// Patch Current Org Prefs.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
2022-03-17 07:07:20 -05:00
|
|
|
func (hs *HTTPServer) PatchOrgPreferences(c *models.ReqContext) response.Response {
|
|
|
|
dtoCmd := dtos.PatchPrefsCmd{}
|
|
|
|
if err := web.Bind(c.Req, &dtoCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
return hs.patchPreferencesFor(c.Req.Context(), c.OrgID, 0, 0, &dtoCmd)
|
2022-03-17 07:07:20 -05:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters updateUserPreferences
|
|
|
|
type UpdateUserPreferencesParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.UpdatePrefsCmd `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters updateOrgPreferences
|
|
|
|
type UpdateOrgPreferencesParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.UpdatePrefsCmd `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getPreferencesResponse
|
|
|
|
type GetPreferencesResponse struct {
|
|
|
|
// in:body
|
|
|
|
Body dtos.Prefs `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters patchUserPreferences
|
|
|
|
type PatchUserPreferencesParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.PatchPrefsCmd `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters patchOrgPreferences
|
|
|
|
type PatchOrgPreferencesParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.PatchPrefsCmd `json:"body"`
|
|
|
|
}
|