1
0

user.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. log "unknwon.dev/clog/v2"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/database"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/route/api/v1/user"
  14. )
  15. func parseLoginSource(c *context.APIContext, sourceID int64) {
  16. if sourceID == 0 {
  17. return
  18. }
  19. _, err := database.Handle.LoginSources().GetByID(c.Req.Context(), sourceID)
  20. if err != nil {
  21. if database.IsErrLoginSourceNotExist(err) {
  22. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  23. } else {
  24. c.Error(err, "get login source by ID")
  25. }
  26. return
  27. }
  28. }
  29. func CreateUser(c *context.APIContext, form api.CreateUserOption) {
  30. parseLoginSource(c, form.SourceID)
  31. if c.Written() {
  32. return
  33. }
  34. user, err := database.Handle.Users().Create(
  35. c.Req.Context(),
  36. form.Username,
  37. form.Email,
  38. database.CreateUserOptions{
  39. FullName: form.FullName,
  40. Password: form.Password,
  41. LoginSource: form.SourceID,
  42. LoginName: form.LoginName,
  43. Activated: true,
  44. },
  45. )
  46. if err != nil {
  47. if database.IsErrUserAlreadyExist(err) ||
  48. database.IsErrEmailAlreadyUsed(err) ||
  49. database.IsErrNameNotAllowed(err) {
  50. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  51. } else {
  52. c.Error(err, "create user")
  53. }
  54. return
  55. }
  56. log.Trace("Account %q created by admin %q", user.Name, c.User.Name)
  57. // Send email notification.
  58. if form.SendNotify && conf.Email.Enabled {
  59. email.SendRegisterNotifyMail(c.Context.Context, database.NewMailerUser(user))
  60. }
  61. c.JSON(http.StatusCreated, user.APIFormat())
  62. }
  63. func EditUser(c *context.APIContext, form api.EditUserOption) {
  64. u := user.GetUserByParams(c)
  65. if c.Written() {
  66. return
  67. }
  68. parseLoginSource(c, form.SourceID)
  69. if c.Written() {
  70. return
  71. }
  72. opts := database.UpdateUserOptions{
  73. LoginSource: &form.SourceID,
  74. LoginName: &form.LoginName,
  75. FullName: &form.FullName,
  76. Website: &form.Website,
  77. Location: &form.Location,
  78. MaxRepoCreation: form.MaxRepoCreation,
  79. IsActivated: form.Active,
  80. IsAdmin: form.Admin,
  81. AllowGitHook: form.AllowGitHook,
  82. AllowImportLocal: form.AllowImportLocal,
  83. ProhibitLogin: nil, // TODO: Add this option to API
  84. }
  85. if form.Password != "" {
  86. opts.Password = &form.Password
  87. }
  88. if u.Email != form.Email {
  89. opts.Email = &form.Email
  90. }
  91. err := database.Handle.Users().Update(c.Req.Context(), u.ID, opts)
  92. if err != nil {
  93. if database.IsErrEmailAlreadyUsed(err) {
  94. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  95. } else {
  96. c.Error(err, "update user")
  97. }
  98. return
  99. }
  100. log.Trace("Account updated by admin %q: %s", c.User.Name, u.Name)
  101. u, err = database.Handle.Users().GetByID(c.Req.Context(), u.ID)
  102. if err != nil {
  103. c.Error(err, "get user")
  104. return
  105. }
  106. c.JSONSuccess(u.APIFormat())
  107. }
  108. func DeleteUser(c *context.APIContext) {
  109. u := user.GetUserByParams(c)
  110. if c.Written() {
  111. return
  112. }
  113. if err := database.Handle.Users().DeleteByID(c.Req.Context(), u.ID, false); err != nil {
  114. if database.IsErrUserOwnRepos(err) ||
  115. database.IsErrUserHasOrgs(err) {
  116. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  117. } else {
  118. c.Error(err, "delete user")
  119. }
  120. return
  121. }
  122. log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
  123. c.NoContent()
  124. }
  125. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  126. u := user.GetUserByParams(c)
  127. if c.Written() {
  128. return
  129. }
  130. user.CreateUserPublicKey(c, form, u.ID)
  131. }