2015-12-15 22:57:18 -05:00
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import (
|
2019-08-08 23:53:43 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-01-22 08:20:53 -05:00
|
|
|
"github.com/cockroachdb/errors"
|
2018-05-27 08:53:48 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
2015-12-15 22:57:18 -05:00
|
|
|
|
2020-02-27 18:06:38 +08:00
|
|
|
"gogs.io/gogs/internal/conf"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
2024-02-18 19:39:41 -05:00
|
|
|
"gogs.io/gogs/internal/database"
|
2020-03-16 01:22:27 +08:00
|
|
|
"gogs.io/gogs/internal/route/api/v1/convert"
|
2015-12-15 22:57:18 -05:00
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListEmails(c *context.APIContext) {
|
2024-03-27 23:18:59 -04:00
|
|
|
emails, err := database.Handle.Users().ListEmails(c.Req.Context(), c.User.ID)
|
2015-12-15 22:57:18 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "get email addresses")
|
2015-12-15 22:57:18 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
apiEmails := make([]*api.Email, len(emails))
|
|
|
|
|
for i := range emails {
|
2020-03-16 01:22:27 +08:00
|
|
|
apiEmails[i] = convert.ToEmail(emails[i])
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(&apiEmails)
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func AddEmail(c *context.APIContext, form api.CreateEmailOption) {
|
2015-12-15 22:57:18 -05:00
|
|
|
if len(form.Emails) == 0 {
|
2019-08-08 23:53:43 -07:00
|
|
|
c.Status(http.StatusUnprocessableEntity)
|
2015-12-15 22:57:18 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 20:15:47 -04:00
|
|
|
apiEmails := make([]*api.Email, 0, len(form.Emails))
|
|
|
|
|
for _, email := range form.Emails {
|
2024-03-27 23:18:59 -04:00
|
|
|
err := database.Handle.Users().AddEmail(c.Req.Context(), c.User.ID, email, !conf.Auth.RequireEmailConfirmation)
|
2023-05-14 20:15:47 -04:00
|
|
|
if err != nil {
|
2024-02-18 19:39:41 -05:00
|
|
|
if database.IsErrEmailAlreadyUsed(err) {
|
|
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.Errorf("email address has been used: %s", err.(database.ErrEmailAlreadyUsed).Email()))
|
2023-05-14 20:15:47 -04:00
|
|
|
} else {
|
|
|
|
|
c.Error(err, "add email addresses")
|
|
|
|
|
}
|
|
|
|
|
return
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-14 20:15:47 -04:00
|
|
|
apiEmails = append(apiEmails,
|
|
|
|
|
&api.Email{
|
|
|
|
|
Email: email,
|
|
|
|
|
Verified: !conf.Auth.RequireEmailConfirmation,
|
|
|
|
|
},
|
|
|
|
|
)
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSON(http.StatusCreated, &apiEmails)
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func DeleteEmail(c *context.APIContext, form api.CreateEmailOption) {
|
2023-05-14 20:15:47 -04:00
|
|
|
for _, email := range form.Emails {
|
|
|
|
|
if email == c.User.Email {
|
|
|
|
|
c.ErrorStatus(http.StatusBadRequest, errors.Errorf("cannot delete primary email %q", email))
|
|
|
|
|
return
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-27 23:18:59 -04:00
|
|
|
err := database.Handle.Users().DeleteEmail(c.Req.Context(), c.User.ID, email)
|
2023-05-14 20:15:47 -04:00
|
|
|
if err != nil {
|
|
|
|
|
c.Error(err, "delete email addresses")
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.NoContent()
|
2015-12-15 22:57:18 -05:00
|
|
|
}
|