1
0

admin_user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. email.SendRegisterNotifyMail(c.Context.Context, database.NewMailerUser(u))
  64. }
  65. c.JSON(http.StatusCreated, toUser(u))
  66. }
  67. type adminEditUserRequest struct {
  68. SourceID int64 `json:"source_id"`
  69. LoginName string `json:"login_name"`
  70. FullName string `json:"full_name" binding:"MaxSize(100)"`
  71. Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
  72. Password string `json:"password" binding:"MaxSize(255)"`
  73. Website string `json:"website" binding:"MaxSize(50)"`
  74. Location string `json:"location" binding:"MaxSize(50)"`
  75. Active *bool `json:"active"`
  76. Admin *bool `json:"admin"`
  77. AllowGitHook *bool `json:"allow_git_hook"`
  78. AllowImportLocal *bool `json:"allow_import_local"`
  79. MaxRepoCreation *int `json:"max_repo_creation"`
  80. }
  81. func adminEditUser(c *context.APIContext, form adminEditUserRequest) {
  82. u := getUserByParams(c)
  83. if c.Written() {
  84. return
  85. }
  86. parseLoginSource(c, form.SourceID)
  87. if c.Written() {
  88. return
  89. }
  90. opts := database.UpdateUserOptions{
  91. LoginSource: &form.SourceID,
  92. LoginName: &form.LoginName,
  93. FullName: &form.FullName,
  94. Website: &form.Website,
  95. Location: &form.Location,
  96. MaxRepoCreation: form.MaxRepoCreation,
  97. IsActivated: form.Active,
  98. IsAdmin: form.Admin,
  99. AllowGitHook: form.AllowGitHook,
  100. AllowImportLocal: form.AllowImportLocal,
  101. ProhibitLogin: nil, // TODO: Add this option to API
  102. }
  103. if form.Password != "" {
  104. opts.Password = &form.Password
  105. }
  106. if u.Email != form.Email {
  107. opts.Email = &form.Email
  108. }
  109. err := database.Handle.Users().Update(c.Req.Context(), u.ID, opts)
  110. if err != nil {
  111. if database.IsErrEmailAlreadyUsed(err) {
  112. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  113. } else {
  114. c.Error(err, "update user")
  115. }
  116. return
  117. }
  118. log.Trace("Account updated by admin %q: %s", c.User.Name, u.Name)
  119. u, err = database.Handle.Users().GetByID(c.Req.Context(), u.ID)
  120. if err != nil {
  121. c.Error(err, "get user")
  122. return
  123. }
  124. c.JSONSuccess(toUser(u))
  125. }
  126. func adminDeleteUser(c *context.APIContext) {
  127. u := getUserByParams(c)
  128. if c.Written() {
  129. return
  130. }
  131. if err := database.Handle.Users().DeleteByID(c.Req.Context(), u.ID, false); err != nil {
  132. if database.IsErrUserOwnRepos(err) ||
  133. database.IsErrUserHasOrgs(err) {
  134. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  135. } else {
  136. c.Error(err, "delete user")
  137. }
  138. return
  139. }
  140. log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
  141. c.NoContent()
  142. }
  143. func adminCreatePublicKey(c *context.APIContext, form createPublicKeyRequest) {
  144. u := getUserByParams(c)
  145. if c.Written() {
  146. return
  147. }
  148. createUserPublicKey(c, form, u.ID)
  149. }