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 4.4 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "net/mail"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/password"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/services/mailer"
  18. )
  19. func parseLoginSource(ctx *context.Context, u *models.User, sourceID int64, loginName string) {
  20. if sourceID == 0 {
  21. return
  22. }
  23. source, err := models.GetLoginSourceByID(sourceID)
  24. if err != nil {
  25. if models.IsErrLoginSourceNotExist(err) {
  26. ctx.Error(http.StatusUnprocessableEntity, "", err.Error())
  27. } else {
  28. ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err.Error())
  29. }
  30. return
  31. }
  32. u.LoginType = source.Type
  33. u.LoginSource = source.ID
  34. u.LoginName = loginName
  35. }
  36. // CreateUser create a user
  37. func CreateUser(ctx *context.Context, form api.CreateUserOption) {
  38. // swagger:operation POST /admin/users admin adminCreateUser
  39. // ---
  40. // summary: Create a user
  41. // consumes:
  42. // - application/json
  43. // produces:
  44. // - application/json
  45. // parameters:
  46. // - name: body
  47. // in: body
  48. // schema:
  49. // "$ref": "#/definitions/CreateUserOption"
  50. // responses:
  51. // "201":
  52. // "$ref": "#/responses/User"
  53. // "400":
  54. // "$ref": "#/responses/error"
  55. // "403":
  56. // "$ref": "#/responses/forbidden"
  57. // "422":
  58. // "$ref": "#/responses/validationError"
  59. _, err1 := mail.ParseAddress(form.Email)
  60. if err1 != nil {
  61. ctx.JSON(http.StatusBadRequest, map[string]string{
  62. "error_msg": "Email format is wrong.",
  63. })
  64. return
  65. }
  66. u := &models.User{
  67. Name: form.Username,
  68. FullName: form.FullName,
  69. Email: form.Email,
  70. Passwd: form.Password,
  71. MustChangePassword: false,
  72. IsActive: !setting.Service.RegisterEmailConfirm,
  73. LoginType: models.LoginPlain,
  74. }
  75. if form.MustChangePassword != nil {
  76. u.MustChangePassword = *form.MustChangePassword
  77. }
  78. if strings.Contains(form.Email, " ") {
  79. log.Error("CreateUser failed: email(%s) contains blank space", form.Email, ctx.Data["MsgID"])
  80. ctx.JSON(http.StatusBadRequest, map[string]string{
  81. "error_msg": "Email contains blank space",
  82. })
  83. return
  84. }
  85. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  86. if ctx.Written() {
  87. return
  88. }
  89. if !password.IsComplexEnough(form.Password) || len(form.Password) < setting.MinPasswordLength {
  90. log.Error("CreateUser failed: PasswordComplexity", ctx.Data["MsgID"])
  91. ctx.JSON(http.StatusBadRequest, map[string]string{
  92. "error_msg": "PasswordComplexity",
  93. })
  94. return
  95. }
  96. if err := models.CreateUser(u); err != nil {
  97. if models.IsErrUserAlreadyExist(err) ||
  98. models.IsErrEmailAlreadyUsed(err) ||
  99. models.IsErrNameReserved(err) ||
  100. models.IsErrNameCharsNotAllowed(err) ||
  101. models.IsErrNamePatternNotAllowed(err) {
  102. log.Error("CreateUser failed:%v", err.Error(), ctx.Data["MsgID"])
  103. ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
  104. "error_msg": err.Error(),
  105. })
  106. } else {
  107. log.Error("CreateUser failed:%v", err.Error(), ctx.Data["MsgID"])
  108. ctx.JSON(http.StatusInternalServerError, map[string]string{
  109. "error_msg": err.Error(),
  110. })
  111. }
  112. return
  113. }
  114. err := models.AddEmailAddress(&models.EmailAddress{
  115. UID: u.ID,
  116. Email: form.Email,
  117. IsActivated: !setting.Service.RegisterEmailConfirm,
  118. })
  119. if err != nil {
  120. log.Error("AddEmailAddress failed:%v", err.Error(), ctx.Data["MsgID"])
  121. ctx.JSON(http.StatusInternalServerError, map[string]string{
  122. "error_msg": err.Error(),
  123. })
  124. return
  125. }
  126. // Send confirmation email
  127. if setting.Service.RegisterEmailConfirm{
  128. mailer.SendActivateAccountMail(ctx.Locale, u)
  129. if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  130. log.Error("Set cache(MailResendLimit) fail: %v", err)
  131. }
  132. }
  133. log.Trace("Account created (%s): %s", ctx.User.Name, u.Name, ctx.Data["MsgID"])
  134. // Send email notification.
  135. if form.SendNotify {
  136. mailer.SendRegisterNotifyMail(ctx.Locale, u)
  137. }
  138. ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin))
  139. }