api_account.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 model dtos.AddCollaboratorCommand
  20. if !c.JsonBody(&model) {
  21. c.JsonApiErr(400, "Invalid request", nil)
  22. return
  23. }
  24. accountToAdd, err := m.GetAccountByLogin(model.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. var collaborator = m.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, m.ROLE_READ_WRITE)
  34. err = m.AddCollaborator(collaborator)
  35. if err != nil {
  36. c.JsonApiErr(500, "Could not add collaborator", err)
  37. return
  38. }
  39. c.Status(204)
  40. }
  41. func GetOtherAccounts(c *middleware.Context) {
  42. otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
  43. if err != nil {
  44. c.JSON(500, utils.DynMap{"message": err.Error()})
  45. return
  46. }
  47. var result []*dtos.OtherAccount
  48. result = append(result, &dtos.OtherAccount{
  49. Id: c.UserAccount.Id,
  50. Role: "owner",
  51. IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
  52. Name: c.UserAccount.Email,
  53. })
  54. for _, other := range otherAccounts {
  55. result = append(result, &dtos.OtherAccount{
  56. Id: other.Id,
  57. Role: other.Role,
  58. Name: other.Email,
  59. IsUsing: other.Id == c.UserAccount.UsingAccountId,
  60. })
  61. }
  62. c.JSON(200, result)
  63. }
  64. func SetUsingAccount(c *middleware.Context) {
  65. usingAccountId := c.ParamsInt64(":id")
  66. account := c.UserAccount
  67. otherAccounts, err := m.GetOtherAccountsFor(c.UserAccount.Id)
  68. if err != nil {
  69. c.JSON(500, utils.DynMap{"message": err.Error()})
  70. return
  71. }
  72. // validate that the account id in the list
  73. valid := false
  74. for _, other := range otherAccounts {
  75. if other.Id == usingAccountId {
  76. valid = true
  77. }
  78. }
  79. if !valid {
  80. c.Status(401)
  81. return
  82. }
  83. account.UsingAccountId = usingAccountId
  84. err = m.SaveAccount(account)
  85. if err != nil {
  86. c.JSON(500, utils.DynMap{"message": err.Error()})
  87. return
  88. }
  89. c.Status(204)
  90. }