account.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. )
  7. func GetAccount(c *middleware.Context) {
  8. query := m.GetAccountInfoQuery{Id: c.UserAccount.Id}
  9. err := bus.Dispatch(&query)
  10. if err != nil {
  11. c.JsonApiErr(500, "Failed to fetch collaboratos", err)
  12. return
  13. }
  14. c.JSON(200, query.Result)
  15. }
  16. func AddCollaborator(c *middleware.Context) {
  17. var cmd m.AddCollaboratorCommand
  18. if !c.JsonBody(&cmd) {
  19. c.JsonApiErr(400, "Invalid request", nil)
  20. return
  21. }
  22. userQuery := m.GetAccountByLoginQuery{Login: cmd.Email}
  23. err := bus.Dispatch(&userQuery)
  24. if err != nil {
  25. c.JsonApiErr(404, "Collaborator not found", nil)
  26. return
  27. }
  28. accountToAdd := userQuery.Result
  29. if accountToAdd.Id == c.UserAccount.Id {
  30. c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
  31. return
  32. }
  33. cmd.AccountId = c.UserAccount.Id
  34. cmd.CollaboratorId = accountToAdd.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 RemoveCollaborator(c *middleware.Context) {
  44. collaboratorId := c.ParamsInt64(":id")
  45. cmd := m.RemoveCollaboratorCommand{AccountId: c.UserAccount.Id, CollaboratorId: collaboratorId}
  46. if err := bus.Dispatch(&cmd); err != nil {
  47. c.JsonApiErr(500, "Failed to remove collaborator", err)
  48. }
  49. c.JsonOK("Collaborator removed")
  50. }
  51. func GetOtherAccounts(c *middleware.Context) {
  52. query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
  53. err := bus.Dispatch(&query)
  54. if err != nil {
  55. c.JsonApiErr(500, "Failed to get other accounts", err)
  56. return
  57. }
  58. result := append(query.Result, &m.OtherAccountDTO{
  59. AccountId: c.UserAccount.Id,
  60. Role: "owner",
  61. Email: c.UserAccount.Email,
  62. })
  63. for _, ac := range result {
  64. if ac.AccountId == c.UserAccount.UsingAccountId {
  65. ac.IsUsing = true
  66. break
  67. }
  68. }
  69. c.JSON(200, result)
  70. }
  71. func validateUsingAccount(accountId int64, otherId int64) bool {
  72. if accountId == otherId {
  73. return true
  74. }
  75. query := m.GetOtherAccountsQuery{AccountId: accountId}
  76. err := bus.Dispatch(&query)
  77. if err != nil {
  78. return false
  79. }
  80. // validate that the account id in the list
  81. valid := false
  82. for _, other := range query.Result {
  83. if other.AccountId == otherId {
  84. valid = true
  85. }
  86. }
  87. return valid
  88. }
  89. func SetUsingAccount(c *middleware.Context) {
  90. usingAccountId := c.ParamsInt64(":id")
  91. if !validateUsingAccount(c.UserAccount.Id, usingAccountId) {
  92. c.JsonApiErr(401, "Not a valid account", nil)
  93. return
  94. }
  95. cmd := m.SetUsingAccountCommand{
  96. AccountId: c.UserAccount.Id,
  97. UsingAccountId: usingAccountId,
  98. }
  99. err := bus.Dispatch(&cmd)
  100. if err != nil {
  101. c.JsonApiErr(500, "Failed to update account", err)
  102. return
  103. }
  104. c.JsonOK("Active account changed")
  105. }