From 158b708eacc01e2bdac1b32c08084a97a963ac5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 18 Sep 2014 12:03:46 +0200 Subject: [PATCH] Small progress on adding collaborator --- grafana | 2 +- pkg/api/api.go | 2 ++ pkg/api/api_account.go | 39 ++++++++++++++++++++++++++++++++ pkg/api/api_login.go | 13 +---------- pkg/models/dashboards.go | 17 ++++++++++---- pkg/stores/rethinkdb_accounts.go | 18 ++++++++++++++- pkg/stores/rethinkdb_test.go | 6 ++--- pkg/stores/store.go | 1 + 8 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 pkg/api/api_account.go diff --git a/grafana b/grafana index 4f798cfe568..aa47eeffb2d 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit 4f798cfe568db2491fe5eea3f06ddd3027117e90 +Subproject commit aa47eeffb2da4cdc8be8f5b1bb5233eeabcd5a03 diff --git a/pkg/api/api.go b/pkg/api/api.go index 50c07f9bcd5..65ee23b88a4 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) } diff --git a/pkg/api/api_account.go b/pkg/api/api_account.go new file mode 100644 index 00000000000..22f2b85b70e --- /dev/null +++ b/pkg/api/api_account.go @@ -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"}) +} diff --git a/pkg/api/api_login.go b/pkg/api/api_login.go index 67b47552812..1651535e53e 100644 --- a/pkg/api/api_login.go +++ b/pkg/api/api_login.go @@ -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") diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 53bd41d6bf8..27066076a76 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -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(), + }) +} diff --git a/pkg/stores/rethinkdb_accounts.go b/pkg/stores/rethinkdb_accounts.go index 9d574fc3bda..0492f5c729c 100644 --- a/pkg/stores/rethinkdb_accounts.go +++ b/pkg/stores/rethinkdb_accounts.go @@ -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), diff --git a/pkg/stores/rethinkdb_test.go b/pkg/stores/rethinkdb_test.go index e90abb785e8..db092ae8080 100644 --- a/pkg/stores/rethinkdb_test.go +++ b/pkg/stores/rethinkdb_test.go @@ -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) }) diff --git a/pkg/stores/store.go b/pkg/stores/store.go index 8b7fec3eb5b..0bd4de8516f 100644 --- a/pkg/stores/store.go +++ b/pkg/stores/store.go @@ -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() }