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