Chore: Propagate context for preferences (#41167)

Ref #36734
This commit is contained in:
Marcus Efraimsson
2021-11-02 13:41:45 +01:00
committed by GitHub
parent ad888813c6
commit 1a89d97fed
6 changed files with 61 additions and 58 deletions
+4 -4
View File
@@ -158,8 +158,8 @@ func (hs *HTTPServer) registerRoutes() {
// For dev purpose
userRoute.Get("/helpflags/clear", routing.Wrap(ClearHelpFlags))
userRoute.Get("/preferences", routing.Wrap(GetUserPreferences))
userRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), routing.Wrap(UpdateUserPreferences))
userRoute.Get("/preferences", routing.Wrap(hs.GetUserPreferences))
userRoute.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), routing.Wrap(hs.UpdateUserPreferences))
userRoute.Get("/auth-tokens", routing.Wrap(hs.GetUserAuthTokens))
userRoute.Post("/revoke-auth-token", bind(models.RevokeAuthTokenCmd{}), routing.Wrap(hs.RevokeUserAuthToken))
@@ -221,8 +221,8 @@ func (hs *HTTPServer) registerRoutes() {
orgRoute.Patch("/invites/:code/revoke", authorize(reqOrgAdmin, ac.EvalPermission(ac.ActionUsersCreate)), routing.Wrap(RevokeInvite))
// prefs
orgRoute.Get("/preferences", authorize(reqOrgAdmin, ac.EvalPermission(ActionOrgsPreferencesRead, ScopeOrgCurrentID)), routing.Wrap(GetOrgPreferences))
orgRoute.Put("/preferences", authorize(reqOrgAdmin, ac.EvalPermission(ActionOrgsPreferencesWrite, ScopeOrgCurrentID)), bind(dtos.UpdatePrefsCmd{}), routing.Wrap(UpdateOrgPreferences))
orgRoute.Get("/preferences", authorize(reqOrgAdmin, ac.EvalPermission(ActionOrgsPreferencesRead, ScopeOrgCurrentID)), routing.Wrap(hs.GetOrgPreferences))
orgRoute.Put("/preferences", authorize(reqOrgAdmin, ac.EvalPermission(ActionOrgsPreferencesWrite, ScopeOrgCurrentID)), bind(dtos.UpdatePrefsCmd{}), routing.Wrap(hs.UpdateOrgPreferences))
})
// current org without requirement of user to be org admin
+15 -14
View File
@@ -1,11 +1,12 @@
package api
import (
"context"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
const (
@@ -19,7 +20,7 @@ func SetHomeDashboard(c *models.ReqContext, cmd models.SavePreferencesCommand) r
cmd.UserId = c.UserId
cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil {
if err := bus.DispatchCtx(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to set home dashboard", err)
}
@@ -27,14 +28,14 @@ func SetHomeDashboard(c *models.ReqContext, cmd models.SavePreferencesCommand) r
}
// GET /api/user/preferences
func GetUserPreferences(c *models.ReqContext) response.Response {
return getPreferencesFor(c.OrgId, c.UserId, 0)
func (hs *HTTPServer) GetUserPreferences(c *models.ReqContext) response.Response {
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0)
}
func getPreferencesFor(orgID, userID, teamID int64) response.Response {
func (hs *HTTPServer) getPreferencesFor(ctx context.Context, orgID, userID, teamID int64) response.Response {
prefsQuery := models.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID}
if err := sqlstore.GetPreferences(&prefsQuery); err != nil {
if err := hs.SQLStore.GetPreferences(ctx, &prefsQuery); err != nil {
return response.Error(500, "Failed to get preferences", err)
}
@@ -49,11 +50,11 @@ func getPreferencesFor(orgID, userID, teamID int64) response.Response {
}
// PUT /api/user/preferences
func UpdateUserPreferences(c *models.ReqContext, dtoCmd dtos.UpdatePrefsCmd) response.Response {
return updatePreferencesFor(c.OrgId, c.UserId, 0, &dtoCmd)
func (hs *HTTPServer) UpdateUserPreferences(c *models.ReqContext, dtoCmd dtos.UpdatePrefsCmd) response.Response {
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0, &dtoCmd)
}
func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) response.Response {
func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) response.Response {
if dtoCmd.Theme != lightTheme && dtoCmd.Theme != darkTheme && dtoCmd.Theme != defaultTheme {
return response.Error(400, "Invalid theme", nil)
}
@@ -67,7 +68,7 @@ func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsC
HomeDashboardId: dtoCmd.HomeDashboardID,
}
if err := sqlstore.SavePreferences(&saveCmd); err != nil {
if err := hs.SQLStore.SavePreferences(ctx, &saveCmd); err != nil {
return response.Error(500, "Failed to save preferences", err)
}
@@ -75,11 +76,11 @@ func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsC
}
// GET /api/org/preferences
func GetOrgPreferences(c *models.ReqContext) response.Response {
return getPreferencesFor(c.OrgId, 0, 0)
func (hs *HTTPServer) GetOrgPreferences(c *models.ReqContext) response.Response {
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, 0, 0)
}
// PUT /api/org/preferences
func UpdateOrgPreferences(c *models.ReqContext, dtoCmd dtos.UpdatePrefsCmd) response.Response {
return updatePreferencesFor(c.OrgId, 0, 0, &dtoCmd)
func (hs *HTTPServer) UpdateOrgPreferences(c *models.ReqContext, dtoCmd dtos.UpdatePrefsCmd) response.Response {
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, 0, 0, &dtoCmd)
}
+2 -2
View File
@@ -155,7 +155,7 @@ func (hs *HTTPServer) GetTeamPreferences(c *models.ReqContext) response.Response
return response.Error(403, "Not allowed to view team preferences.", err)
}
return getPreferencesFor(orgId, 0, teamId)
return hs.getPreferencesFor(c.Req.Context(), orgId, 0, teamId)
}
// PUT /api/teams/:teamId/preferences
@@ -167,7 +167,7 @@ func (hs *HTTPServer) UpdateTeamPreferences(c *models.ReqContext, dtoCmd dtos.Up
return response.Error(403, "Not allowed to update team preferences.", err)
}
return updatePreferencesFor(orgId, 0, teamId, &dtoCmd)
return hs.updatePreferencesFor(c.Req.Context(), orgId, 0, teamId, &dtoCmd)
}
// createTeam creates a team.