user.go 3.5 KB

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