api_account.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. userQuery := m.GetAccountByLoginQuery{Login: cmd.Email}
  24. err := bus.Dispatch(&userQuery)
  25. if err != nil {
  26. c.JsonApiErr(404, "Collaborator not found", nil)
  27. return
  28. }
  29. accountToAdd := userQuery.Result
  30. if accountToAdd.Id == c.UserAccount.Id {
  31. c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
  32. return
  33. }
  34. cmd.AccountId = accountToAdd.Id
  35. cmd.ForAccountId = c.UserAccount.Id
  36. cmd.Role = m.ROLE_READ_WRITE
  37. err = bus.Dispatch(&cmd)
  38. if err != nil {
  39. c.JsonApiErr(500, "Could not add collaborator", err)
  40. return
  41. }
  42. c.JsonOK("Collaborator added")
  43. }
  44. func GetOtherAccounts(c *middleware.Context) {
  45. query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
  46. err := bus.Dispatch(&query)
  47. if err != nil {
  48. c.JSON(500, utils.DynMap{"message": err.Error()})
  49. return
  50. }
  51. result := append(query.Result, &m.OtherAccountDTO{
  52. Id: c.UserAccount.Id,
  53. Role: "owner",
  54. Email: c.UserAccount.Email,
  55. })
  56. for _, ac := range result {
  57. if ac.Id == c.UserAccount.UsingAccountId {
  58. ac.IsUsing = true
  59. break
  60. }
  61. }
  62. c.JSON(200, result)
  63. }
  64. func validateUsingAccount(accountId int64, otherId int64) bool {
  65. if accountId == otherId {
  66. return true
  67. }
  68. query := m.GetOtherAccountsQuery{AccountId: accountId}
  69. err := bus.Dispatch(&query)
  70. if err != nil {
  71. return false
  72. }
  73. // validate that the account id in the list
  74. valid := false
  75. for _, other := range query.Result {
  76. if other.Id == otherId {
  77. valid = true
  78. }
  79. }
  80. return valid
  81. }
  82. func SetUsingAccount(c *middleware.Context) {
  83. usingAccountId := c.ParamsInt64(":id")
  84. if !validateUsingAccount(c.UserAccount.Id, usingAccountId) {
  85. c.JsonApiErr(401, "Not a valid account", nil)
  86. return
  87. }
  88. cmd := m.SetUsingAccountCommand{
  89. AccountId: c.UserAccount.Id,
  90. UsingAccountId: usingAccountId,
  91. }
  92. err := bus.Dispatch(&cmd)
  93. if err != nil {
  94. c.JsonApiErr(500, "Failed to update account", err)
  95. return
  96. }
  97. c.JsonOK("Active account changed")
  98. }