You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.7 KiB

  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. api "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/context"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/setting"
  11. "github.com/gogits/gogs/routers/api/v1/convert"
  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.IsErrAuthenticationNotExist(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. models.SendRegisterNotifyMail(ctx.Context.Context, u)
  60. }
  61. ctx.JSON(201, convert.ToUser(u))
  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. u.Salt = models.GetUserSalt()
  76. u.EncodePasswd()
  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 err := models.UpdateUser(u); err != nil {
  96. if models.IsErrEmailAlreadyUsed(err) {
  97. ctx.Error(422, "", err)
  98. } else {
  99. ctx.Error(500, "UpdateUser", err)
  100. }
  101. return
  102. }
  103. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  104. ctx.JSON(200, convert.ToUser(u))
  105. }
  106. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
  107. func DeleteUser(ctx *context.APIContext) {
  108. u := user.GetUserByParams(ctx)
  109. if ctx.Written() {
  110. return
  111. }
  112. if err := models.DeleteUser(u); err != nil {
  113. if models.IsErrUserOwnRepos(err) ||
  114. models.IsErrUserHasOrgs(err) {
  115. ctx.Error(422, "", err)
  116. } else {
  117. ctx.Error(500, "DeleteUser", err)
  118. }
  119. return
  120. }
  121. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  122. ctx.Status(204)
  123. }
  124. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
  125. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  126. u := user.GetUserByParams(ctx)
  127. if ctx.Written() {
  128. return
  129. }
  130. user.CreateUserPublicKey(ctx, form, u.ID)
  131. }