You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package secure
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/password"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/services/mailer"
  15. )
  16. func parseLoginSource(ctx *context.Context, u *models.User, sourceID int64, loginName string) {
  17. if sourceID == 0 {
  18. return
  19. }
  20. source, err := models.GetLoginSourceByID(sourceID)
  21. if err != nil {
  22. if models.IsErrLoginSourceNotExist(err) {
  23. ctx.Error(http.StatusUnprocessableEntity, "", err.Error())
  24. } else {
  25. ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err.Error())
  26. }
  27. return
  28. }
  29. u.LoginType = source.Type
  30. u.LoginSource = source.ID
  31. u.LoginName = loginName
  32. }
  33. // CreateUser create a user
  34. func CreateUser(ctx *context.Context, form api.CreateUserOption) {
  35. // swagger:operation POST /admin/users admin adminCreateUser
  36. // ---
  37. // summary: Create a user
  38. // consumes:
  39. // - application/json
  40. // produces:
  41. // - application/json
  42. // parameters:
  43. // - name: body
  44. // in: body
  45. // schema:
  46. // "$ref": "#/definitions/CreateUserOption"
  47. // responses:
  48. // "201":
  49. // "$ref": "#/responses/User"
  50. // "400":
  51. // "$ref": "#/responses/error"
  52. // "403":
  53. // "$ref": "#/responses/forbidden"
  54. // "422":
  55. // "$ref": "#/responses/validationError"
  56. u := &models.User{
  57. Name: form.Username,
  58. FullName: form.FullName,
  59. Email: form.Email,
  60. Passwd: form.Password,
  61. MustChangePassword: false,
  62. IsActive: true,
  63. LoginType: models.LoginPlain,
  64. }
  65. if form.MustChangePassword != nil {
  66. u.MustChangePassword = *form.MustChangePassword
  67. }
  68. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  69. if ctx.Written() {
  70. return
  71. }
  72. if !password.IsComplexEnough(form.Password) {
  73. log.Error("CreateUser failed: PasswordComplexity", ctx.Data["MsgID"])
  74. ctx.JSON(http.StatusBadRequest, map[string]string{
  75. "error_msg": "PasswordComplexity",
  76. })
  77. return
  78. }
  79. if err := models.CreateUser(u); err != nil {
  80. if models.IsErrUserAlreadyExist(err) ||
  81. models.IsErrEmailAlreadyUsed(err) ||
  82. models.IsErrNameReserved(err) ||
  83. models.IsErrNameCharsNotAllowed(err) ||
  84. models.IsErrNamePatternNotAllowed(err) {
  85. log.Error("CreateUser failed:%v",err.Error(), ctx.Data["MsgID"])
  86. ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
  87. "error_msg": err.Error(),
  88. })
  89. } else {
  90. log.Error("CreateUser failed:%v",err.Error(), ctx.Data["MsgID"])
  91. ctx.JSON(http.StatusInternalServerError, map[string]string{
  92. "error_msg": err.Error(),
  93. })
  94. }
  95. return
  96. }
  97. log.Trace("Account created (%s): %s", ctx.User.Name, u.Name, ctx.Data["MsgID"])
  98. // Send email notification.
  99. if form.SendNotify {
  100. mailer.SendRegisterNotifyMail(ctx.Locale, u)
  101. }
  102. ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
  103. }