| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | package models | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/log" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/session" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2019-01-18 00:11:52 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							|  |  |  | 	"gopkg.in/macaron.v1" | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | type ReqContext struct { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	*macaron.Context | 
					
						
							|  |  |  | 	*SignedInUser | 
					
						
							| 
									
										
										
										
											2019-02-06 23:45:48 +08:00
										 |  |  | 	UserToken *UserToken | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 19:41:15 +08:00
										 |  |  | 	// This should only be used by the auth_proxy
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	Session session.SessionStore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Handle handles and logs error by given status.
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) Handle(status int, title string, err error) { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08: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 17:49:46 +08:00
										 |  |  | 	ctx.HTML(status, setting.ERR_TEMPLATE_NAME) | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func (ctx *ReqContext) JsonOK(message string) { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	resp := make(map[string]interface{}) | 
					
						
							|  |  |  | 	resp["message"] = message | 
					
						
							|  |  |  | 	ctx.JSON(200, resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 		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-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) { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	ctx.Data["perfmon.timer"] = timer | 
					
						
							|  |  |  | } |