forked from Gitlink/gitea-1156
fix: edit user
This commit is contained in:
parent
4c176c1a0a
commit
4a55172452
|
@ -24,6 +24,7 @@ type CreateUserOption struct {
|
||||||
|
|
||||||
// EditUserOption edit user options
|
// EditUserOption edit user options
|
||||||
type EditUserOption struct {
|
type EditUserOption struct {
|
||||||
|
NewName string `json:"new_name" binding:"MaxSize(40)"`
|
||||||
// required: true
|
// required: true
|
||||||
SourceID int64 `json:"source_id"`
|
SourceID int64 `json:"source_id"`
|
||||||
// required: true
|
// required: true
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
|
@ -154,12 +155,13 @@ func EditUser(ctx *context.APIContext) {
|
||||||
// "$ref": "#/responses/forbidden"
|
// "$ref": "#/responses/forbidden"
|
||||||
// "422":
|
// "422":
|
||||||
// "$ref": "#/responses/validationError"
|
// "$ref": "#/responses/validationError"
|
||||||
|
|
||||||
|
log.Info("user setting begin :%s", ctx.User.Name)
|
||||||
form := web.GetForm(ctx).(*api.EditUserOption)
|
form := web.GetForm(ctx).(*api.EditUserOption)
|
||||||
u := user.GetUserByParams(ctx)
|
u := user.GetUserByParams(ctx)
|
||||||
if ctx.Written() {
|
if ctx.Written() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
parseLoginSource(ctx, u, form.SourceID, form.LoginName)
|
parseLoginSource(ctx, u, form.SourceID, form.LoginName)
|
||||||
if ctx.Written() {
|
if ctx.Written() {
|
||||||
return
|
return
|
||||||
|
@ -193,6 +195,28 @@ func EditUser(ctx *context.APIContext) {
|
||||||
if form.MustChangePassword != nil {
|
if form.MustChangePassword != nil {
|
||||||
u.MustChangePassword = *form.MustChangePassword
|
u.MustChangePassword = *form.MustChangePassword
|
||||||
}
|
}
|
||||||
|
// Non-local users are not allowed to change their username.
|
||||||
|
if len(form.NewName) != 0 && u.IsLocal() {
|
||||||
|
|
||||||
|
// Check if user name has been changed
|
||||||
|
if err := models.ChangeUserName(u, form.NewName); err != nil {
|
||||||
|
switch {
|
||||||
|
case models.IsErrUserAlreadyExist(err):
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("user name is already taken [name: %s]", form.NewName), err)
|
||||||
|
case models.IsErrNameReserved(err):
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("user name is reserved [name:%s]", form.NewName), err)
|
||||||
|
case models.IsErrNamePatternNotAllowed(err):
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("user name's pattern is not allowed [name:%s]", form.NewName), err)
|
||||||
|
case models.IsErrNameCharsNotAllowed(err):
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("user name's chars is not allowed [name:%s]", form.NewName), err)
|
||||||
|
default:
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, "ChangeUserName", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.Name = form.NewName
|
||||||
|
u.LowerName = strings.ToLower(form.NewName)
|
||||||
|
}
|
||||||
|
|
||||||
u.LoginName = form.LoginName
|
u.LoginName = form.LoginName
|
||||||
|
|
||||||
|
@ -243,14 +267,15 @@ func EditUser(ctx *context.APIContext) {
|
||||||
u.IsRestricted = *form.Restricted
|
u.IsRestricted = *form.Restricted
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.UpdateUser(u); err != nil {
|
if err := models.UpdateUserSetting(u); err != nil {
|
||||||
if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) {
|
if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) {
|
||||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("the e-mail is not valid [name %s]", u.Email), err)
|
||||||
} else {
|
} else {
|
||||||
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
|
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
|
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User))
|
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User))
|
||||||
|
@ -405,7 +430,7 @@ func GetAllUsers(ctx *context.APIContext) {
|
||||||
// "$ref": "#/responses/forbidden"
|
// "$ref": "#/responses/forbidden"
|
||||||
|
|
||||||
listOptions := utils.GetListOptions(ctx)
|
listOptions := utils.GetListOptions(ctx)
|
||||||
|
log.Info("user setting begin :%s", ctx.User.Name)
|
||||||
users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
|
users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
|
||||||
Actor: ctx.User,
|
Actor: ctx.User,
|
||||||
Type: models.UserTypeIndividual,
|
Type: models.UserTypeIndividual,
|
||||||
|
|
|
@ -15214,6 +15214,10 @@
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"x-go-name": "MustChangePassword"
|
"x-go-name": "MustChangePassword"
|
||||||
},
|
},
|
||||||
|
"new_name": {
|
||||||
|
"type": "string",
|
||||||
|
"x-go-name": "NewName"
|
||||||
|
},
|
||||||
"password": {
|
"password": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"x-go-name": "Password"
|
"x-go-name": "Password"
|
||||||
|
|
Loading…
Reference in New Issue