rename Context to ReqContext
This commit is contained in:
@@ -16,7 +16,7 @@ type AuthOptions struct {
|
||||
ReqSignedIn bool
|
||||
}
|
||||
|
||||
func getRequestUserId(c *m.Context) int64 {
|
||||
func getRequestUserId(c *m.ReqContext) int64 {
|
||||
userId := c.Session.Get(session.SESS_KEY_USERID)
|
||||
|
||||
if userId != nil {
|
||||
@@ -26,7 +26,7 @@ func getRequestUserId(c *m.Context) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func getApiKey(c *m.Context) string {
|
||||
func getApiKey(c *m.ReqContext) string {
|
||||
header := c.Req.Header.Get("Authorization")
|
||||
parts := strings.SplitN(header, " ", 2)
|
||||
if len(parts) == 2 && parts[0] == "Bearer" {
|
||||
@@ -37,7 +37,7 @@ func getApiKey(c *m.Context) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func accessForbidden(c *m.Context) {
|
||||
func accessForbidden(c *m.ReqContext) {
|
||||
if c.IsApiRequest() {
|
||||
c.JsonApiErr(403, "Permission denied", nil)
|
||||
return
|
||||
@@ -46,7 +46,7 @@ func accessForbidden(c *m.Context) {
|
||||
c.Redirect(setting.AppSubUrl + "/")
|
||||
}
|
||||
|
||||
func notAuthorized(c *m.Context) {
|
||||
func notAuthorized(c *m.ReqContext) {
|
||||
if c.IsApiRequest() {
|
||||
c.JsonApiErr(401, "Unauthorized", nil)
|
||||
return
|
||||
@@ -58,7 +58,7 @@ func notAuthorized(c *m.Context) {
|
||||
}
|
||||
|
||||
func RoleAuth(roles ...m.RoleType) macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
ok := false
|
||||
for _, role := range roles {
|
||||
if role == c.OrgRole {
|
||||
@@ -73,7 +73,7 @@ func RoleAuth(roles ...m.RoleType) macaron.Handler {
|
||||
}
|
||||
|
||||
func Auth(options *AuthOptions) macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
if !c.IsSignedIn && options.ReqSignedIn && !c.AllowAnonymous {
|
||||
notAuthorized(c)
|
||||
return
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func initContextWithAuthProxy(ctx *m.Context, orgId int64) bool {
|
||||
func initContextWithAuthProxy(ctx *m.ReqContext, orgId int64) bool {
|
||||
if !setting.AuthProxyEnabled {
|
||||
return false
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func initContextWithAuthProxy(ctx *m.Context, orgId int64) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
var syncGrafanaUserWithLdapUser = func(ctx *m.Context, query *m.GetSignedInUserQuery) error {
|
||||
var syncGrafanaUserWithLdapUser = func(ctx *m.ReqContext, query *m.GetSignedInUserQuery) error {
|
||||
if setting.LdapEnabled {
|
||||
expireEpoch := time.Now().Add(time.Duration(-setting.AuthProxyLdapSyncTtl) * time.Minute).Unix()
|
||||
|
||||
@@ -121,7 +121,7 @@ var syncGrafanaUserWithLdapUser = func(ctx *m.Context, query *m.GetSignedInUserQ
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkAuthenticationProxy(ctx *m.Context, proxyHeaderValue string) error {
|
||||
func checkAuthenticationProxy(ctx *m.ReqContext, proxyHeaderValue string) error {
|
||||
if len(strings.TrimSpace(setting.AuthProxyWhitelist)) > 0 {
|
||||
proxies := strings.Split(setting.AuthProxyWhitelist, ",")
|
||||
remoteAddrSplit := strings.Split(ctx.Req.RemoteAddr, ":")
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestAuthProxyWithLdapEnabled(t *testing.T) {
|
||||
Convey("When session variable lastLdapSync not set, call syncSignedInUser and set lastLdapSync", func() {
|
||||
// arrange
|
||||
sess := mockSession{}
|
||||
ctx := m.Context{Session: &sess}
|
||||
ctx := m.ReqContext{Session: &sess}
|
||||
So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeNil)
|
||||
|
||||
// act
|
||||
@@ -46,7 +46,7 @@ func TestAuthProxyWithLdapEnabled(t *testing.T) {
|
||||
Convey("When session variable not expired, don't sync and don't change session var", func() {
|
||||
// arrange
|
||||
sess := mockSession{}
|
||||
ctx := m.Context{Session: &sess}
|
||||
ctx := m.ReqContext{Session: &sess}
|
||||
now := time.Now().Unix()
|
||||
sess.Set(session.SESS_KEY_LASTLDAPSYNC, now)
|
||||
|
||||
@@ -61,7 +61,7 @@ func TestAuthProxyWithLdapEnabled(t *testing.T) {
|
||||
Convey("When lastldapsync is expired, session variable should be updated", func() {
|
||||
// arrange
|
||||
sess := mockSession{}
|
||||
ctx := m.Context{Session: &sess}
|
||||
ctx := m.ReqContext{Session: &sess}
|
||||
expiredTime := time.Now().Add(time.Duration(-120) * time.Minute).Unix()
|
||||
sess.Set(session.SESS_KEY_LASTLDAPSYNC, expiredTime)
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ func getDashboardUrlBySlug(orgId int64, slug string) (string, error) {
|
||||
}
|
||||
|
||||
func RedirectFromLegacyDashboardUrl() macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
slug := c.Params("slug")
|
||||
|
||||
if slug != "" {
|
||||
@@ -34,7 +34,7 @@ func RedirectFromLegacyDashboardUrl() macaron.Handler {
|
||||
}
|
||||
|
||||
func RedirectFromLegacyDashboardSoloUrl() macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
slug := c.Params("slug")
|
||||
|
||||
if slug != "" {
|
||||
|
||||
@@ -48,7 +48,7 @@ func Logger() macaron.Handler {
|
||||
}
|
||||
|
||||
if ctx, ok := c.Data["ctx"]; ok {
|
||||
ctxTyped := ctx.(*m.Context)
|
||||
ctxTyped := ctx.(*m.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 {
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
func GetContextHandler() macaron.Handler {
|
||||
return func(c *macaron.Context) {
|
||||
ctx := &m.Context{
|
||||
ctx := &m.ReqContext{
|
||||
Context: c,
|
||||
SignedInUser: &m.SignedInUser{},
|
||||
Session: session.GetSession(),
|
||||
@@ -61,7 +61,7 @@ func GetContextHandler() macaron.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
func initContextWithAnonymousUser(ctx *m.Context) bool {
|
||||
func initContextWithAnonymousUser(ctx *m.ReqContext) bool {
|
||||
if !setting.AnonymousEnabled {
|
||||
return false
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func initContextWithAnonymousUser(ctx *m.Context) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func initContextWithUserSessionCookie(ctx *m.Context, orgId int64) bool {
|
||||
func initContextWithUserSessionCookie(ctx *m.ReqContext, orgId int64) bool {
|
||||
// initialize session
|
||||
if err := ctx.Session.Start(ctx.Context); err != nil {
|
||||
ctx.Logger.Error("Failed to start session", "error", err)
|
||||
@@ -104,7 +104,7 @@ func initContextWithUserSessionCookie(ctx *m.Context, orgId int64) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func initContextWithApiKey(ctx *m.Context) bool {
|
||||
func initContextWithApiKey(ctx *m.ReqContext) bool {
|
||||
var keyString string
|
||||
if keyString = getApiKey(ctx); keyString == "" {
|
||||
return false
|
||||
@@ -140,7 +140,7 @@ func initContextWithApiKey(ctx *m.Context) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func initContextWithBasicAuth(ctx *m.Context, orgId int64) bool {
|
||||
func initContextWithBasicAuth(ctx *m.ReqContext, orgId int64) bool {
|
||||
|
||||
if !setting.BasicAuthEnabled {
|
||||
return false
|
||||
@@ -183,7 +183,7 @@ func initContextWithBasicAuth(ctx *m.Context, orgId int64) bool {
|
||||
}
|
||||
|
||||
func AddDefaultResponseHeaders() macaron.Handler {
|
||||
return func(ctx *m.Context) {
|
||||
return func(ctx *m.ReqContext) {
|
||||
if ctx.IsApiRequest() && ctx.Req.Method == "GET" {
|
||||
ctx.Resp.Header().Add("Cache-Control", "no-cache")
|
||||
ctx.Resp.Header().Add("Pragma", "no-cache")
|
||||
|
||||
@@ -131,7 +131,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
|
||||
middlewareScenario("UserId in session", func(sc *scenarioContext) {
|
||||
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.Context) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
||||
}).exec()
|
||||
|
||||
@@ -277,7 +277,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
})
|
||||
|
||||
// create session
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.Context) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
||||
c.Session.Set(session.SESS_KEY_USERID, int64(33))
|
||||
}).exec()
|
||||
|
||||
@@ -301,7 +301,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
setting.LdapEnabled = true
|
||||
|
||||
called := false
|
||||
syncGrafanaUserWithLdapUser = func(ctx *m.Context, query *m.GetSignedInUserQuery) error {
|
||||
syncGrafanaUserWithLdapUser = func(ctx *m.ReqContext, query *m.GetSignedInUserQuery) error {
|
||||
called = true
|
||||
return nil
|
||||
}
|
||||
@@ -342,7 +342,7 @@ func middlewareScenario(desc string, fn scenarioFunc) {
|
||||
sc.m.Use(OrgRedirect())
|
||||
sc.m.Use(AddDefaultResponseHeaders())
|
||||
|
||||
sc.defaultHandler = func(c *m.Context) {
|
||||
sc.defaultHandler = func(c *m.ReqContext) {
|
||||
sc.context = c
|
||||
if sc.handlerFunc != nil {
|
||||
sc.handlerFunc(sc.context)
|
||||
@@ -357,7 +357,7 @@ func middlewareScenario(desc string, fn scenarioFunc) {
|
||||
|
||||
type scenarioContext struct {
|
||||
m *macaron.Macaron
|
||||
context *m.Context
|
||||
context *m.ReqContext
|
||||
resp *httptest.ResponseRecorder
|
||||
apiKey string
|
||||
authHeader string
|
||||
@@ -437,4 +437,4 @@ func (sc *scenarioContext) exec() {
|
||||
}
|
||||
|
||||
type scenarioFunc func(c *scenarioContext)
|
||||
type handlerFunc func(c *m.Context)
|
||||
type handlerFunc func(c *m.ReqContext)
|
||||
|
||||
@@ -22,7 +22,7 @@ func OrgRedirect() macaron.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
ctx, ok := c.Data["ctx"].(*m.Context)
|
||||
ctx, ok := c.Data["ctx"].(*m.ReqContext)
|
||||
if !ok || !ctx.IsSignedIn {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
|
||||
Convey("Can redirect to correct org", t, func() {
|
||||
middlewareScenario("when setting a correct org for the user", func(sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.Context) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
||||
}).exec()
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
||||
})
|
||||
|
||||
middlewareScenario("when setting an invalid org for user", func(sc *scenarioContext) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.Context) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
||||
}).exec()
|
||||
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
)
|
||||
|
||||
func MeasureRequestTime() macaron.Handler {
|
||||
return func(res http.ResponseWriter, req *http.Request, c *m.Context) {
|
||||
return func(res http.ResponseWriter, req *http.Request, c *m.ReqContext) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func Quota(target string) macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
limitReached, err := QuotaReached(c, target)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "failed to get quota", err)
|
||||
@@ -24,7 +24,7 @@ func Quota(target string) macaron.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
func QuotaReached(c *m.Context, target string) (bool, error) {
|
||||
func QuotaReached(c *m.ReqContext, target string) (bool, error) {
|
||||
if !setting.Quota.Enabled {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func TestMiddlewareQuota(t *testing.T) {
|
||||
|
||||
middlewareScenario("with user logged in", func(sc *scenarioContext) {
|
||||
// log us in, so we have a user_id and org_id in the context
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.Context) {
|
||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
||||
}).exec()
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ func Recovery() macaron.Handler {
|
||||
panicLogger := log.Root
|
||||
// try to get request logger
|
||||
if ctx, ok := c.Data["ctx"]; ok {
|
||||
ctxTyped := ctx.(*m.Context)
|
||||
ctxTyped := ctx.(*m.ReqContext)
|
||||
panicLogger = ctxTyped.Logger
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ func Recovery() macaron.Handler {
|
||||
c.Data["ErrorMsg"] = string(stack)
|
||||
}
|
||||
|
||||
ctx, ok := c.Data["ctx"].(*m.Context)
|
||||
ctx, ok := c.Data["ctx"].(*m.ReqContext)
|
||||
|
||||
if ok && ctx.IsApiRequest() {
|
||||
resp := make(map[string]interface{})
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestRecoveryMiddleware(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func PanicHandler(c *m.Context) {
|
||||
func PanicHandler(c *m.ReqContext) {
|
||||
panic("Handler has panicked")
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func recoveryScenario(desc string, url string, fn scenarioFunc) {
|
||||
sc.m.Use(OrgRedirect())
|
||||
sc.m.Use(AddDefaultResponseHeaders())
|
||||
|
||||
sc.defaultHandler = func(c *m.Context) {
|
||||
sc.defaultHandler = func(c *m.ReqContext) {
|
||||
sc.context = c
|
||||
if sc.handlerFunc != nil {
|
||||
sc.handlerFunc(sc.context)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
var renderKeysLock sync.Mutex
|
||||
var renderKeys map[string]*m.SignedInUser = make(map[string]*m.SignedInUser)
|
||||
|
||||
func initContextWithRenderAuth(ctx *m.Context) bool {
|
||||
func initContextWithRenderAuth(ctx *m.ReqContext) bool {
|
||||
key := ctx.GetCookie("renderKey")
|
||||
if key == "" {
|
||||
return false
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func Sessioner(options *ms.Options) macaron.Handler {
|
||||
session.Init(options)
|
||||
|
||||
return func(ctx *m.Context) {
|
||||
return func(ctx *m.ReqContext) {
|
||||
ctx.Next()
|
||||
|
||||
if err := ctx.Session.Release(); err != nil {
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func ValidateHostHeader(domain string) macaron.Handler {
|
||||
return func(c *m.Context) {
|
||||
return func(c *m.ReqContext) {
|
||||
// ignore local render calls
|
||||
if c.IsRenderCall {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user