api_account.go 2.7 KB

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