2014-10-05 14:13:01 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/Unknwon/macaron"
|
|
|
|
"github.com/macaron-contrib/session"
|
2014-11-20 08:19:44 -06:00
|
|
|
|
2014-10-05 14:13:01 -05:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/models"
|
|
|
|
)
|
|
|
|
|
2014-11-20 05:11:07 -06:00
|
|
|
func authGetRequestAccountId(c *Context, sess session.Store) (int64, error) {
|
2014-10-05 14:13:01 -05:00
|
|
|
accountId := sess.Get("accountId")
|
|
|
|
|
|
|
|
urlQuery := c.Req.URL.Query()
|
|
|
|
if len(urlQuery["render"]) > 0 {
|
2014-12-01 15:25:57 -06:00
|
|
|
accId, _ := strconv.ParseInt(urlQuery["accountId"][0], 10, 64)
|
2014-10-05 14:13:01 -05:00
|
|
|
sess.Set("accountId", accId)
|
|
|
|
accountId = accId
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountId == nil {
|
|
|
|
return -1, errors.New("Auth: session account id not found")
|
|
|
|
}
|
|
|
|
|
2014-11-20 05:11:07 -06:00
|
|
|
return accountId.(int64), nil
|
2014-10-05 14:13:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func authDenied(c *Context) {
|
|
|
|
c.Redirect("/login")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Auth() macaron.Handler {
|
|
|
|
return func(c *Context, sess session.Store) {
|
|
|
|
accountId, err := authGetRequestAccountId(c, sess)
|
|
|
|
|
|
|
|
if err != nil && c.Req.URL.Path != "/login" {
|
|
|
|
authDenied(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := models.GetAccount(accountId)
|
|
|
|
if err != nil {
|
|
|
|
authDenied(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usingAccount, err := models.GetAccount(account.UsingAccountId)
|
|
|
|
if err != nil {
|
|
|
|
authDenied(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UserAccount = account
|
|
|
|
c.Account = usingAccount
|
|
|
|
}
|
|
|
|
}
|