You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web"
  14. )
  15. // ListEmails list all of the authenticated user's email addresses
  16. // see https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
  17. func ListEmails(ctx *context.APIContext) {
  18. // swagger:operation GET /user/emails user userListEmails
  19. // ---
  20. // summary: List the authenticated user's email addresses
  21. // produces:
  22. // - application/json
  23. // responses:
  24. // "200":
  25. // "$ref": "#/responses/EmailList"
  26. emails, err := models.GetEmailAddresses(ctx.User.ID)
  27. if err != nil {
  28. ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
  29. return
  30. }
  31. apiEmails := make([]*api.Email, len(emails))
  32. for i := range emails {
  33. apiEmails[i] = convert.ToEmail(emails[i])
  34. }
  35. ctx.JSON(http.StatusOK, &apiEmails)
  36. }
  37. // AddEmail add an email address
  38. func AddEmail(ctx *context.APIContext) {
  39. // swagger:operation POST /user/emails user userAddEmail
  40. // ---
  41. // summary: Add email addresses
  42. // produces:
  43. // - application/json
  44. // parameters:
  45. // - name: options
  46. // in: body
  47. // schema:
  48. // "$ref": "#/definitions/CreateEmailOption"
  49. // parameters:
  50. // - name: body
  51. // in: body
  52. // schema:
  53. // "$ref": "#/definitions/CreateEmailOption"
  54. // responses:
  55. // '201':
  56. // "$ref": "#/responses/EmailList"
  57. // "422":
  58. // "$ref": "#/responses/validationError"
  59. form := web.GetForm(ctx).(*api.CreateEmailOption)
  60. if len(form.Emails) == 0 {
  61. ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
  62. return
  63. }
  64. emails := make([]*models.EmailAddress, len(form.Emails))
  65. for i := range form.Emails {
  66. emails[i] = &models.EmailAddress{
  67. UID: ctx.User.ID,
  68. Email: form.Emails[i],
  69. IsActivated: !setting.Service.RegisterEmailConfirm,
  70. }
  71. }
  72. if err := models.AddEmailAddresses(emails); err != nil {
  73. if models.IsErrEmailAlreadyUsed(err) {
  74. ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email)
  75. } else if models.IsErrEmailInvalid(err) {
  76. errMsg := fmt.Sprintf("Email address %s invalid", err.(models.ErrEmailInvalid).Email)
  77. ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
  78. } else {
  79. ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
  80. }
  81. return
  82. }
  83. apiEmails := make([]*api.Email, len(emails))
  84. for i := range emails {
  85. apiEmails[i] = convert.ToEmail(emails[i])
  86. }
  87. ctx.JSON(http.StatusCreated, &apiEmails)
  88. }
  89. // DeleteEmail delete email
  90. func DeleteEmail(ctx *context.APIContext) {
  91. // swagger:operation DELETE /user/emails user userDeleteEmail
  92. // ---
  93. // summary: Delete email addresses
  94. // produces:
  95. // - application/json
  96. // parameters:
  97. // - name: body
  98. // in: body
  99. // schema:
  100. // "$ref": "#/definitions/DeleteEmailOption"
  101. // responses:
  102. // "204":
  103. // "$ref": "#/responses/empty"
  104. // "404":
  105. // "$ref": "#/responses/notFound"
  106. form := web.GetForm(ctx).(*api.DeleteEmailOption)
  107. if len(form.Emails) == 0 {
  108. ctx.Status(http.StatusNoContent)
  109. return
  110. }
  111. emails := make([]*models.EmailAddress, len(form.Emails))
  112. for i := range form.Emails {
  113. emails[i] = &models.EmailAddress{
  114. Email: form.Emails[i],
  115. UID: ctx.User.ID,
  116. }
  117. }
  118. if err := models.DeleteEmailAddresses(emails); err != nil {
  119. if models.IsErrEmailAddressNotExist(err) {
  120. ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
  121. return
  122. }
  123. ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
  124. return
  125. }
  126. ctx.Status(http.StatusNoContent)
  127. }