admin_user.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package v1
  2. import (
  3. "net/http"
  4. log "unknwon.dev/clog/v2"
  5. "gogs.io/gogs/internal/conf"
  6. "gogs.io/gogs/internal/context"
  7. "gogs.io/gogs/internal/database"
  8. "gogs.io/gogs/internal/email"
  9. )
  10. func parseLoginSource(c *context.APIContext, sourceID int64) {
  11. if sourceID == 0 {
  12. return
  13. }
  14. _, err := database.Handle.LoginSources().GetByID(c.Req.Context(), sourceID)
  15. if err != nil {
  16. if database.IsErrLoginSourceNotExist(err) {
  17. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  18. } else {
  19. c.Error(err, "get login source by ID")
  20. }
  21. return
  22. }
  23. }
  24. type adminCreateUserRequest struct {
  25. SourceID int64 `json:"source_id"`
  26. LoginName string `json:"login_name"`
  27. Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"`
  28. FullName string `json:"full_name" binding:"MaxSize(100)"`
  29. Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
  30. Password string `json:"password" binding:"MaxSize(255)"`
  31. SendNotify bool `json:"send_notify"`
  32. }
  33. func adminCreateUser(c *context.APIContext, form adminCreateUserRequest) {
  34. parseLoginSource(c, form.SourceID)
  35. if c.Written() {
  36. return
  37. }
  38. u, err := database.Handle.Users().Create(
  39. c.Req.Context(),
  40. form.Username,
  41. form.Email,
  42. database.CreateUserOptions{
  43. FullName: form.FullName,
  44. Password: form.Password,
  45. LoginSource: form.SourceID,
  46. LoginName: form.LoginName,
  47. Activated: true,
  48. },
  49. )
  50. if err != nil {
  51. if database.IsErrUserAlreadyExist(err) ||
  52. database.IsErrEmailAlreadyUsed(err) ||
  53. database.IsErrNameNotAllowed(err) {
  54. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  55. } else {
  56. c.Error(err, "create user")
  57. }
  58. return
  59. }
  60. log.Trace("Account %q created by admin %q", u.Name, c.User.Name)
  61. // Send email notification.
  62. if form.SendNotify && conf.Email.Enabled {
  63. if err := email.SendRegisterNotifyMail(c.Context.Context, database.NewMailerUser(u)); err != nil {
  64. log.Error("Failed to send register notify mail: %v", err)
  65. }
  66. }
  67. c.JSON(http.StatusCreated, toUser(u))
  68. }
  69. type adminEditUserRequest struct {
  70. SourceID int64 `json:"source_id"`
  71. LoginName string `json:"login_name"`
  72. FullName string `json:"full_name" binding:"MaxSize(100)"`
  73. Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
  74. Password string `json:"password" binding:"MaxSize(255)"`
  75. Website string `json:"website" binding:"MaxSize(50)"`
  76. Location string `json:"location" binding:"MaxSize(50)"`
  77. Active *bool `json:"active"`
  78. Admin *bool `json:"admin"`
  79. AllowGitHook *bool `json:"allow_git_hook"`
  80. AllowImportLocal *bool `json:"allow_import_local"`
  81. MaxRepoCreation *int `json:"max_repo_creation"`
  82. }
  83. func adminEditUser(c *context.APIContext, form adminEditUserRequest) {
  84. u := getUserByParams(c)
  85. if c.Written() {
  86. return
  87. }
  88. parseLoginSource(c, form.SourceID)
  89. if c.Written() {
  90. return
  91. }
  92. opts := database.UpdateUserOptions{
  93. LoginSource: &form.SourceID,
  94. LoginName: &form.LoginName,
  95. FullName: &form.FullName,
  96. Website: &form.Website,
  97. Location: &form.Location,
  98. MaxRepoCreation: form.MaxRepoCreation,
  99. IsActivated: form.Active,
  100. IsAdmin: form.Admin,
  101. AllowGitHook: form.AllowGitHook,
  102. AllowImportLocal: form.AllowImportLocal,
  103. ProhibitLogin: nil, // TODO: Add this option to API
  104. }
  105. if form.Password != "" {
  106. opts.Password = &form.Password
  107. }
  108. if u.Email != form.Email {
  109. opts.Email = &form.Email
  110. }
  111. err := database.Handle.Users().Update(c.Req.Context(), u.ID, opts)
  112. if err != nil {
  113. if database.IsErrEmailAlreadyUsed(err) {
  114. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  115. } else {
  116. c.Error(err, "update user")
  117. }
  118. return
  119. }
  120. log.Trace("Account updated by admin %q: %s", c.User.Name, u.Name)
  121. u, err = database.Handle.Users().GetByID(c.Req.Context(), u.ID)
  122. if err != nil {
  123. c.Error(err, "get user")
  124. return
  125. }
  126. c.JSONSuccess(toUser(u))
  127. }
  128. func adminDeleteUser(c *context.APIContext) {
  129. u := getUserByParams(c)
  130. if c.Written() {
  131. return
  132. }
  133. if err := database.Handle.Users().DeleteByID(c.Req.Context(), u.ID, false); err != nil {
  134. if database.IsErrUserOwnRepos(err) ||
  135. database.IsErrUserHasOrgs(err) {
  136. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  137. } else {
  138. c.Error(err, "delete user")
  139. }
  140. return
  141. }
  142. log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
  143. c.NoContent()
  144. }
  145. func adminCreatePublicKey(c *context.APIContext, form createPublicKeyRequest) {
  146. u := getUserByParams(c)
  147. if c.Written() {
  148. return
  149. }
  150. createUserPublicKey(c, form, u.ID)
  151. }