Commit 158b708e by Torkel Ödegaard

Small progress on adding collaborator

parent 4dfe8b6f
Subproject commit 4f798cfe568db2491fe5eea3f06ddd3027117e90
Subproject commit aa47eeffb2da4cdc8be8f5b1bb5233eeabcd5a03
......@@ -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)
}
......
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"})
}
......@@ -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")
......
......@@ -21,22 +21,22 @@ type Dashboard struct {
Data map[string]interface{}
}
type UserAccountLink struct {
UserId int
type CollaboratorLink struct {
AccountId int
Role string
ModifiedOn time.Time
CreatedOn time.Time
}
type UserAccount struct {
DatabaseId int `gorethink:"id"`
Id int `gorethink:"id"`
UserName string
Login string
Email string
Password string
NextDashboardId int
UsingAccountId int
GrantedAccess []UserAccountLink
Collaborators []CollaboratorLink
CreatedOn time.Time
ModifiedOn time.Time
}
......@@ -87,3 +87,12 @@ func (dash *Dashboard) UpdateSlug() {
re2 := regexp.MustCompile("\\s")
dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
}
func (account *UserAccount) AddCollaborator(accountId int) {
account.Collaborators = append(account.Collaborators, CollaboratorLink{
AccountId: accountId,
Role: "admin",
CreatedOn: time.Now(),
ModifiedOn: time.Now(),
})
}
......@@ -31,7 +31,7 @@ func (self *rethinkStore) SaveUserAccount(account *models.UserAccount) error {
return err
}
account.DatabaseId = accountId
account.Id = accountId
resp, err := r.Table("accounts").Insert(account).RunWrite(self.session)
if err != nil {
......@@ -61,6 +61,22 @@ func (self *rethinkStore) GetUserAccountLogin(emailOrName string) (*models.UserA
return &account, nil
}
func (self *rethinkStore) GetAccount(id int) (*models.UserAccount, error) {
resp, err := r.Table("accounts").Get(id).Run(self.session)
if err != nil {
return nil, err
}
var account models.UserAccount
err = resp.One(&account)
if err != nil {
return nil, errors.New("Not found")
}
return &account, nil
}
func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
resp, err := r.Table("accounts").Get(accountId).Update(map[string]interface{}{
"NextDashboardId": r.Row.Field("NextDashboardId").Add(1),
......
......@@ -38,17 +38,17 @@ func TestRethinkStore(t *testing.T) {
account := &models.UserAccount{UserName: "torkelo", Email: "mupp", Login: "test@test.com"}
err := store.SaveUserAccount(account)
So(err, ShouldBeNil)
So(account.DatabaseId, ShouldNotEqual, 0)
So(account.Id, ShouldNotEqual, 0)
read, err := store.GetUserAccountLogin("test@test.com")
So(err, ShouldBeNil)
So(read.DatabaseId, ShouldEqual, account.DatabaseId)
So(read.Id, ShouldEqual, account.DatabaseId)
})
Convey("can get next dashboard id", t, func() {
account := &models.UserAccount{UserName: "torkelo", Email: "mupp"}
err := store.SaveUserAccount(account)
dashId, err := store.getNextDashboardNumber(account.DatabaseId)
dashId, err := store.getNextDashboardNumber(account.Id)
So(err, ShouldBeNil)
So(dashId, ShouldEqual, 1)
})
......
......@@ -11,6 +11,7 @@ type Store interface {
Query(query string, acccountId int) ([]*models.SearchResult, error)
SaveUserAccount(acccount *models.UserAccount) error
GetUserAccountLogin(emailOrName string) (*models.UserAccount, error)
GetAccount(id int) (*models.UserAccount, error)
Close()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment