api_account.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package api
  2. import (
  3. "github.com/torkelo/grafana-pro/pkg/api/dtos"
  4. "github.com/torkelo/grafana-pro/pkg/middleware"
  5. "github.com/torkelo/grafana-pro/pkg/models"
  6. "github.com/torkelo/grafana-pro/pkg/utils"
  7. )
  8. func GetAccount(c *middleware.Context) {
  9. model := dtos.AccountInfo{
  10. Name: c.UserAccount.Name,
  11. Email: c.UserAccount.Email,
  12. }
  13. collaborators, err := models.GetCollaboratorsForAccount(c.UserAccount.Id)
  14. if err != nil {
  15. c.JsonApiErr(500, "Failed to fetch collaboratos", err)
  16. return
  17. }
  18. for _, collaborator := range collaborators {
  19. model.Collaborators = append(model.Collaborators, &dtos.Collaborator{
  20. AccountId: collaborator.AccountId,
  21. Role: collaborator.Role,
  22. Email: collaborator.Email,
  23. })
  24. }
  25. c.JSON(200, model)
  26. }
  27. func AddCollaborator(c *middleware.Context) {
  28. var model dtos.AddCollaboratorCommand
  29. if !c.JsonBody(&model) {
  30. c.JsonApiErr(400, "Invalid request", nil)
  31. return
  32. }
  33. accountToAdd, err := models.GetAccountByLogin(model.Email)
  34. if err != nil {
  35. c.JsonApiErr(404, "Collaborator not found", nil)
  36. return
  37. }
  38. if accountToAdd.Id == c.UserAccount.Id {
  39. c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
  40. return
  41. }
  42. var collaborator = models.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, models.ROLE_READ_WRITE)
  43. err = models.AddCollaborator(collaborator)
  44. if err != nil {
  45. c.JsonApiErr(500, "Could not add collaborator", err)
  46. return
  47. }
  48. c.Status(204)
  49. }
  50. func GetOtherAccounts(c *middleware.Context) {
  51. otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
  52. if err != nil {
  53. c.JSON(500, utils.DynMap{"message": err.Error()})
  54. return
  55. }
  56. var result []*dtos.OtherAccount
  57. result = append(result, &dtos.OtherAccount{
  58. Id: c.UserAccount.Id,
  59. Role: "owner",
  60. IsUsing: c.UserAccount.Id == c.UserAccount.UsingAccountId,
  61. Name: c.UserAccount.Email,
  62. })
  63. for _, other := range otherAccounts {
  64. result = append(result, &dtos.OtherAccount{
  65. Id: other.Id,
  66. Role: other.Role,
  67. Name: other.Email,
  68. IsUsing: other.Id == c.UserAccount.UsingAccountId,
  69. })
  70. }
  71. c.JSON(200, result)
  72. }
  73. // func SetUsingAccount(c *middleware.Context) {
  74. // idString := c.Params.ByName("id")
  75. // id, _ := strconv.Atoi(idString)
  76. //
  77. // account := auth.userAccount
  78. // otherAccount, err := self.store.GetAccount(id)
  79. // if err != nil {
  80. // c.JSON(500, gin.H{"message": err.Error()})
  81. // return
  82. // }
  83. //
  84. // if otherAccount.Id != account.Id && !otherAccount.HasCollaborator(account.Id) {
  85. // c.Abort(401)
  86. // return
  87. // }
  88. //
  89. // account.UsingAccountId = otherAccount.Id
  90. // err = self.store.UpdateAccount(account)
  91. // if err != nil {
  92. // c.JSON(500, gin.H{"message": err.Error()})
  93. // return
  94. // }
  95. //
  96. // c.Abort(204)
  97. // }