forked from forgejo/forgejo
Unify user update methods (#28733)
Fixes #28660 Fixes an admin api bug related to `user.LoginSource` Fixed `/user/emails` response not identical to GitHub api This PR unifies the user update methods. The goal is to keep the logic only at one place (having audit logs in mind). For example, do the password checks only in one method not everywhere a password is updated. After that PR is merged, the user creation should be next.
This commit is contained in:
parent
b4513f48ce
commit
f8b471ace1
42 changed files with 1383 additions and 1068 deletions
|
@ -9,10 +9,10 @@ import (
|
|||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
user_service "code.gitea.io/gitea/services/user"
|
||||
)
|
||||
|
||||
// ListEmails list all of the authenticated user's email addresses
|
||||
|
@ -56,22 +56,14 @@ func AddEmail(ctx *context.APIContext) {
|
|||
// "$ref": "#/responses/EmailList"
|
||||
// "422":
|
||||
// "$ref": "#/responses/validationError"
|
||||
|
||||
form := web.GetForm(ctx).(*api.CreateEmailOption)
|
||||
if len(form.Emails) == 0 {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
|
||||
return
|
||||
}
|
||||
|
||||
emails := make([]*user_model.EmailAddress, len(form.Emails))
|
||||
for i := range form.Emails {
|
||||
emails[i] = &user_model.EmailAddress{
|
||||
UID: ctx.Doer.ID,
|
||||
Email: form.Emails[i],
|
||||
IsActivated: !setting.Service.RegisterEmailConfirm,
|
||||
}
|
||||
}
|
||||
|
||||
if err := user_model.AddEmailAddresses(ctx, emails); err != nil {
|
||||
if err := user_service.AddEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil {
|
||||
if user_model.IsErrEmailAlreadyUsed(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
|
||||
} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
|
||||
|
@ -91,11 +83,17 @@ func AddEmail(ctx *context.APIContext) {
|
|||
return
|
||||
}
|
||||
|
||||
apiEmails := make([]*api.Email, len(emails))
|
||||
for i := range emails {
|
||||
apiEmails[i] = convert.ToEmail(emails[i])
|
||||
emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusCreated, &apiEmails)
|
||||
|
||||
apiEmails := make([]*api.Email, 0, len(emails))
|
||||
for _, email := range emails {
|
||||
apiEmails = append(apiEmails, convert.ToEmail(email))
|
||||
}
|
||||
ctx.JSON(http.StatusCreated, apiEmails)
|
||||
}
|
||||
|
||||
// DeleteEmail delete email
|
||||
|
@ -115,26 +113,19 @@ func DeleteEmail(ctx *context.APIContext) {
|
|||
// "$ref": "#/responses/empty"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
form := web.GetForm(ctx).(*api.DeleteEmailOption)
|
||||
if len(form.Emails) == 0 {
|
||||
ctx.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
emails := make([]*user_model.EmailAddress, len(form.Emails))
|
||||
for i := range form.Emails {
|
||||
emails[i] = &user_model.EmailAddress{
|
||||
Email: form.Emails[i],
|
||||
UID: ctx.Doer.ID,
|
||||
}
|
||||
}
|
||||
|
||||
if err := user_model.DeleteEmailAddresses(ctx, emails); err != nil {
|
||||
if err := user_service.DeleteEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil {
|
||||
if user_model.IsErrEmailAddressNotExist(err) {
|
||||
ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
|
||||
return
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
|
||||
}
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
|
|
|
@ -6,11 +6,12 @@ package user
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
user_service "code.gitea.io/gitea/services/user"
|
||||
)
|
||||
|
||||
// GetUserSettings returns user settings
|
||||
|
@ -44,36 +45,18 @@ func UpdateUserSettings(ctx *context.APIContext) {
|
|||
|
||||
form := web.GetForm(ctx).(*api.UserSettingsOptions)
|
||||
|
||||
if form.FullName != nil {
|
||||
ctx.Doer.FullName = *form.FullName
|
||||
opts := &user_service.UpdateOptions{
|
||||
FullName: optional.FromPtr(form.FullName),
|
||||
Description: optional.FromPtr(form.Description),
|
||||
Website: optional.FromPtr(form.Website),
|
||||
Location: optional.FromPtr(form.Location),
|
||||
Language: optional.FromPtr(form.Language),
|
||||
Theme: optional.FromPtr(form.Theme),
|
||||
DiffViewStyle: optional.FromPtr(form.DiffViewStyle),
|
||||
KeepEmailPrivate: optional.FromPtr(form.HideEmail),
|
||||
KeepActivityPrivate: optional.FromPtr(form.HideActivity),
|
||||
}
|
||||
if form.Description != nil {
|
||||
ctx.Doer.Description = *form.Description
|
||||
}
|
||||
if form.Website != nil {
|
||||
ctx.Doer.Website = *form.Website
|
||||
}
|
||||
if form.Location != nil {
|
||||
ctx.Doer.Location = *form.Location
|
||||
}
|
||||
if form.Language != nil {
|
||||
ctx.Doer.Language = *form.Language
|
||||
}
|
||||
if form.Theme != nil {
|
||||
ctx.Doer.Theme = *form.Theme
|
||||
}
|
||||
if form.DiffViewStyle != nil {
|
||||
ctx.Doer.DiffViewStyle = *form.DiffViewStyle
|
||||
}
|
||||
|
||||
if form.HideEmail != nil {
|
||||
ctx.Doer.KeepEmailPrivate = *form.HideEmail
|
||||
}
|
||||
if form.HideActivity != nil {
|
||||
ctx.Doer.KeepActivityPrivate = *form.HideActivity
|
||||
}
|
||||
|
||||
if err := user_model.UpdateUser(ctx, ctx.Doer, false); err != nil {
|
||||
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue