user.go 3.8 KB

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