api_account.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. usingAccountId := c.ParamsInt64(":id")
  75. account := c.UserAccount
  76. otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
  77. if err != nil {
  78. c.JSON(500, utils.DynMap{"message": err.Error()})
  79. return
  80. }
  81. // validate that the account id in the list
  82. valid := false
  83. for _, other := range otherAccounts {
  84. if other.Id == usingAccountId {
  85. valid = true
  86. }
  87. }
  88. if !valid {
  89. c.Status(401)
  90. return
  91. }
  92. account.UsingAccountId = usingAccountId
  93. err = models.SaveAccount(account)
  94. if err != nil {
  95. c.JSON(500, utils.DynMap{"message": err.Error()})
  96. return
  97. }
  98. c.Status(204)
  99. }