user.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/user"
  12. )
  13. func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
  14. if sourceID == 0 {
  15. return
  16. }
  17. source, err := models.GetLoginSourceByID(sourceID)
  18. if err != nil {
  19. if models.IsErrLoginSourceNotExist(err) {
  20. ctx.Error(422, "", err)
  21. } else {
  22. ctx.Error(500, "GetLoginSourceByID", err)
  23. }
  24. return
  25. }
  26. u.LoginType = source.Type
  27. u.LoginSource = source.ID
  28. u.LoginName = loginName
  29. }
  30. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user
  31. func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
  32. u := &models.User{
  33. Name: form.Username,
  34. FullName: form.FullName,
  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, u.APIFormat())
  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 form.MaxRepoCreation != nil {
  95. u.MaxRepoCreation = *form.MaxRepoCreation
  96. }
  97. if err := models.UpdateUser(u); err != nil {
  98. if models.IsErrEmailAlreadyUsed(err) {
  99. ctx.Error(422, "", err)
  100. } else {
  101. ctx.Error(500, "UpdateUser", err)
  102. }
  103. return
  104. }
  105. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  106. ctx.JSON(200, u.APIFormat())
  107. }
  108. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
  109. func DeleteUser(ctx *context.APIContext) {
  110. u := user.GetUserByParams(ctx)
  111. if ctx.Written() {
  112. return
  113. }
  114. if err := models.DeleteUser(u); err != nil {
  115. if models.IsErrUserOwnRepos(err) ||
  116. models.IsErrUserHasOrgs(err) {
  117. ctx.Error(422, "", err)
  118. } else {
  119. ctx.Error(500, "DeleteUser", err)
  120. }
  121. return
  122. }
  123. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  124. ctx.Status(204)
  125. }
  126. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
  127. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  128. u := user.GetUserByParams(ctx)
  129. if ctx.Written() {
  130. return
  131. }
  132. user.CreateUserPublicKey(ctx, form, u.ID)
  133. }