diff --git a/pkg/api/api.go b/pkg/api/api.go index 75d8ddc36ac..8f2a5d59787 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -22,6 +22,7 @@ func Register(m *macaron.Macaron) { m.Get("/account/", auth, Index) m.Get("/api/account/", auth, GetAccount) m.Post("/api/account/collaborators/add", auth, AddCollaborator) + m.Post("/api/account/using/:id", auth, SetUsingAccount) m.Get("/api/account/others", auth, GetOtherAccounts) // user register diff --git a/pkg/api/api_account.go b/pkg/api/api_account.go index 30e12f1cb6c..0001cd4b9a1 100644 --- a/pkg/api/api_account.go +++ b/pkg/api/api_account.go @@ -1,6 +1,7 @@ package api import ( + "github.com/gin-gonic/gin" "github.com/torkelo/grafana-pro/pkg/api/dtos" "github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/models" @@ -88,28 +89,36 @@ func GetOtherAccounts(c *middleware.Context) { c.JSON(200, result) } -// func SetUsingAccount(c *middleware.Context) { -// idString := c.Params.ByName("id") -// id, _ := strconv.Atoi(idString) -// -// account := auth.userAccount -// otherAccount, err := self.store.GetAccount(id) -// if err != nil { -// c.JSON(500, gin.H{"message": err.Error()}) -// return -// } -// -// if otherAccount.Id != account.Id && !otherAccount.HasCollaborator(account.Id) { -// c.Abort(401) -// return -// } -// -// account.UsingAccountId = otherAccount.Id -// err = self.store.UpdateAccount(account) -// if err != nil { -// c.JSON(500, gin.H{"message": err.Error()}) -// return -// } -// -// c.Abort(204) -// } +func SetUsingAccount(c *middleware.Context) { + usingAccountId := c.ParamsInt64(":id") + + account := c.UserAccount + otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id) + + if err != nil { + c.JSON(500, gin.H{"message": err.Error()}) + return + } + + // validate that the account id in the list + valid := false + for _, other := range otherAccounts { + if other.Id == usingAccountId { + valid = true + } + } + + if !valid { + c.Status(401) + return + } + + account.UsingAccountId = usingAccountId + err = models.SaveAccount(account) + if err != nil { + c.JSON(500, gin.H{"message": err.Error()}) + return + } + + c.Status(204) +} diff --git a/pkg/api/api_login_oauth.go b/pkg/api/api_login_oauth.go index eabfac81787..459b3ec9bb2 100644 --- a/pkg/api/api_login_oauth.go +++ b/pkg/api/api_login_oauth.go @@ -59,7 +59,7 @@ func OAuthLogin(ctx *middleware.Context) { Company: userInfo.Company, } - if err = models.CreateAccount(account); err != nil { + if err = models.SaveAccount(account); err != nil { ctx.Handle(500, "Failed to create account", err) return } diff --git a/pkg/api/api_register.go b/pkg/api/api_register.go index fa3f941d814..9733f37bab9 100644 --- a/pkg/api/api_register.go +++ b/pkg/api/api_register.go @@ -27,7 +27,7 @@ func CreateAccount(c *middleware.Context) { Password: registerModel.Password, } - err := models.CreateAccount(&account) + err := models.SaveAccount(&account) if err != nil { log.Error(2, "Failed to create user account, email: %v, error: %v", registerModel.Email, err) c.JSON(500, utils.DynMap{"status": "failed to create account"}) diff --git a/pkg/models/account.go b/pkg/models/account.go index 6c8d87d422c..75a743f587d 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -6,8 +6,7 @@ import ( ) var ( - CreateAccount func(acccount *Account) error - UpdateAccount func(acccount *Account) error + SaveAccount func(account *Account) error GetAccountByLogin func(emailOrName string) (*Account, error) GetAccount func(accountId int64) (*Account, error) GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error) diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index 0e39aa5fb80..d3483a4589d 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -29,7 +29,7 @@ var ( func Init() { tables = append(tables, new(models.Account), new(models.Dashboard), new(models.Collaborator)) - models.CreateAccount = CreateAccount + models.SaveAccount = SaveAccount models.GetAccount = GetAccount models.GetAccountByLogin = GetAccountByLogin models.GetOtherAccountsFor = GetOtherAccountsFor diff --git a/pkg/stores/sqlstore/sqlstore_accounts.go b/pkg/stores/sqlstore/sqlstore_accounts.go index 27f2b1d720e..572fe67105b 100644 --- a/pkg/stores/sqlstore/sqlstore_accounts.go +++ b/pkg/stores/sqlstore/sqlstore_accounts.go @@ -1,10 +1,8 @@ package sqlstore -import ( - "github.com/torkelo/grafana-pro/pkg/models" -) +import "github.com/torkelo/grafana-pro/pkg/models" -func CreateAccount(account *models.Account) error { +func SaveAccount(account *models.Account) error { var err error sess := x.NewSession() @@ -14,7 +12,13 @@ func CreateAccount(account *models.Account) error { return err } - if _, err = sess.Insert(account); err != nil { + if account.Id == 0 { + _, err = sess.Insert(account) + } else { + _, err = sess.Id(account.Id).Update(account) + } + + if err != nil { sess.Rollback() return err } else if err = sess.Commit(); err != nil { diff --git a/pkg/stores/sqlstore/sqlstore_dashboards.go b/pkg/stores/sqlstore/sqlstore_dashboards.go index cfb4419cbef..9beaaef3a66 100644 --- a/pkg/stores/sqlstore/sqlstore_dashboards.go +++ b/pkg/stores/sqlstore/sqlstore_dashboards.go @@ -15,7 +15,7 @@ func SaveDashboard(dash *models.Dashboard) error { if dash.Id == 0 { _, err = sess.Insert(dash) } else { - _, err = sess.Update(dash) + _, err = sess.Id(dash.Id).Update(dash) } if err != nil {