macaron transition progress

This commit is contained in:
Torkel Ödegaard
2014-10-05 21:13:07 +02:00
parent 201e1d3e6d
commit 222319d924
11 changed files with 403 additions and 38 deletions
+57
View File
@@ -0,0 +1,57 @@
package middleware
import (
"errors"
"strconv"
"github.com/Unknwon/macaron"
"github.com/macaron-contrib/session"
"github.com/torkelo/grafana-pro/pkg/models"
)
func authGetRequestAccountId(c *Context, sess session.Store) (int, error) {
accountId := sess.Get("accountId")
urlQuery := c.Req.URL.Query()
if len(urlQuery["render"]) > 0 {
accId, _ := strconv.Atoi(urlQuery["accountId"][0])
sess.Set("accountId", accId)
accountId = accId
}
if accountId == nil {
return -1, errors.New("Auth: session account id not found")
}
return accountId.(int), nil
}
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
}
}
+10 -4
View File
@@ -1,7 +1,8 @@
package middleware
import (
"time"
"encoding/json"
"io/ioutil"
"github.com/Unknwon/macaron"
"github.com/macaron-contrib/session"
@@ -21,13 +22,12 @@ type Context struct {
}
func GetContextHandler() macaron.Handler {
return func(c *macaron.Context) {
return func(c *macaron.Context, sess session.Store) {
ctx := &Context{
Context: c,
Session: sess,
}
ctx.Data["PageStartTime"] = time.Now()
c.Map(ctx)
}
}
@@ -50,3 +50,9 @@ func (ctx *Context) Handle(status int, title string, err error) {
ctx.HTML(status, "index")
}
func (ctx *Context) JsonBody(model interface{}) bool {
b, _ := ioutil.ReadAll(ctx.Req.Body)
err := json.Unmarshal(b, &model)
return err == nil
}