user.go 3.9 KB

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