diff --git a/pkg/middleware/auth.go b/pkg/middleware/auth.go index 27e2c2e0cf2..4f491cfca2b 100644 --- a/pkg/middleware/auth.go +++ b/pkg/middleware/auth.go @@ -6,7 +6,7 @@ import ( macaron "gopkg.in/macaron.v1" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" ) @@ -16,7 +16,7 @@ type AuthOptions struct { ReqSignedIn bool } -func getApiKey(c *m.ReqContext) string { +func getApiKey(c *models.ReqContext) string { header := c.Req.Header.Get("Authorization") parts := strings.SplitN(header, " ", 2) if len(parts) == 2 && parts[0] == "Bearer" { @@ -32,7 +32,7 @@ func getApiKey(c *m.ReqContext) string { return "" } -func accessForbidden(c *m.ReqContext) { +func accessForbidden(c *models.ReqContext) { if c.IsApiRequest() { c.JsonApiErr(403, "Permission denied", nil) return @@ -41,7 +41,7 @@ func accessForbidden(c *m.ReqContext) { c.Redirect(setting.AppSubUrl + "/") } -func notAuthorized(c *m.ReqContext) { +func notAuthorized(c *models.ReqContext) { if c.IsApiRequest() { c.JsonApiErr(401, "Unauthorized", nil) return @@ -52,14 +52,14 @@ func notAuthorized(c *m.ReqContext) { c.Redirect(setting.AppSubUrl + "/login") } -func EnsureEditorOrViewerCanEdit(c *m.ReqContext) { - if !c.SignedInUser.HasRole(m.ROLE_EDITOR) && !setting.ViewersCanEdit { +func EnsureEditorOrViewerCanEdit(c *models.ReqContext) { + if !c.SignedInUser.HasRole(models.ROLE_EDITOR) && !setting.ViewersCanEdit { accessForbidden(c) } } -func RoleAuth(roles ...m.RoleType) macaron.Handler { - return func(c *m.ReqContext) { +func RoleAuth(roles ...models.RoleType) macaron.Handler { + return func(c *models.ReqContext) { ok := false for _, role := range roles { if role == c.OrgRole { @@ -74,7 +74,7 @@ func RoleAuth(roles ...m.RoleType) macaron.Handler { } func Auth(options *AuthOptions) macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { if !c.IsSignedIn && options.ReqSignedIn && !c.AllowAnonymous { notAuthorized(c) return @@ -93,8 +93,8 @@ func Auth(options *AuthOptions) macaron.Handler { // Intended for when feature flags open up access to APIs that // are otherwise only available to admins. func AdminOrFeatureEnabled(enabled bool) macaron.Handler { - return func(c *m.ReqContext) { - if c.OrgRole == m.ROLE_ADMIN { + return func(c *models.ReqContext) { + if c.OrgRole == models.ROLE_ADMIN { return } @@ -105,7 +105,7 @@ func AdminOrFeatureEnabled(enabled bool) macaron.Handler { } func SnapshotPublicModeOrSignedIn() macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { if setting.SnapshotPublicMode { return } diff --git a/pkg/middleware/auth_proxy.go b/pkg/middleware/auth_proxy.go index 75a72de49a6..1fa48b52a2d 100644 --- a/pkg/middleware/auth_proxy.go +++ b/pkg/middleware/auth_proxy.go @@ -4,13 +4,13 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/remotecache" authproxy "github.com/grafana/grafana/pkg/middleware/auth_proxy" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) var header = setting.AuthProxyHeaderName -func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *m.ReqContext, orgID int64) bool { +func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqContext, orgID int64) bool { username := ctx.Req.Header.Get(header) auth := authproxy.New(&authproxy.Options{ Store: store, diff --git a/pkg/middleware/dashboard_redirect.go b/pkg/middleware/dashboard_redirect.go index 2edf04d543e..2bf4da3c35b 100644 --- a/pkg/middleware/dashboard_redirect.go +++ b/pkg/middleware/dashboard_redirect.go @@ -5,23 +5,23 @@ import ( "strings" "github.com/grafana/grafana/pkg/bus" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "gopkg.in/macaron.v1" ) func getDashboardURLBySlug(orgID int64, slug string) (string, error) { - query := m.GetDashboardQuery{Slug: slug, OrgId: orgID} + query := models.GetDashboardQuery{Slug: slug, OrgId: orgID} if err := bus.Dispatch(&query); err != nil { - return "", m.ErrDashboardNotFound + return "", models.ErrDashboardNotFound } - return m.GetDashboardUrl(query.Result.Uid, query.Result.Slug), nil + return models.GetDashboardUrl(query.Result.Uid, query.Result.Slug), nil } func RedirectFromLegacyDashboardURL() macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { slug := c.Params("slug") if slug != "" { @@ -35,7 +35,7 @@ func RedirectFromLegacyDashboardURL() macaron.Handler { } func RedirectFromLegacyDashboardSoloURL() macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { slug := c.Params("slug") renderRequest := c.QueryBool("render") diff --git a/pkg/middleware/dashboard_redirect_test.go b/pkg/middleware/dashboard_redirect_test.go index ca4ea9c84b2..38115e37385 100644 --- a/pkg/middleware/dashboard_redirect_test.go +++ b/pkg/middleware/dashboard_redirect_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/grafana/grafana/pkg/bus" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/util" . "github.com/smartystreets/goconvey/convey" ) @@ -16,13 +16,13 @@ func TestMiddlewareDashboardRedirect(t *testing.T) { redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardURL() redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloURL() - fakeDash := m.NewDashboard("Child dash") + fakeDash := models.NewDashboard("Child dash") fakeDash.Id = 1 fakeDash.FolderId = 1 fakeDash.HasAcl = false fakeDash.Uid = util.GenerateShortUID() - bus.AddHandler("test", func(query *m.GetDashboardQuery) error { + bus.AddHandler("test", func(query *models.GetDashboardQuery) error { query.Result = fakeDash return nil }) @@ -35,7 +35,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) { Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { So(sc.resp.Code, ShouldEqual, 301) redirectURL, _ := sc.resp.Result().Location() - So(redirectURL.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)) + So(redirectURL.Path, ShouldEqual, models.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)) So(len(redirectURL.Query()), ShouldEqual, 2) }) }) @@ -48,7 +48,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) { Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { So(sc.resp.Code, ShouldEqual, 301) redirectURL, _ := sc.resp.Result().Location() - expectedURL := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug) + expectedURL := models.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug) expectedURL = strings.Replace(expectedURL, "/d/", "/d-solo/", 1) So(redirectURL.Path, ShouldEqual, expectedURL) So(len(redirectURL.Query()), ShouldEqual, 2) diff --git a/pkg/middleware/headers.go b/pkg/middleware/headers.go index 28c623d74b0..96c670468e9 100644 --- a/pkg/middleware/headers.go +++ b/pkg/middleware/headers.go @@ -1,14 +1,14 @@ package middleware import ( - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" macaron "gopkg.in/macaron.v1" ) const HeaderNameNoBackendCache = "X-Grafana-NoCache" func HandleNoCacheHeader() macaron.Handler { - return func(ctx *m.ReqContext) { + return func(ctx *models.ReqContext) { ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true" } } diff --git a/pkg/middleware/logger.go b/pkg/middleware/logger.go index 2c63810b9c8..8befc7df4aa 100644 --- a/pkg/middleware/logger.go +++ b/pkg/middleware/logger.go @@ -19,7 +19,7 @@ import ( "net/http" "time" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "github.com/prometheus/client_golang/prometheus" "gopkg.in/macaron.v1" @@ -48,7 +48,7 @@ func Logger() macaron.Handler { } if ctx, ok := c.Data["ctx"]; ok { - ctxTyped := ctx.(*m.ReqContext) + ctxTyped := ctx.(*models.ReqContext) if status == 500 { ctxTyped.Logger.Error("Request Completed", "method", req.Method, "path", req.URL.Path, "status", status, "remote_addr", c.RemoteAddr(), "time_ms", int64(timeTakenMs), "size", rw.Size(), "referer", req.Referer()) } else { diff --git a/pkg/middleware/org_redirect.go b/pkg/middleware/org_redirect.go index ca63733946c..491ba541185 100644 --- a/pkg/middleware/org_redirect.go +++ b/pkg/middleware/org_redirect.go @@ -7,11 +7,13 @@ import ( "strings" "github.com/grafana/grafana/pkg/bus" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "gopkg.in/macaron.v1" ) +// OrgRedirect changes org and redirects users if the +// querystring `orgId` doesn't match the active org. func OrgRedirect() macaron.Handler { return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) { orgIdValue := req.URL.Query().Get("orgId") @@ -21,7 +23,7 @@ func OrgRedirect() macaron.Handler { return } - ctx, ok := c.Data["ctx"].(*m.ReqContext) + ctx, ok := c.Data["ctx"].(*models.ReqContext) if !ok || !ctx.IsSignedIn { return } @@ -30,7 +32,7 @@ func OrgRedirect() macaron.Handler { return } - cmd := m.SetUsingOrgCommand{UserId: ctx.UserId, OrgId: orgId} + cmd := models.SetUsingOrgCommand{UserId: ctx.UserId, OrgId: orgId} if err := bus.Dispatch(&cmd); err != nil { if ctx.IsApiRequest() { ctx.JsonApiErr(404, "Not found", nil) diff --git a/pkg/middleware/org_redirect_test.go b/pkg/middleware/org_redirect_test.go index e74b6e8451c..86f9136356d 100644 --- a/pkg/middleware/org_redirect_test.go +++ b/pkg/middleware/org_redirect_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/grafana/grafana/pkg/bus" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" . "github.com/smartystreets/goconvey/convey" ) @@ -15,17 +15,17 @@ func TestOrgRedirectMiddleware(t *testing.T) { Convey("Can redirect to correct org", t, func() { middlewareScenario(t, "when setting a correct org for the user", func(sc *scenarioContext) { sc.withTokenSessionCookie("token") - bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error { + bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error { return nil }) - bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error { - query.Result = &m.SignedInUser{OrgId: 1, UserId: 12} + bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error { + query.Result = &models.SignedInUser{OrgId: 1, UserId: 12} return nil }) - sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) { - return &m.UserToken{ + sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) { + return &models.UserToken{ UserId: 0, UnhashedToken: "", }, nil @@ -41,17 +41,17 @@ func TestOrgRedirectMiddleware(t *testing.T) { middlewareScenario(t, "when setting an invalid org for user", func(sc *scenarioContext) { sc.withTokenSessionCookie("token") - bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error { + bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error { return fmt.Errorf("") }) - bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error { - query.Result = &m.SignedInUser{OrgId: 1, UserId: 12} + bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error { + query.Result = &models.SignedInUser{OrgId: 1, UserId: 12} return nil }) - sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) { - return &m.UserToken{ + sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) { + return &models.UserToken{ UserId: 12, UnhashedToken: "", }, nil diff --git a/pkg/middleware/perf.go b/pkg/middleware/perf.go index 5b6ab6f2d0a..043f704222a 100644 --- a/pkg/middleware/perf.go +++ b/pkg/middleware/perf.go @@ -3,12 +3,11 @@ package middleware import ( "net/http" + "github.com/grafana/grafana/pkg/models" "gopkg.in/macaron.v1" - - m "github.com/grafana/grafana/pkg/models" ) func MeasureRequestTime() macaron.Handler { - return func(res http.ResponseWriter, req *http.Request, c *m.ReqContext) { + return func(res http.ResponseWriter, req *http.Request, c *models.ReqContext) { } } diff --git a/pkg/middleware/quota.go b/pkg/middleware/quota.go index 51f906e2c92..69895c26b55 100644 --- a/pkg/middleware/quota.go +++ b/pkg/middleware/quota.go @@ -5,7 +5,7 @@ import ( "gopkg.in/macaron.v1" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/quota" ) @@ -13,7 +13,7 @@ import ( func Quota(quotaService *quota.QuotaService) func(target string) macaron.Handler { //https://open.spotify.com/track/7bZSoBEAEEUsGEuLOf94Jm?si=T1Tdju5qRSmmR0zph_6RBw fuuuuunky return func(target string) macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { limitReached, err := quotaService.QuotaReached(c, target) if err != nil { c.JsonApiErr(500, "failed to get quota", err) diff --git a/pkg/middleware/quota_test.go b/pkg/middleware/quota_test.go index c6c8a1fd4d3..174d9bf7286 100644 --- a/pkg/middleware/quota_test.go +++ b/pkg/middleware/quota_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/grafana/grafana/pkg/bus" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/auth" "github.com/grafana/grafana/pkg/services/quota" "github.com/grafana/grafana/pkg/setting" @@ -44,8 +44,8 @@ func TestMiddlewareQuota(t *testing.T) { QuotaFn := Quota(qs) middlewareScenario(t, "with user not logged in", func(sc *scenarioContext) { - bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error { - query.Result = &m.GlobalQuotaDTO{ + bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error { + query.Result = &models.GlobalQuotaDTO{ Target: query.Target, Limit: query.Default, Used: 4, @@ -83,20 +83,20 @@ func TestMiddlewareQuota(t *testing.T) { middlewareScenario(t, "with user logged in", func(sc *scenarioContext) { sc.withTokenSessionCookie("token") - bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error { - query.Result = &m.SignedInUser{OrgId: 2, UserId: 12} + bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error { + query.Result = &models.SignedInUser{OrgId: 2, UserId: 12} return nil }) - sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) { - return &m.UserToken{ + sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) { + return &models.UserToken{ UserId: 12, UnhashedToken: "", }, nil } - bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error { - query.Result = &m.GlobalQuotaDTO{ + bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error { + query.Result = &models.GlobalQuotaDTO{ Target: query.Target, Limit: query.Default, Used: 4, @@ -104,8 +104,8 @@ func TestMiddlewareQuota(t *testing.T) { return nil }) - bus.AddHandler("userQuota", func(query *m.GetUserQuotaByTargetQuery) error { - query.Result = &m.UserQuotaDTO{ + bus.AddHandler("userQuota", func(query *models.GetUserQuotaByTargetQuery) error { + query.Result = &models.UserQuotaDTO{ Target: query.Target, Limit: query.Default, Used: 4, @@ -113,8 +113,8 @@ func TestMiddlewareQuota(t *testing.T) { return nil }) - bus.AddHandler("orgQuota", func(query *m.GetOrgQuotaByTargetQuery) error { - query.Result = &m.OrgQuotaDTO{ + bus.AddHandler("orgQuota", func(query *models.GetOrgQuotaByTargetQuery) error { + query.Result = &models.OrgQuotaDTO{ Target: query.Target, Limit: query.Default, Used: 4, diff --git a/pkg/middleware/recovery.go b/pkg/middleware/recovery.go index 7e8a5282b1b..5cc59923cdd 100644 --- a/pkg/middleware/recovery.go +++ b/pkg/middleware/recovery.go @@ -25,7 +25,7 @@ import ( "gopkg.in/macaron.v1" "github.com/grafana/grafana/pkg/infra/log" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" ) @@ -106,7 +106,7 @@ func Recovery() macaron.Handler { panicLogger := log.Root // try to get request logger if ctx, ok := c.Data["ctx"]; ok { - ctxTyped := ctx.(*m.ReqContext) + ctxTyped := ctx.(*models.ReqContext) panicLogger = ctxTyped.Logger } @@ -138,7 +138,7 @@ func Recovery() macaron.Handler { c.Data["ErrorMsg"] = string(stack) } - ctx, ok := c.Data["ctx"].(*m.ReqContext) + ctx, ok := c.Data["ctx"].(*models.ReqContext) if ok && ctx.IsApiRequest() { resp := make(map[string]interface{}) diff --git a/pkg/middleware/recovery_test.go b/pkg/middleware/recovery_test.go index 14844da7857..0ff5908ff8a 100644 --- a/pkg/middleware/recovery_test.go +++ b/pkg/middleware/recovery_test.go @@ -6,7 +6,7 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/infra/remotecache" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/auth" "github.com/grafana/grafana/pkg/setting" . "github.com/smartystreets/goconvey/convey" @@ -42,7 +42,7 @@ func TestRecoveryMiddleware(t *testing.T) { }) } -func PanicHandler(c *m.ReqContext) { +func PanicHandler(c *models.ReqContext) { panic("Handler has panicked") } @@ -72,7 +72,7 @@ func recoveryScenario(t *testing.T, desc string, url string, fn scenarioFunc) { // mock out gc goroutine sc.m.Use(OrgRedirect()) - sc.defaultHandler = func(c *m.ReqContext) { + sc.defaultHandler = func(c *models.ReqContext) { sc.context = c if sc.handlerFunc != nil { sc.handlerFunc(sc.context) diff --git a/pkg/middleware/render_auth.go b/pkg/middleware/render_auth.go index bf402872c39..ff5e33c842e 100644 --- a/pkg/middleware/render_auth.go +++ b/pkg/middleware/render_auth.go @@ -3,12 +3,11 @@ package middleware import ( "time" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/rendering" - - m "github.com/grafana/grafana/pkg/models" ) -func initContextWithRenderAuth(ctx *m.ReqContext, renderService rendering.Service) bool { +func initContextWithRenderAuth(ctx *models.ReqContext, renderService rendering.Service) bool { key := ctx.GetCookie("renderKey") if key == "" { return false @@ -21,10 +20,10 @@ func initContextWithRenderAuth(ctx *m.ReqContext, renderService rendering.Servic } ctx.IsSignedIn = true - ctx.SignedInUser = &m.SignedInUser{ + ctx.SignedInUser = &models.SignedInUser{ OrgId: renderUser.OrgID, UserId: renderUser.UserID, - OrgRole: m.RoleType(renderUser.OrgRole), + OrgRole: models.RoleType(renderUser.OrgRole), } ctx.IsRenderCall = true ctx.LastSeenAt = time.Now() diff --git a/pkg/middleware/validate_host.go b/pkg/middleware/validate_host.go index 63c4b3000e9..ec2ac8253eb 100644 --- a/pkg/middleware/validate_host.go +++ b/pkg/middleware/validate_host.go @@ -3,13 +3,13 @@ package middleware import ( "strings" - m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "gopkg.in/macaron.v1" ) func ValidateHostHeader(domain string) macaron.Handler { - return func(c *m.ReqContext) { + return func(c *models.ReqContext) { // ignore local render calls if c.IsRenderCall { return