user.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/db"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/route/api/v1/user"
  14. )
  15. func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginName string) {
  16. if sourceID == 0 {
  17. return
  18. }
  19. source, err := db.LoginSources.GetByID(sourceID)
  20. if err != nil {
  21. if db.IsErrLoginSourceNotExist(err) {
  22. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  23. } else {
  24. c.Error(err, "get login source by ID")
  25. }
  26. return
  27. }
  28. u.LoginSource = source.ID
  29. u.LoginName = loginName
  30. }
  31. func CreateUser(c *context.APIContext, form api.CreateUserOption) {
  32. u := &db.User{
  33. Name: form.Username,
  34. FullName: form.FullName,
  35. Email: form.Email,
  36. Passwd: form.Password,
  37. IsActive: true,
  38. }
  39. parseLoginSource(c, u, form.SourceID, form.LoginName)
  40. if c.Written() {
  41. return
  42. }
  43. if err := db.CreateUser(u); err != nil {
  44. if db.IsErrUserAlreadyExist(err) ||
  45. db.IsErrEmailAlreadyUsed(err) ||
  46. db.IsErrNameNotAllowed(err) {
  47. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  48. } else {
  49. c.Error(err, "create user")
  50. }
  51. return
  52. }
  53. log.Trace("Account created by admin %q: %s", c.User.Name, u.Name)
  54. // Send email notification.
  55. if form.SendNotify && conf.Email.Enabled {
  56. email.SendRegisterNotifyMail(c.Context.Context, db.NewMailerUser(u))
  57. }
  58. c.JSON(http.StatusCreated, u.APIFormat())
  59. }
  60. func EditUser(c *context.APIContext, form api.EditUserOption) {
  61. u := user.GetUserByParams(c)
  62. if c.Written() {
  63. return
  64. }
  65. parseLoginSource(c, u, form.SourceID, form.LoginName)
  66. if c.Written() {
  67. return
  68. }
  69. if len(form.Password) > 0 {
  70. u.Passwd = form.Password
  71. var err error
  72. if u.Salt, err = db.GetUserSalt(); err != nil {
  73. c.Error(err, "get user salt")
  74. return
  75. }
  76. u.EncodePassword()
  77. }
  78. u.LoginName = form.LoginName
  79. u.FullName = form.FullName
  80. u.Email = form.Email
  81. u.Website = form.Website
  82. u.Location = form.Location
  83. if form.Active != nil {
  84. u.IsActive = *form.Active
  85. }
  86. if form.Admin != nil {
  87. u.IsAdmin = *form.Admin
  88. }
  89. if form.AllowGitHook != nil {
  90. u.AllowGitHook = *form.AllowGitHook
  91. }
  92. if form.AllowImportLocal != nil {
  93. u.AllowImportLocal = *form.AllowImportLocal
  94. }
  95. if form.MaxRepoCreation != nil {
  96. u.MaxRepoCreation = *form.MaxRepoCreation
  97. }
  98. if err := db.UpdateUser(u); err != nil {
  99. if db.IsErrEmailAlreadyUsed(err) {
  100. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  101. } else {
  102. c.Error(err, "update user")
  103. }
  104. return
  105. }
  106. log.Trace("Account profile updated by admin %q: %s", c.User.Name, u.Name)
  107. c.JSONSuccess(u.APIFormat())
  108. }
  109. func DeleteUser(c *context.APIContext) {
  110. u := user.GetUserByParams(c)
  111. if c.Written() {
  112. return
  113. }
  114. if err := db.DeleteUser(u); err != nil {
  115. if db.IsErrUserOwnRepos(err) ||
  116. db.IsErrUserHasOrgs(err) {
  117. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  118. } else {
  119. c.Error(err, "delete user")
  120. }
  121. return
  122. }
  123. log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
  124. c.NoContent()
  125. }
  126. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  127. u := user.GetUserByParams(c)
  128. if c.Written() {
  129. return
  130. }
  131. user.CreateUserPublicKey(c, form, u.ID)
  132. }