2014-10-05 22:50:04 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2014-10-06 03:13:01 +08:00
|
|
|
"encoding/json"
|
2014-10-08 05:56:37 +08:00
|
|
|
"strconv"
|
2015-01-14 21:25:12 +08:00
|
|
|
"strings"
|
2014-10-05 22:50:04 +08:00
|
|
|
|
|
|
|
"github.com/Unknwon/macaron"
|
|
|
|
"github.com/macaron-contrib/session"
|
|
|
|
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-10-05 22:50:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
*macaron.Context
|
2015-01-16 23:17:35 +08:00
|
|
|
*m.SignedInUser
|
2015-01-16 21:32:18 +08:00
|
|
|
|
2014-10-05 22:50:04 +08:00
|
|
|
Session session.Store
|
|
|
|
|
2015-01-27 22:45:27 +08:00
|
|
|
IsSignedIn bool
|
|
|
|
HasAnonymousAccess bool
|
2014-10-07 03:31:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-05 22:50:04 +08:00
|
|
|
func GetContextHandler() macaron.Handler {
|
2014-10-06 03:13:01 +08:00
|
|
|
return func(c *macaron.Context, sess session.Store) {
|
2014-10-05 22:50:04 +08:00
|
|
|
ctx := &Context{
|
2015-01-27 22:45:27 +08:00
|
|
|
Context: c,
|
|
|
|
Session: sess,
|
|
|
|
SignedInUser: &m.SignedInUser{},
|
|
|
|
IsSignedIn: false,
|
|
|
|
HasAnonymousAccess: false,
|
2014-10-05 22:50:04 +08:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:16:54 +08:00
|
|
|
// try get account id from request
|
2015-01-20 01:01:04 +08:00
|
|
|
if userId := getRequestUserId(ctx); userId != 0 {
|
|
|
|
query := m.GetSignedInUserQuery{UserId: userId}
|
2015-01-16 21:32:18 +08:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2015-01-20 01:01:04 +08:00
|
|
|
log.Error(3, "Failed to get user by id, %v, %v", userId, err)
|
2015-01-15 19:16:54 +08:00
|
|
|
} else {
|
2015-01-16 23:17:35 +08:00
|
|
|
ctx.SignedInUser = query.Result
|
2015-01-27 22:45:27 +08:00
|
|
|
ctx.IsSignedIn = true
|
2015-01-15 19:16:54 +08:00
|
|
|
}
|
2015-01-27 15:26:11 +08:00
|
|
|
} else if key := getApiKey(ctx); key != "" {
|
2015-01-16 23:15:35 +08:00
|
|
|
// Try API Key auth
|
2015-01-27 15:26:11 +08:00
|
|
|
keyQuery := m.GetApiKeyByKeyQuery{Key: key}
|
|
|
|
if err := bus.Dispatch(&keyQuery); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
2015-01-16 23:15:35 +08:00
|
|
|
return
|
|
|
|
} else {
|
2015-01-27 15:26:11 +08:00
|
|
|
keyInfo := keyQuery.Result
|
2015-01-16 23:15:35 +08:00
|
|
|
|
|
|
|
ctx.IsSignedIn = true
|
2015-01-20 01:01:04 +08:00
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
2015-01-16 23:15:35 +08:00
|
|
|
|
2015-01-20 01:01:04 +08:00
|
|
|
// TODO: fix this
|
2015-01-27 15:26:11 +08:00
|
|
|
ctx.AccountRole = keyInfo.Role
|
|
|
|
ctx.ApiKeyId = keyInfo.Id
|
|
|
|
ctx.AccountId = keyInfo.AccountId
|
2015-01-16 23:15:35 +08:00
|
|
|
}
|
2015-01-27 22:45:27 +08:00
|
|
|
} else if setting.AnonymousEnabled {
|
|
|
|
accountQuery := m.GetAccountByNameQuery{Name: setting.AnonymousAccountName}
|
|
|
|
if err := bus.Dispatch(&accountQuery); err != nil {
|
|
|
|
if err == m.ErrAccountNotFound {
|
|
|
|
log.Error(3, "Anonymous access account name does not exist", nil)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.IsSignedIn = false
|
|
|
|
ctx.HasAnonymousAccess = true
|
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
|
|
|
ctx.AccountRole = m.RoleType(setting.AnonymousAccountRole)
|
|
|
|
ctx.AccountId = accountQuery.Result.Id
|
|
|
|
}
|
2015-01-15 19:16:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-05 22:50:04 +08:00
|
|
|
c.Map(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle handles and logs error by given status.
|
|
|
|
func (ctx *Context) Handle(status int, title string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "%s: %v", title, err)
|
2015-01-29 22:46:54 +08:00
|
|
|
if setting.Env != setting.PROD {
|
2014-10-05 22:50:04 +08:00
|
|
|
ctx.Data["ErrorMsg"] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case 404:
|
|
|
|
ctx.Data["Title"] = "Page Not Found"
|
|
|
|
case 500:
|
|
|
|
ctx.Data["Title"] = "Internal Server Error"
|
|
|
|
}
|
|
|
|
|
2014-10-08 05:56:37 +08:00
|
|
|
ctx.HTML(status, strconv.Itoa(status))
|
2014-10-05 22:50:04 +08:00
|
|
|
}
|
2014-10-06 03:13:01 +08:00
|
|
|
|
2014-12-17 10:09:54 +08:00
|
|
|
func (ctx *Context) JsonOK(message string) {
|
|
|
|
resp := make(map[string]interface{})
|
|
|
|
|
|
|
|
resp["message"] = message
|
|
|
|
|
|
|
|
ctx.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
func (ctx *Context) IsApiRequest() bool {
|
|
|
|
return strings.HasPrefix(ctx.Req.URL.Path, "/api")
|
|
|
|
}
|
|
|
|
|
2014-10-08 03:54:38 +08:00
|
|
|
func (ctx *Context) JsonApiErr(status int, message string, err error) {
|
2014-10-07 03:31:54 +08:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "%s: %v", message, err)
|
2014-12-16 19:04:08 +08:00
|
|
|
if setting.Env != setting.PROD {
|
|
|
|
resp["error"] = err.Error()
|
2014-10-07 03:31:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case 404:
|
|
|
|
resp["message"] = "Not Found"
|
|
|
|
case 500:
|
|
|
|
resp["message"] = "Internal Server Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
if message != "" {
|
|
|
|
resp["message"] = message
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:17:13 +08:00
|
|
|
ctx.JSON(status, resp)
|
2014-10-07 03:31:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-06 03:13:01 +08:00
|
|
|
func (ctx *Context) JsonBody(model interface{}) bool {
|
2014-11-20 16:16:28 +08:00
|
|
|
b, _ := ctx.Req.Body().Bytes()
|
2014-10-06 03:13:01 +08:00
|
|
|
err := json.Unmarshal(b, &model)
|
|
|
|
return err == nil
|
|
|
|
}
|