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.

email.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. )
  14. // ListEmails list all of the authenticated user's email addresses
  15. // see https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
  16. func ListEmails(ctx *context.APIContext) {
  17. // swagger:operation GET /user/emails user userListEmails
  18. // ---
  19. // summary: List the authenticated user's email addresses
  20. // produces:
  21. // - application/json
  22. // responses:
  23. // "200":
  24. // "$ref": "#/responses/EmailList"
  25. emails, err := models.GetEmailAddresses(ctx.User.ID)
  26. if err != nil {
  27. ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
  28. return
  29. }
  30. apiEmails := make([]*api.Email, len(emails))
  31. for i := range emails {
  32. apiEmails[i] = convert.ToEmail(emails[i])
  33. }
  34. ctx.JSON(http.StatusOK, &apiEmails)
  35. }
  36. // AddEmail add an email address
  37. func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) {
  38. // swagger:operation POST /user/emails user userAddEmail
  39. // ---
  40. // summary: Add email addresses
  41. // produces:
  42. // - application/json
  43. // parameters:
  44. // - name: options
  45. // in: body
  46. // schema:
  47. // "$ref": "#/definitions/CreateEmailOption"
  48. // parameters:
  49. // - name: body
  50. // in: body
  51. // schema:
  52. // "$ref": "#/definitions/CreateEmailOption"
  53. // responses:
  54. // '201':
  55. // "$ref": "#/responses/EmailList"
  56. // "422":
  57. // "$ref": "#/responses/validationError"
  58. if len(form.Emails) == 0 {
  59. ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
  60. return
  61. }
  62. emails := make([]*models.EmailAddress, len(form.Emails))
  63. for i := range form.Emails {
  64. emails[i] = &models.EmailAddress{
  65. UID: ctx.User.ID,
  66. Email: form.Emails[i],
  67. IsActivated: !setting.Service.RegisterEmailConfirm,
  68. }
  69. }
  70. if err := models.AddEmailAddresses(emails); err != nil {
  71. if models.IsErrEmailAlreadyUsed(err) {
  72. ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email)
  73. } else if models.IsErrEmailInvalid(err) {
  74. errMsg := fmt.Sprintf("Email address %s invalid", err.(models.ErrEmailInvalid).Email)
  75. ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
  76. } else {
  77. ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
  78. }
  79. return
  80. }
  81. apiEmails := make([]*api.Email, len(emails))
  82. for i := range emails {
  83. apiEmails[i] = convert.ToEmail(emails[i])
  84. }
  85. ctx.JSON(http.StatusCreated, &apiEmails)
  86. }
  87. // DeleteEmail delete email
  88. func DeleteEmail(ctx *context.APIContext, form api.DeleteEmailOption) {
  89. // swagger:operation DELETE /user/emails user userDeleteEmail
  90. // ---
  91. // summary: Delete email addresses
  92. // produces:
  93. // - application/json
  94. // parameters:
  95. // - name: body
  96. // in: body
  97. // schema:
  98. // "$ref": "#/definitions/DeleteEmailOption"
  99. // responses:
  100. // "204":
  101. // "$ref": "#/responses/empty"
  102. if len(form.Emails) == 0 {
  103. ctx.Status(http.StatusNoContent)
  104. return
  105. }
  106. emails := make([]*models.EmailAddress, len(form.Emails))
  107. for i := range form.Emails {
  108. emails[i] = &models.EmailAddress{
  109. Email: form.Emails[i],
  110. UID: ctx.User.ID,
  111. }
  112. }
  113. if err := models.DeleteEmailAddresses(emails); err != nil {
  114. ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
  115. return
  116. }
  117. ctx.Status(http.StatusNoContent)
  118. }