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.

121 lines
3.1 KiB

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