mirror of
https://github.com/grafana/grafana.git
synced 2024-12-27 09:21:35 -06:00
Refactoring set using account
This commit is contained in:
parent
607b0c0c0e
commit
5dcf6ff2d3
2
grafana
2
grafana
@ -1 +1 @@
|
||||
Subproject commit 4e542d8b83844f8faa4d5ae3edab593950aaa344
|
||||
Subproject commit ad91093902bdfc0d2a87bb362a76a9057aef4361
|
@ -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")
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user