api_account.go 2.4 KB

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