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.

151 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. Email: form.Email,
  36. Passwd: form.Password,
  37. IsActive: true,
  38. LoginType: models.LOGIN_PLAIN,
  39. }
  40. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  41. if ctx.Written() {
  42. return
  43. }
  44. if err := models.CreateUser(u); err != nil {
  45. if models.IsErrUserAlreadyExist(err) ||
  46. models.IsErrEmailAlreadyUsed(err) ||
  47. models.IsErrNameReserved(err) ||
  48. models.IsErrNamePatternNotAllowed(err) {
  49. ctx.Error(422, "", err)
  50. } else {
  51. ctx.Error(500, "CreateUser", err)
  52. }
  53. return
  54. }
  55. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  56. // Send email notification.
  57. if form.SendNotify && setting.MailService != nil {
  58. models.SendRegisterNotifyMail(ctx.Context.Context, u)
  59. }
  60. ctx.JSON(201, convert.ToUser(u))
  61. }
  62. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
  63. func EditUser(ctx *context.APIContext, form api.EditUserOption) {
  64. u := user.GetUserByParams(ctx)
  65. if ctx.Written() {
  66. return
  67. }
  68. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  69. if ctx.Written() {
  70. return
  71. }
  72. if len(form.Password) > 0 {
  73. u.Passwd = form.Password
  74. u.Salt = models.GetUserSalt()
  75. u.EncodePasswd()
  76. }
  77. u.LoginName = form.LoginName
  78. u.FullName = form.FullName
  79. u.Email = form.Email
  80. u.Website = form.Website
  81. u.Location = form.Location
  82. if form.Active != nil {
  83. u.IsActive = *form.Active
  84. }
  85. if form.Admin != nil {
  86. u.IsAdmin = *form.Admin
  87. }
  88. if form.AllowGitHook != nil {
  89. u.AllowGitHook = *form.AllowGitHook
  90. }
  91. if form.AllowImportLocal != nil {
  92. u.AllowImportLocal = *form.AllowImportLocal
  93. }
  94. if err := models.UpdateUser(u); err != nil {
  95. if models.IsErrEmailAlreadyUsed(err) {
  96. ctx.Error(422, "", err)
  97. } else {
  98. ctx.Error(500, "UpdateUser", err)
  99. }
  100. return
  101. }
  102. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  103. ctx.JSON(200, convert.ToUser(u))
  104. }
  105. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
  106. func DeleteUser(ctx *context.APIContext) {
  107. u := user.GetUserByParams(ctx)
  108. if ctx.Written() {
  109. return
  110. }
  111. if err := models.DeleteUser(u); err != nil {
  112. if models.IsErrUserOwnRepos(err) ||
  113. models.IsErrUserHasOrgs(err) {
  114. ctx.Error(422, "", err)
  115. } else {
  116. ctx.Error(500, "DeleteUser", err)
  117. }
  118. return
  119. }
  120. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  121. ctx.Status(204)
  122. }
  123. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
  124. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  125. u := user.GetUserByParams(ctx)
  126. if ctx.Written() {
  127. return
  128. }
  129. user.CreateUserPublicKey(ctx, form, u.Id)
  130. }