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"
|
2018-03-06 16:59:45 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2019-01-17 10:11:52 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"gopkg.in/macaron.v1"
|
2018-03-06 16:59:45 -06:00
|
|
|
)
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
type ReqContext struct {
|
2018-03-06 16:59:45 -06:00
|
|
|
*macaron.Context
|
|
|
|
*SignedInUser
|
2019-02-06 09:45:48 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle handles and logs error by given status.
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) Handle(status int, title string, err error) {
|
2018-03-06 16:59:45 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Error(title, "error", err)
|
|
|
|
if setting.Env != setting.PROD {
|
|
|
|
ctx.Data["ErrorMsg"] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Title"] = title
|
|
|
|
ctx.Data["AppSubUrl"] = setting.AppSubUrl
|
|
|
|
ctx.Data["Theme"] = "dark"
|
|
|
|
|
2018-11-02 04:49:46 -05:00
|
|
|
ctx.HTML(status, setting.ERR_TEMPLATE_NAME)
|
2018-03-06 16:59:45 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) JsonOK(message string) {
|
2018-03-06 16:59:45 -06:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
resp["message"] = message
|
|
|
|
ctx.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
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{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Error(message, "error", err)
|
|
|
|
if setting.Env != setting.PROD {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) HasUserRole(role RoleType) bool {
|
2018-03-06 16:59:45 -06:00
|
|
|
return ctx.OrgRole.Includes(role)
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func (ctx *ReqContext) HasHelpFlag(flag 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) {
|
2018-03-06 16:59:45 -06:00
|
|
|
ctx.Data["perfmon.timer"] = timer
|
|
|
|
}
|