mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 02:02:33 +08:00
chore: avoid aliasing models in middleware (#22484)
This commit is contained in:
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
macaron "gopkg.in/macaron.v1"
|
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/setting"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
@ -16,7 +16,7 @@ type AuthOptions struct {
|
|||||||
ReqSignedIn bool
|
ReqSignedIn bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func getApiKey(c *m.ReqContext) string {
|
func getApiKey(c *models.ReqContext) string {
|
||||||
header := c.Req.Header.Get("Authorization")
|
header := c.Req.Header.Get("Authorization")
|
||||||
parts := strings.SplitN(header, " ", 2)
|
parts := strings.SplitN(header, " ", 2)
|
||||||
if len(parts) == 2 && parts[0] == "Bearer" {
|
if len(parts) == 2 && parts[0] == "Bearer" {
|
||||||
@ -32,7 +32,7 @@ func getApiKey(c *m.ReqContext) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func accessForbidden(c *m.ReqContext) {
|
func accessForbidden(c *models.ReqContext) {
|
||||||
if c.IsApiRequest() {
|
if c.IsApiRequest() {
|
||||||
c.JsonApiErr(403, "Permission denied", nil)
|
c.JsonApiErr(403, "Permission denied", nil)
|
||||||
return
|
return
|
||||||
@ -41,7 +41,7 @@ func accessForbidden(c *m.ReqContext) {
|
|||||||
c.Redirect(setting.AppSubUrl + "/")
|
c.Redirect(setting.AppSubUrl + "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func notAuthorized(c *m.ReqContext) {
|
func notAuthorized(c *models.ReqContext) {
|
||||||
if c.IsApiRequest() {
|
if c.IsApiRequest() {
|
||||||
c.JsonApiErr(401, "Unauthorized", nil)
|
c.JsonApiErr(401, "Unauthorized", nil)
|
||||||
return
|
return
|
||||||
@ -52,14 +52,14 @@ func notAuthorized(c *m.ReqContext) {
|
|||||||
c.Redirect(setting.AppSubUrl + "/login")
|
c.Redirect(setting.AppSubUrl + "/login")
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnsureEditorOrViewerCanEdit(c *m.ReqContext) {
|
func EnsureEditorOrViewerCanEdit(c *models.ReqContext) {
|
||||||
if !c.SignedInUser.HasRole(m.ROLE_EDITOR) && !setting.ViewersCanEdit {
|
if !c.SignedInUser.HasRole(models.ROLE_EDITOR) && !setting.ViewersCanEdit {
|
||||||
accessForbidden(c)
|
accessForbidden(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RoleAuth(roles ...m.RoleType) macaron.Handler {
|
func RoleAuth(roles ...models.RoleType) macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
ok := false
|
ok := false
|
||||||
for _, role := range roles {
|
for _, role := range roles {
|
||||||
if role == c.OrgRole {
|
if role == c.OrgRole {
|
||||||
@ -74,7 +74,7 @@ func RoleAuth(roles ...m.RoleType) macaron.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Auth(options *AuthOptions) 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 {
|
if !c.IsSignedIn && options.ReqSignedIn && !c.AllowAnonymous {
|
||||||
notAuthorized(c)
|
notAuthorized(c)
|
||||||
return
|
return
|
||||||
@ -93,8 +93,8 @@ func Auth(options *AuthOptions) macaron.Handler {
|
|||||||
// Intended for when feature flags open up access to APIs that
|
// Intended for when feature flags open up access to APIs that
|
||||||
// are otherwise only available to admins.
|
// are otherwise only available to admins.
|
||||||
func AdminOrFeatureEnabled(enabled bool) macaron.Handler {
|
func AdminOrFeatureEnabled(enabled bool) macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
if c.OrgRole == m.ROLE_ADMIN {
|
if c.OrgRole == models.ROLE_ADMIN {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +105,7 @@ func AdminOrFeatureEnabled(enabled bool) macaron.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SnapshotPublicModeOrSignedIn() macaron.Handler {
|
func SnapshotPublicModeOrSignedIn() macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
if setting.SnapshotPublicMode {
|
if setting.SnapshotPublicMode {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,13 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/remotecache"
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
||||||
authproxy "github.com/grafana/grafana/pkg/middleware/auth_proxy"
|
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"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
var header = setting.AuthProxyHeaderName
|
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)
|
username := ctx.Req.Header.Get(header)
|
||||||
auth := authproxy.New(&authproxy.Options{
|
auth := authproxy.New(&authproxy.Options{
|
||||||
Store: store,
|
Store: store,
|
||||||
|
@ -5,23 +5,23 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"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"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getDashboardURLBySlug(orgID int64, slug string) (string, error) {
|
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 {
|
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 {
|
func RedirectFromLegacyDashboardURL() macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
slug := c.Params("slug")
|
slug := c.Params("slug")
|
||||||
|
|
||||||
if slug != "" {
|
if slug != "" {
|
||||||
@ -35,7 +35,7 @@ func RedirectFromLegacyDashboardURL() macaron.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RedirectFromLegacyDashboardSoloURL() macaron.Handler {
|
func RedirectFromLegacyDashboardSoloURL() macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
slug := c.Params("slug")
|
slug := c.Params("slug")
|
||||||
renderRequest := c.QueryBool("render")
|
renderRequest := c.QueryBool("render")
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"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/grafana/grafana/pkg/util"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
@ -16,13 +16,13 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
|
|||||||
redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardURL()
|
redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardURL()
|
||||||
redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloURL()
|
redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloURL()
|
||||||
|
|
||||||
fakeDash := m.NewDashboard("Child dash")
|
fakeDash := models.NewDashboard("Child dash")
|
||||||
fakeDash.Id = 1
|
fakeDash.Id = 1
|
||||||
fakeDash.FolderId = 1
|
fakeDash.FolderId = 1
|
||||||
fakeDash.HasAcl = false
|
fakeDash.HasAcl = false
|
||||||
fakeDash.Uid = util.GenerateShortUID()
|
fakeDash.Uid = util.GenerateShortUID()
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
|
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||||
query.Result = fakeDash
|
query.Result = fakeDash
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -35,7 +35,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
|
|||||||
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
|
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
|
||||||
So(sc.resp.Code, ShouldEqual, 301)
|
So(sc.resp.Code, ShouldEqual, 301)
|
||||||
redirectURL, _ := sc.resp.Result().Location()
|
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)
|
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() {
|
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
|
||||||
So(sc.resp.Code, ShouldEqual, 301)
|
So(sc.resp.Code, ShouldEqual, 301)
|
||||||
redirectURL, _ := sc.resp.Result().Location()
|
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)
|
expectedURL = strings.Replace(expectedURL, "/d/", "/d-solo/", 1)
|
||||||
So(redirectURL.Path, ShouldEqual, expectedURL)
|
So(redirectURL.Path, ShouldEqual, expectedURL)
|
||||||
So(len(redirectURL.Query()), ShouldEqual, 2)
|
So(len(redirectURL.Query()), ShouldEqual, 2)
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
macaron "gopkg.in/macaron.v1"
|
macaron "gopkg.in/macaron.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HeaderNameNoBackendCache = "X-Grafana-NoCache"
|
const HeaderNameNoBackendCache = "X-Grafana-NoCache"
|
||||||
|
|
||||||
func HandleNoCacheHeader() macaron.Handler {
|
func HandleNoCacheHeader() macaron.Handler {
|
||||||
return func(ctx *m.ReqContext) {
|
return func(ctx *models.ReqContext) {
|
||||||
ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true"
|
ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
@ -48,7 +48,7 @@ func Logger() macaron.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctx, ok := c.Data["ctx"]; ok {
|
if ctx, ok := c.Data["ctx"]; ok {
|
||||||
ctxTyped := ctx.(*m.ReqContext)
|
ctxTyped := ctx.(*models.ReqContext)
|
||||||
if status == 500 {
|
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())
|
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 {
|
} else {
|
||||||
|
@ -7,11 +7,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"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"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// OrgRedirect changes org and redirects users if the
|
||||||
|
// querystring `orgId` doesn't match the active org.
|
||||||
func OrgRedirect() macaron.Handler {
|
func OrgRedirect() macaron.Handler {
|
||||||
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
|
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
|
||||||
orgIdValue := req.URL.Query().Get("orgId")
|
orgIdValue := req.URL.Query().Get("orgId")
|
||||||
@ -21,7 +23,7 @@ func OrgRedirect() macaron.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, ok := c.Data["ctx"].(*m.ReqContext)
|
ctx, ok := c.Data["ctx"].(*models.ReqContext)
|
||||||
if !ok || !ctx.IsSignedIn {
|
if !ok || !ctx.IsSignedIn {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -30,7 +32,7 @@ func OrgRedirect() macaron.Handler {
|
|||||||
return
|
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 err := bus.Dispatch(&cmd); err != nil {
|
||||||
if ctx.IsApiRequest() {
|
if ctx.IsApiRequest() {
|
||||||
ctx.JsonApiErr(404, "Not found", nil)
|
ctx.JsonApiErr(404, "Not found", nil)
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,17 +15,17 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
|||||||
Convey("Can redirect to correct org", t, func() {
|
Convey("Can redirect to correct org", t, func() {
|
||||||
middlewareScenario(t, "when setting a correct org for the user", func(sc *scenarioContext) {
|
middlewareScenario(t, "when setting a correct org for the user", func(sc *scenarioContext) {
|
||||||
sc.withTokenSessionCookie("token")
|
sc.withTokenSessionCookie("token")
|
||||||
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
|
||||||
query.Result = &m.SignedInUser{OrgId: 1, UserId: 12}
|
query.Result = &models.SignedInUser{OrgId: 1, UserId: 12}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
|
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
|
||||||
return &m.UserToken{
|
return &models.UserToken{
|
||||||
UserId: 0,
|
UserId: 0,
|
||||||
UnhashedToken: "",
|
UnhashedToken: "",
|
||||||
}, nil
|
}, nil
|
||||||
@ -41,17 +41,17 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
|||||||
|
|
||||||
middlewareScenario(t, "when setting an invalid org for user", func(sc *scenarioContext) {
|
middlewareScenario(t, "when setting an invalid org for user", func(sc *scenarioContext) {
|
||||||
sc.withTokenSessionCookie("token")
|
sc.withTokenSessionCookie("token")
|
||||||
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
bus.AddHandler("test", func(query *models.SetUsingOrgCommand) error {
|
||||||
return fmt.Errorf("")
|
return fmt.Errorf("")
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
|
||||||
query.Result = &m.SignedInUser{OrgId: 1, UserId: 12}
|
query.Result = &models.SignedInUser{OrgId: 1, UserId: 12}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
|
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
|
||||||
return &m.UserToken{
|
return &models.UserToken{
|
||||||
UserId: 12,
|
UserId: 12,
|
||||||
UnhashedToken: "",
|
UnhashedToken: "",
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -3,12 +3,11 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
|
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MeasureRequestTime() macaron.Handler {
|
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) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
|
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ import (
|
|||||||
func Quota(quotaService *quota.QuotaService) func(target string) macaron.Handler {
|
func Quota(quotaService *quota.QuotaService) func(target string) macaron.Handler {
|
||||||
//https://open.spotify.com/track/7bZSoBEAEEUsGEuLOf94Jm?si=T1Tdju5qRSmmR0zph_6RBw fuuuuunky
|
//https://open.spotify.com/track/7bZSoBEAEEUsGEuLOf94Jm?si=T1Tdju5qRSmmR0zph_6RBw fuuuuunky
|
||||||
return func(target string) macaron.Handler {
|
return func(target string) macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
limitReached, err := quotaService.QuotaReached(c, target)
|
limitReached, err := quotaService.QuotaReached(c, target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "failed to get quota", err)
|
c.JsonApiErr(500, "failed to get quota", err)
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"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/auth"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
@ -44,8 +44,8 @@ func TestMiddlewareQuota(t *testing.T) {
|
|||||||
QuotaFn := Quota(qs)
|
QuotaFn := Quota(qs)
|
||||||
|
|
||||||
middlewareScenario(t, "with user not logged in", func(sc *scenarioContext) {
|
middlewareScenario(t, "with user not logged in", func(sc *scenarioContext) {
|
||||||
bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error {
|
bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error {
|
||||||
query.Result = &m.GlobalQuotaDTO{
|
query.Result = &models.GlobalQuotaDTO{
|
||||||
Target: query.Target,
|
Target: query.Target,
|
||||||
Limit: query.Default,
|
Limit: query.Default,
|
||||||
Used: 4,
|
Used: 4,
|
||||||
@ -83,20 +83,20 @@ func TestMiddlewareQuota(t *testing.T) {
|
|||||||
|
|
||||||
middlewareScenario(t, "with user logged in", func(sc *scenarioContext) {
|
middlewareScenario(t, "with user logged in", func(sc *scenarioContext) {
|
||||||
sc.withTokenSessionCookie("token")
|
sc.withTokenSessionCookie("token")
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
bus.AddHandler("test", func(query *models.GetSignedInUserQuery) error {
|
||||||
query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
|
query.Result = &models.SignedInUser{OrgId: 2, UserId: 12}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
|
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
|
||||||
return &m.UserToken{
|
return &models.UserToken{
|
||||||
UserId: 12,
|
UserId: 12,
|
||||||
UnhashedToken: "",
|
UnhashedToken: "",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error {
|
bus.AddHandler("globalQuota", func(query *models.GetGlobalQuotaByTargetQuery) error {
|
||||||
query.Result = &m.GlobalQuotaDTO{
|
query.Result = &models.GlobalQuotaDTO{
|
||||||
Target: query.Target,
|
Target: query.Target,
|
||||||
Limit: query.Default,
|
Limit: query.Default,
|
||||||
Used: 4,
|
Used: 4,
|
||||||
@ -104,8 +104,8 @@ func TestMiddlewareQuota(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("userQuota", func(query *m.GetUserQuotaByTargetQuery) error {
|
bus.AddHandler("userQuota", func(query *models.GetUserQuotaByTargetQuery) error {
|
||||||
query.Result = &m.UserQuotaDTO{
|
query.Result = &models.UserQuotaDTO{
|
||||||
Target: query.Target,
|
Target: query.Target,
|
||||||
Limit: query.Default,
|
Limit: query.Default,
|
||||||
Used: 4,
|
Used: 4,
|
||||||
@ -113,8 +113,8 @@ func TestMiddlewareQuota(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("orgQuota", func(query *m.GetOrgQuotaByTargetQuery) error {
|
bus.AddHandler("orgQuota", func(query *models.GetOrgQuotaByTargetQuery) error {
|
||||||
query.Result = &m.OrgQuotaDTO{
|
query.Result = &models.OrgQuotaDTO{
|
||||||
Target: query.Target,
|
Target: query.Target,
|
||||||
Limit: query.Default,
|
Limit: query.Default,
|
||||||
Used: 4,
|
Used: 4,
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"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"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ func Recovery() macaron.Handler {
|
|||||||
panicLogger := log.Root
|
panicLogger := log.Root
|
||||||
// try to get request logger
|
// try to get request logger
|
||||||
if ctx, ok := c.Data["ctx"]; ok {
|
if ctx, ok := c.Data["ctx"]; ok {
|
||||||
ctxTyped := ctx.(*m.ReqContext)
|
ctxTyped := ctx.(*models.ReqContext)
|
||||||
panicLogger = ctxTyped.Logger
|
panicLogger = ctxTyped.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func Recovery() macaron.Handler {
|
|||||||
c.Data["ErrorMsg"] = string(stack)
|
c.Data["ErrorMsg"] = string(stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, ok := c.Data["ctx"].(*m.ReqContext)
|
ctx, ok := c.Data["ctx"].(*models.ReqContext)
|
||||||
|
|
||||||
if ok && ctx.IsApiRequest() {
|
if ok && ctx.IsApiRequest() {
|
||||||
resp := make(map[string]interface{})
|
resp := make(map[string]interface{})
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/remotecache"
|
"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/services/auth"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "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")
|
panic("Handler has panicked")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ func recoveryScenario(t *testing.T, desc string, url string, fn scenarioFunc) {
|
|||||||
// mock out gc goroutine
|
// mock out gc goroutine
|
||||||
sc.m.Use(OrgRedirect())
|
sc.m.Use(OrgRedirect())
|
||||||
|
|
||||||
sc.defaultHandler = func(c *m.ReqContext) {
|
sc.defaultHandler = func(c *models.ReqContext) {
|
||||||
sc.context = c
|
sc.context = c
|
||||||
if sc.handlerFunc != nil {
|
if sc.handlerFunc != nil {
|
||||||
sc.handlerFunc(sc.context)
|
sc.handlerFunc(sc.context)
|
||||||
|
@ -3,12 +3,11 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/rendering"
|
"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")
|
key := ctx.GetCookie("renderKey")
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return false
|
return false
|
||||||
@ -21,10 +20,10 @@ func initContextWithRenderAuth(ctx *m.ReqContext, renderService rendering.Servic
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.IsSignedIn = true
|
ctx.IsSignedIn = true
|
||||||
ctx.SignedInUser = &m.SignedInUser{
|
ctx.SignedInUser = &models.SignedInUser{
|
||||||
OrgId: renderUser.OrgID,
|
OrgId: renderUser.OrgID,
|
||||||
UserId: renderUser.UserID,
|
UserId: renderUser.UserID,
|
||||||
OrgRole: m.RoleType(renderUser.OrgRole),
|
OrgRole: models.RoleType(renderUser.OrgRole),
|
||||||
}
|
}
|
||||||
ctx.IsRenderCall = true
|
ctx.IsRenderCall = true
|
||||||
ctx.LastSeenAt = time.Now()
|
ctx.LastSeenAt = time.Now()
|
||||||
|
@ -3,13 +3,13 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ValidateHostHeader(domain string) macaron.Handler {
|
func ValidateHostHeader(domain string) macaron.Handler {
|
||||||
return func(c *m.ReqContext) {
|
return func(c *models.ReqContext) {
|
||||||
// ignore local render calls
|
// ignore local render calls
|
||||||
if c.IsRenderCall {
|
if c.IsRenderCall {
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user