2018-03-06 16:59:45 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-04-14 10:54:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2022-11-18 02:56:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models/usertoken"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2018-03-06 16:59:45 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2019-01-17 10:11:52 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-03-06 16:59:45 -06:00
|
|
|
)
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
type ReqContext struct {
|
2021-10-11 07:30:59 -05:00
|
|
|
*web.Context
|
2022-08-10 04:56:48 -05:00
|
|
|
*user.SignedInUser
|
2022-11-18 02:56:06 -06:00
|
|
|
UserToken *usertoken.UserToken
|
2018-03-06 16:59:45 -06:00
|
|
|
|
|
|
|
IsSignedIn bool
|
|
|
|
IsRenderCall bool
|
|
|
|
AllowAnonymous bool
|
2018-10-26 03:40:33 -05:00
|
|
|
SkipCache bool
|
2018-03-06 16:59:45 -06:00
|
|
|
Logger log.Logger
|
2021-01-12 00:42:32 -06:00
|
|
|
// RequestNonce is a cryptographic request identifier for use with Content Security Policy.
|
2022-06-02 15:57:55 -05:00
|
|
|
RequestNonce string
|
|
|
|
IsPublicDashboardView bool
|
2021-09-13 08:41:03 -05:00
|
|
|
|
|
|
|
PerfmonTimer prometheus.Summary
|
|
|
|
LookupTokenErr error
|
2018-03-06 16:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle handles and logs error by given status.
|
2020-12-15 12:09:04 -06:00
|
|
|
func (ctx *ReqContext) Handle(cfg *setting.Cfg, status int, title string, err error) {
|
2021-09-13 08:41:03 -05:00
|
|
|
data := struct {
|
|
|
|
Title string
|
2021-10-13 01:35:01 -05:00
|
|
|
AppTitle string
|
2021-09-13 08:41:03 -05:00
|
|
|
AppSubUrl string
|
|
|
|
Theme string
|
|
|
|
ErrorMsg error
|
2021-10-13 01:35:01 -05:00
|
|
|
}{title, "Grafana", cfg.AppSubURL, "dark", nil}
|
2018-03-06 16:59:45 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Error(title, "error", err)
|
2020-10-02 08:45:45 -05:00
|
|
|
if setting.Env != setting.Prod {
|
2021-09-13 08:41:03 -05:00
|
|
|
data.ErrorMsg = err
|
2018-03-06 16:59:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 08:41:03 -05:00
|
|
|
ctx.HTML(status, cfg.ErrTemplateName, data)
|
2018-03-06 16:59:45 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) IsApiRequest() bool {
|
2018-03-06 16:59:45 -06:00
|
|
|
return strings.HasPrefix(ctx.Req.URL.Path, "/api")
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) JsonApiErr(status int, message string, err error) {
|
2018-03-06 16:59:45 -06:00
|
|
|
resp := make(map[string]interface{})
|
2022-04-14 10:54:49 -05:00
|
|
|
traceID := tracing.TraceIDFromContext(ctx.Req.Context(), false)
|
2018-03-06 16:59:45 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-14 10:54:49 -05:00
|
|
|
resp["traceID"] = traceID
|
|
|
|
ctx.Logger.Error(message, "error", err, "traceID", traceID)
|
2020-10-02 08:45:45 -05:00
|
|
|
if setting.Env != setting.Prod {
|
2018-03-06 16:59:45 -06:00
|
|
|
resp["error"] = err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case 404:
|
|
|
|
resp["message"] = "Not Found"
|
|
|
|
case 500:
|
|
|
|
resp["message"] = "Internal Server Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
if message != "" {
|
|
|
|
resp["message"] = message
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(status, resp)
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (ctx *ReqContext) HasUserRole(role org.RoleType) bool {
|
2018-03-06 16:59:45 -06:00
|
|
|
return ctx.OrgRole.Includes(role)
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (ctx *ReqContext) HasHelpFlag(flag user.HelpFlags1) bool {
|
2018-03-06 16:59:45 -06:00
|
|
|
return ctx.HelpFlags1.HasFlag(flag)
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) TimeRequest(timer prometheus.Summary) {
|
2021-09-13 08:41:03 -05:00
|
|
|
ctx.PerfmonTimer = timer
|
2018-03-06 16:59:45 -06:00
|
|
|
}
|
2021-04-21 05:34:42 -05:00
|
|
|
|
|
|
|
// QueryBoolWithDefault extracts a value from the request query params and applies a bool default if not present.
|
|
|
|
func (ctx *ReqContext) QueryBoolWithDefault(field string, d bool) bool {
|
|
|
|
f := ctx.Query(field)
|
|
|
|
if f == "" {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.QueryBool(field)
|
|
|
|
}
|