diff --git a/grafana b/grafana index 4e542d8b838..ad91093902b 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit 4e542d8b83844f8faa4d5ae3edab593950aaa344 +Subproject commit ad91093902bdfc0d2a87bb362a76a9057aef4361 diff --git a/pkg/api/api_account.go b/pkg/api/api_account.go index 2ef03d5ee4f..444c07cb593 100644 --- a/pkg/api/api_account.go +++ b/pkg/api/api_account.go @@ -76,36 +76,45 @@ func GetOtherAccounts(c *middleware.Context) { c.JSON(200, result) } -func SetUsingAccount(c *middleware.Context) { - // usingAccountId := c.ParamsInt64(":id") - // - // account := c.UserAccount - // otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id) - // - // if err != nil { - // c.JSON(500, utils.DynMap{"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 = m.SaveAccount(account) - // if err != nil { - // c.JSON(500, utils.DynMap{"message": err.Error()}) - // return - // } - // - // c.Status(204) +func validateUsingAccount(accountId int64, otherId int64) bool { + if accountId == otherId { + return true + } + + query := m.GetOtherAccountsQuery{AccountId: accountId} + err := bus.Dispatch(&query) + if err != nil { + return false + } + + // validate that the account id in the list + valid := false + for _, other := range query.Result { + if other.Id == otherId { + valid = true + } + } + return valid +} + +func SetUsingAccount(c *middleware.Context) { + usingAccountId := c.ParamsInt64(":id") + + if !validateUsingAccount(c.UserAccount.Id, usingAccountId) { + c.JsonApiErr(401, "Not a valid account", nil) + return + } + + cmd := m.SetUsingAccountCommand{ + AccountId: c.UserAccount.Id, + UsingAccountId: usingAccountId, + } + + err := bus.Dispatch(&cmd) + if err != nil { + c.JsonApiErr(500, "Failed to update account", err) + return + } + + c.JsonOK("Active account changed") } diff --git a/pkg/models/account.go b/pkg/models/account.go index ca376f9b8f6..c48406fb474 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -64,6 +64,11 @@ type CreateAccountCommand struct { Result Account `json:"-"` } +type SetUsingAccountCommand struct { + AccountId int64 + UsingAccountId int64 +} + // returns a view projection type GetAccountInfoQuery struct { Id int64 diff --git a/pkg/stores/sqlstore/accounts.go b/pkg/stores/sqlstore/accounts.go index edb294845cb..5331528162a 100644 --- a/pkg/stores/sqlstore/accounts.go +++ b/pkg/stores/sqlstore/accounts.go @@ -14,6 +14,7 @@ func init() { bus.AddHandler("sql", AddCollaborator) bus.AddHandler("sql", GetOtherAccounts) bus.AddHandler("sql", CreateAccount) + bus.AddHandler("sql", SetUsingAccount) } func CreateAccount(cmd *m.CreateAccountCommand) error { @@ -33,6 +34,17 @@ func CreateAccount(cmd *m.CreateAccountCommand) error { }) } +func SetUsingAccount(cmd *m.SetUsingAccountCommand) error { + return inTransaction(func(sess *xorm.Session) error { + account := m.Account{} + sess.Id(cmd.AccountId).Get(&account) + + account.UsingAccountId = cmd.UsingAccountId + _, err := sess.Id(account.Id).Update(&account) + return err + }) +} + func GetAccountInfo(query *m.GetAccountInfoQuery) error { var account m.Account has, err := x.Id(query.Id).Get(&account) diff --git a/pkg/stores/sqlstore/accounts_test.go b/pkg/stores/sqlstore/accounts_test.go index cd0b952643d..1b1f76ad2f6 100644 --- a/pkg/stores/sqlstore/accounts_test.go +++ b/pkg/stores/sqlstore/accounts_test.go @@ -61,6 +61,12 @@ func TestAccountDataAccess(t *testing.T) { So(err, ShouldBeNil) So(query.Result[0].Email, ShouldEqual, "ac1@test.com") }) + + Convey("Can set using account", func() { + cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id} + err := SetUsingAccount(&cmd) + So(err, ShouldBeNil) + }) }) }) })