gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "fmt"
  6. "net/http"
  7. user_model "code.gitea.io/gitea/models/user"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/modules/web"
  10. "code.gitea.io/gitea/services/context"
  11. "code.gitea.io/gitea/services/convert"
  12. user_service "code.gitea.io/gitea/services/user"
  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 := user_model.GetEmailAddresses(ctx, ctx.Doer.ID)
  26. if err != nil {
  27. ctx.APIErrorInternal(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) {
  38. // swagger:operation POST /user/emails user userAddEmail
  39. // ---
  40. // summary: Add email addresses
  41. // produces:
  42. // - application/json
  43. // parameters:
  44. // - name: body
  45. // in: body
  46. // schema:
  47. // "$ref": "#/definitions/CreateEmailOption"
  48. // responses:
  49. // '201':
  50. // "$ref": "#/responses/EmailList"
  51. // "422":
  52. // "$ref": "#/responses/validationError"
  53. form := web.GetForm(ctx).(*api.CreateEmailOption)
  54. if len(form.Emails) == 0 {
  55. ctx.APIError(http.StatusUnprocessableEntity, "Email list empty")
  56. return
  57. }
  58. if err := user_service.AddEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil {
  59. if user_model.IsErrEmailAlreadyUsed(err) {
  60. ctx.APIError(http.StatusUnprocessableEntity, "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
  61. } else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
  62. email := ""
  63. if typedError, ok := err.(user_model.ErrEmailInvalid); ok {
  64. email = typedError.Email
  65. }
  66. if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok {
  67. email = typedError.Email
  68. }
  69. errMsg := fmt.Sprintf("Email address %q invalid", email)
  70. ctx.APIError(http.StatusUnprocessableEntity, errMsg)
  71. } else {
  72. ctx.APIErrorInternal(err)
  73. }
  74. return
  75. }
  76. emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID)
  77. if err != nil {
  78. ctx.APIErrorInternal(err)
  79. return
  80. }
  81. apiEmails := make([]*api.Email, 0, len(emails))
  82. for _, email := range emails {
  83. apiEmails = append(apiEmails, convert.ToEmail(email))
  84. }
  85. ctx.JSON(http.StatusCreated, apiEmails)
  86. }
  87. // DeleteEmail delete email
  88. func DeleteEmail(ctx *context.APIContext) {
  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. // "404":
  103. // "$ref": "#/responses/notFound"
  104. form := web.GetForm(ctx).(*api.DeleteEmailOption)
  105. if len(form.Emails) == 0 {
  106. ctx.Status(http.StatusNoContent)
  107. return
  108. }
  109. if err := user_service.DeleteEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil {
  110. if user_model.IsErrEmailAddressNotExist(err) {
  111. ctx.APIError(http.StatusNotFound, err)
  112. } else {
  113. ctx.APIErrorInternal(err)
  114. }
  115. return
  116. }
  117. ctx.Status(http.StatusNoContent)
  118. }