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