api_account.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 SetUsingAccount(c *middleware.Context) {
  63. // usingAccountId := c.ParamsInt64(":id")
  64. //
  65. // account := c.UserAccount
  66. // otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
  67. //
  68. // if err != nil {
  69. // c.JSON(500, utils.DynMap{"message": err.Error()})
  70. // return
  71. // }
  72. //
  73. // // validate that the account id in the list
  74. // valid := false
  75. // for _, other := range otherAccounts {
  76. // if other.Id == usingAccountId {
  77. // valid = true
  78. // }
  79. // }
  80. //
  81. // if !valid {
  82. // c.Status(401)
  83. // return
  84. // }
  85. //
  86. // account.UsingAccountId = usingAccountId
  87. // err = m.SaveAccount(account)
  88. // if err != nil {
  89. // c.JSON(500, utils.DynMap{"message": err.Error()})
  90. // return
  91. // }
  92. //
  93. // c.Status(204)
  94. }