2018-03-06 16:59:45 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/services/session"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
type ReqContext struct {
|
2018-03-06 16:59:45 -06:00
|
|
|
*macaron.Context
|
|
|
|
*SignedInUser
|
|
|
|
|
|
|
|
Session session.SessionStore
|
|
|
|
|
|
|
|
IsSignedIn bool
|
|
|
|
IsRenderCall bool
|
|
|
|
AllowAnonymous bool
|
|
|
|
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"
|
|
|
|
|
|
|
|
ctx.HTML(status, "error")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|