working on account registration and more

This commit is contained in:
Torkel Ödegaard
2014-08-22 18:05:37 +02:00
parent c684b1ddab
commit 31fe471da5
9 changed files with 137 additions and 30 deletions
+2 -2
View File
@@ -51,8 +51,8 @@ func (self *HttpServer) ListenAndServe() {
}
// register default route
self.router.GET("/", self.authMiddleware(), self.index)
self.router.GET("/dashboard/*_", self.authMiddleware(), self.index)
self.router.GET("/", self.auth(), self.index)
self.router.GET("/dashboard/*_", self.auth(), self.index)
self.router.Run(":" + self.port)
}
+5 -4
View File
@@ -8,16 +8,17 @@ import (
func init() {
addRoutes(func(self *HttpServer) {
self.router.GET("/api/dashboards/:id", self.getDashboard)
self.router.GET("/api/search/", self.search)
self.router.POST("/api/dashboard", self.postDashboard)
self.router.GET("/api/dashboards/:id", self.auth(), self.getDashboard)
self.router.GET("/api/search/", self.auth(), self.search)
self.router.POST("/api/dashboard", self.auth(), self.postDashboard)
})
}
func (self *HttpServer) getDashboard(c *gin.Context) {
id := c.Params.ByName("id")
accountId, err := c.Get("accountId")
dash, err := self.store.GetDashboard(id, 1)
dash, err := self.store.GetDashboard(id, accountId.(int))
if err != nil {
c.JSON(404, newErrorResponse("Dashboard not found"))
return
+25 -13
View File
@@ -5,7 +5,6 @@ import "github.com/gin-gonic/gin"
func init() {
addRoutes(func(self *HttpServer) {
self.router.GET("/login/*_", self.index)
self.router.GET("/register/*_", self.index)
self.router.POST("/login", self.loginPost)
self.router.POST("/logout", self.logoutPost)
})
@@ -20,18 +19,28 @@ type loginJsonModel struct {
func (self *HttpServer) loginPost(c *gin.Context) {
var loginModel loginJsonModel
if c.EnsureBody(&loginModel) {
if loginModel.Email == "manu" && loginModel.Password == "123" {
session, _ := sessionStore.Get(c.Request, "grafana-session")
session.Values["login"] = true
session.Save(c.Request, c.Writer)
c.JSON(200, gin.H{"status": "you are logged in"})
} else {
c.JSON(401, gin.H{"status": "unauthorized"})
}
if !c.EnsureBody(&loginModel) {
c.JSON(400, gin.H{"status": "bad request"})
return
}
account, err := self.store.GetUserAccountLogin(loginModel.Email)
if err != nil {
c.JSON(400, gin.H{"status": "some error"})
}
if loginModel.Password != account.Password {
c.JSON(401, gin.H{"status": "unauthorized"})
return
}
session, _ := sessionStore.Get(c.Request, "grafana-session")
session.Values["login"] = true
session.Values["accountId"] = account.DatabaseId
session.Save(c.Request, c.Writer)
c.JSON(200, gin.H{"status": "you are logged in"})
}
func (self *HttpServer) logoutPost(c *gin.Context) {
@@ -42,15 +51,18 @@ func (self *HttpServer) logoutPost(c *gin.Context) {
c.JSON(200, gin.H{"status": "logged out"})
}
func (self *HttpServer) authMiddleware() gin.HandlerFunc {
func (self *HttpServer) auth() gin.HandlerFunc {
return func(c *gin.Context) {
session, _ := sessionStore.Get(c.Request, "grafana-session")
if c.Request.URL.Path != "/login" && session.Values["login"] == nil {
c.Writer.Header().Set("Location", "/login")
c.Abort(302)
return
}
c.Set("accountId", session.Values["accountId"])
session.Save(c.Request, c.Writer)
}
}
+45
View File
@@ -0,0 +1,45 @@
package api
import (
log "github.com/alecthomas/log4go"
"github.com/gin-gonic/gin"
"github.com/torkelo/grafana-pro/pkg/models"
)
func init() {
addRoutes(func(self *HttpServer) {
self.router.GET("/register/*_", self.index)
self.router.POST("/api/register/user", self.registerUserPost)
})
}
type registerAccountJsonModel struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
Password2 bool `json:"remember2"`
}
func (self *HttpServer) registerUserPost(c *gin.Context) {
var registerModel registerAccountJsonModel
if !c.EnsureBody(&registerModel) {
c.JSON(400, gin.H{"status": "bad request"})
return
}
account := models.UserAccount{
UserName: registerModel.Email,
Login: registerModel.Email,
Email: registerModel.Email,
Password: registerModel.Password,
}
err := self.store.SaveUserAccount(&account)
if err != nil {
log.Error("Failed to create user account, email: %v, error: %v", registerModel.Email, err)
c.JSON(500, gin.H{"status": "failed to create account"})
return
}
c.JSON(200, gin.H{"status": "ok"})
}