api_account.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package api
  2. import (
  3. "github.com/torkelo/grafana-pro/pkg/bus"
  4. "github.com/torkelo/grafana-pro/pkg/middleware"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. "github.com/torkelo/grafana-pro/pkg/utils"
  7. )
  8. func GetAccount(c *middleware.Context) {
  9. query := m.GetAccountInfoQuery{Id: c.UserAccount.Id}
  10. err := bus.Dispatch(&query)
  11. if err != nil {
  12. c.JsonApiErr(500, "Failed to fetch collaboratos", err)
  13. return
  14. }
  15. c.JSON(200, query.Result)
  16. }
  17. func AddCollaborator(c *middleware.Context) {
  18. var cmd m.AddCollaboratorCommand
  19. if !c.JsonBody(&cmd) {
  20. c.JsonApiErr(400, "Invalid request", nil)
  21. return
  22. }
  23. accountToAdd, err := m.GetAccountByLogin(cmd.Email)
  24. if err != nil {
  25. c.JsonApiErr(404, "Collaborator not found", nil)
  26. return
  27. }
  28. if accountToAdd.Id == c.UserAccount.Id {
  29. c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
  30. return
  31. }
  32. cmd.AccountId = accountToAdd.Id
  33. cmd.ForAccountId = c.UserAccount.Id
  34. cmd.Role = m.ROLE_READ_WRITE
  35. err = bus.Dispatch(&cmd)
  36. if err != nil {
  37. c.JsonApiErr(500, "Could not add collaborator", err)
  38. return
  39. }
  40. c.JsonOK("Collaborator added")
  41. }
  42. func GetOtherAccounts(c *middleware.Context) {
  43. query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
  44. err := bus.Dispatch(&query)
  45. if err != nil {
  46. c.JSON(500, utils.DynMap{"message": err.Error()})
  47. return
  48. }
  49. result := append(query.Result, &m.OtherAccountDTO{
  50. Id: c.UserAccount.Id,
  51. Role: "owner",
  52. Email: c.UserAccount.Email,
  53. })
  54. for _, ac := range result {
  55. if ac.Id == c.UserAccount.UsingAccountId {
  56. ac.IsUsing = true
  57. break
  58. }
  59. }
  60. c.JSON(200, result)
  61. }
  62. func validateUsingAccount(accountId int64, otherId int64) bool {
  63. if accountId == otherId {
  64. return true
  65. }
  66. query := m.GetOtherAccountsQuery{AccountId: accountId}
  67. err := bus.Dispatch(&query)
  68. if err != nil {
  69. return false
  70. }
  71. // validate that the account id in the list
  72. valid := false
  73. for _, other := range query.Result {
  74. if other.Id == otherId {
  75. valid = true
  76. }
  77. }
  78. return valid
  79. }
  80. func SetUsingAccount(c *middleware.Context) {
  81. usingAccountId := c.ParamsInt64(":id")
  82. if !validateUsingAccount(c.UserAccount.Id, usingAccountId) {
  83. c.JsonApiErr(401, "Not a valid account", nil)
  84. return
  85. }
  86. cmd := m.SetUsingAccountCommand{
  87. AccountId: c.UserAccount.Id,
  88. UsingAccountId: usingAccountId,
  89. }
  90. err := bus.Dispatch(&cmd)
  91. if err != nil {
  92. c.JsonApiErr(500, "Failed to update account", err)
  93. return
  94. }
  95. c.JsonOK("Active account changed")
  96. }