| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | package models | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 14:45:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2019-01-18 00:11:52 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | type ReqContext struct { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	*web.Context | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	*SignedInUser | 
					
						
							| 
									
										
										
										
											2019-02-06 23:45:48 +08:00
										 |  |  | 	UserToken *UserToken | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	IsSignedIn     bool | 
					
						
							|  |  |  | 	IsRenderCall   bool | 
					
						
							|  |  |  | 	AllowAnonymous bool | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	SkipCache      bool | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	Logger         log.Logger | 
					
						
							| 
									
										
										
										
											2021-01-12 14:42:32 +08:00
										 |  |  | 	// RequestNonce is a cryptographic request identifier for use with Content Security Policy.
 | 
					
						
							|  |  |  | 	RequestNonce string | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PerfmonTimer   prometheus.Summary | 
					
						
							|  |  |  | 	LookupTokenErr error | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Handle handles and logs error by given status.
 | 
					
						
							| 
									
										
										
										
											2020-12-16 02:09:04 +08:00
										 |  |  | func (ctx *ReqContext) Handle(cfg *setting.Cfg, status int, title string, err error) { | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 	data := struct { | 
					
						
							|  |  |  | 		Title     string | 
					
						
							| 
									
										
										
										
											2021-10-13 14:35:01 +08:00
										 |  |  | 		AppTitle  string | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 		AppSubUrl string | 
					
						
							|  |  |  | 		Theme     string | 
					
						
							|  |  |  | 		ErrorMsg  error | 
					
						
							| 
									
										
										
										
											2021-10-13 14:35:01 +08:00
										 |  |  | 	}{title, "Grafana", cfg.AppSubURL, "dark", nil} | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		ctx.Logger.Error(title, "error", err) | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 		if setting.Env != setting.Prod { | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 			data.ErrorMsg = err | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 	ctx.HTML(status, cfg.ErrTemplateName, data) | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) IsApiRequest() bool { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	return strings.HasPrefix(ctx.Req.URL.Path, "/api") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) JsonApiErr(status int, message string, err error) { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	resp := make(map[string]interface{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		ctx.Logger.Error(message, "error", err) | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 		if setting.Env != setting.Prod { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08: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) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) HasUserRole(role RoleType) bool { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	return ctx.OrgRole.Includes(role) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) HasHelpFlag(flag HelpFlags1) bool { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	return ctx.HelpFlags1.HasFlag(flag) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) TimeRequest(timer prometheus.Summary) { | 
					
						
							| 
									
										
										
										
											2021-09-13 21:41:03 +08:00
										 |  |  | 	ctx.PerfmonTimer = timer | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-04-21 18:34:42 +08: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) | 
					
						
							|  |  |  | } |