From 7b265acf704fd4b9295f09c155c83827639ab91b Mon Sep 17 00:00:00 2001 From: lewis <747342561@qq.com> Date: Fri, 9 Jul 2021 17:22:30 +0800 Subject: [PATCH] mod register resp --- routers/routes/routes.go | 4 +- routers/secure/user.go | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100755 routers/secure/user.go diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 82dc2de9f..c59ecefca 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -6,6 +6,7 @@ package routes import ( "bytes" + "code.gitea.io/gitea/routers/secure" "encoding/gob" "net/http" "path" @@ -27,7 +28,6 @@ import ( "code.gitea.io/gitea/routers" "code.gitea.io/gitea/routers/admin" apiv1 "code.gitea.io/gitea/routers/api/v1" - apiAdmin "code.gitea.io/gitea/routers/api/v1/admin" "code.gitea.io/gitea/routers/dev" "code.gitea.io/gitea/routers/events" "code.gitea.io/gitea/routers/org" @@ -1129,7 +1129,7 @@ func RegisterRoutes(m *macaron.Macaron) { //secure api, m.Group("/secure", func() { - m.Post("/user", binding.Bind(structs.CreateUserOption{}), apiAdmin.CreateUser) + m.Post("/user", binding.Bind(structs.CreateUserOption{}), secure.CreateUser) }, reqBasicAuth) m.Group("/api/internal", func() { diff --git a/routers/secure/user.go b/routers/secure/user.go new file mode 100755 index 000000000..e9017cfd4 --- /dev/null +++ b/routers/secure/user.go @@ -0,0 +1,116 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package secure + +import ( + "net/http" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/password" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/services/mailer" +) + +func parseLoginSource(ctx *context.Context, u *models.User, sourceID int64, loginName string) { + if sourceID == 0 { + return + } + + source, err := models.GetLoginSourceByID(sourceID) + if err != nil { + if models.IsErrLoginSourceNotExist(err) { + ctx.Error(http.StatusUnprocessableEntity, "", err.Error()) + } else { + ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err.Error()) + } + return + } + + u.LoginType = source.Type + u.LoginSource = source.ID + u.LoginName = loginName +} + +// CreateUser create a user +func CreateUser(ctx *context.Context, form api.CreateUserOption) { + // swagger:operation POST /admin/users admin adminCreateUser + // --- + // summary: Create a user + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: body + // in: body + // schema: + // "$ref": "#/definitions/CreateUserOption" + // responses: + // "201": + // "$ref": "#/responses/User" + // "400": + // "$ref": "#/responses/error" + // "403": + // "$ref": "#/responses/forbidden" + // "422": + // "$ref": "#/responses/validationError" + + u := &models.User{ + Name: form.Username, + FullName: form.FullName, + Email: form.Email, + Passwd: form.Password, + MustChangePassword: false, + IsActive: true, + LoginType: models.LoginPlain, + } + if form.MustChangePassword != nil { + u.MustChangePassword = *form.MustChangePassword + } + + parseLoginSource(ctx, u, form.SourceID, form.LoginName) + if ctx.Written() { + return + } + if !password.IsComplexEnough(form.Password) { + log.Error("CreateUser failed: PasswordComplexity", ctx.Data["MsgID"]) + ctx.JSON(http.StatusBadRequest, map[string]string{ + "error_msg": "PasswordComplexity", + }) + return + } + if err := models.CreateUser(u); err != nil { + if models.IsErrUserAlreadyExist(err) || + models.IsErrEmailAlreadyUsed(err) || + models.IsErrNameReserved(err) || + models.IsErrNameCharsNotAllowed(err) || + models.IsErrNamePatternNotAllowed(err) { + log.Error("CreateUser failed:%v",err.Error(), ctx.Data["MsgID"]) + ctx.JSON(http.StatusUnprocessableEntity, map[string]string{ + "error_msg": err.Error(), + }) + } else { + log.Error("CreateUser failed:%v",err.Error(), ctx.Data["MsgID"]) + ctx.JSON(http.StatusInternalServerError, map[string]string{ + "error_msg": err.Error(), + }) + } + return + } + log.Trace("Account created (%s): %s", ctx.User.Name, u.Name, ctx.Data["MsgID"]) + + // Send email notification. + if form.SendNotify { + mailer.SendRegisterNotifyMail(ctx.Locale, u) + } + ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) +} + + +