Small progress on adding collaborator

This commit is contained in:
Torkel Ödegaard
2014-09-18 12:03:46 +02:00
parent 4dfe8b6f69
commit 158b708eac
8 changed files with 77 additions and 21 deletions
+2
View File
@@ -53,6 +53,8 @@ func (self *HttpServer) ListenAndServe() {
// register default route
self.router.GET("/", self.auth(), self.index)
self.router.GET("/dashboard/*_", self.auth(), self.index)
self.router.GET("/admin/*_", self.auth(), self.index)
self.router.GET("/account/*_", self.auth(), self.index)
self.router.Run(":" + self.port)
}
+39
View File
@@ -0,0 +1,39 @@
package api
import "github.com/gin-gonic/gin"
func init() {
addRoutes(func(self *HttpServer) {
self.router.POST("/api/account/collaborators/add", self.auth(), self.addCollaborator)
})
}
type addCollaboratorDto struct {
Email string `json:"email" binding:"required"`
}
func (self *HttpServer) addCollaborator(c *gin.Context) {
var model addCollaboratorDto
if !c.EnsureBody(&model) {
c.JSON(400, gin.H{"status": "bad request"})
return
}
accountId, _ := c.Get("accountId")
account, err := self.store.GetAccount(accountId.(int))
if err != nil {
c.JSON(401, gin.H{"status": "Authentication error"})
}
collaborator, err := self.store.GetUserAccountLogin(model.Email)
if err != nil {
c.JSON(404, gin.H{"status": "Collaborator not found"})
}
account.AddCollaborator(collaborator.Id)
self.store.SaveUserAccount(account)
c.JSON(200, gin.H{"status": "Collaborator added"})
}
+1 -12
View File
@@ -36,7 +36,7 @@ func (self *HttpServer) loginPost(c *gin.Context) {
session, _ := sessionStore.Get(c.Request, "grafana-session")
session.Values["login"] = loginModel.Email
session.Values["accountId"] = account.DatabaseId
session.Values["accountId"] = account.Id
session.Save(c.Request, c.Writer)
var resp = &LoginResultDto{}
@@ -54,17 +54,6 @@ func (self *HttpServer) logoutPost(c *gin.Context) {
c.JSON(200, gin.H{"status": "logged out"})
}
type GrafanaReqContext struct {
}
type authenticatedAuthRouteFunc func(c *gin.Context, grc GrafanaReqContext)
func (self *HttpServer) addAuthRoute(route string, handler authenticatedAuthRouteFunc) {
self.router.GET(route, self.auth(), func(c *gin.Context) {
})
}
func (self *HttpServer) auth() gin.HandlerFunc {
return func(c *gin.Context) {
session, _ := sessionStore.Get(c.Request, "grafana-session")