user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. var err error
  75. if u.Salt, err = models.GetUserSalt(); err != nil {
  76. ctx.Error(500, "UpdateUser", err)
  77. return
  78. }
  79. u.EncodePasswd()
  80. }
  81. u.LoginName = form.LoginName
  82. u.FullName = form.FullName
  83. u.Email = form.Email
  84. u.Website = form.Website
  85. u.Location = form.Location
  86. if form.Active != nil {
  87. u.IsActive = *form.Active
  88. }
  89. if form.Admin != nil {
  90. u.IsAdmin = *form.Admin
  91. }
  92. if form.AllowGitHook != nil {
  93. u.AllowGitHook = *form.AllowGitHook
  94. }
  95. if form.AllowImportLocal != nil {
  96. u.AllowImportLocal = *form.AllowImportLocal
  97. }
  98. if form.MaxRepoCreation != nil {
  99. u.MaxRepoCreation = *form.MaxRepoCreation
  100. }
  101. if err := models.UpdateUser(u); err != nil {
  102. if models.IsErrEmailAlreadyUsed(err) {
  103. ctx.Error(422, "", err)
  104. } else {
  105. ctx.Error(500, "UpdateUser", err)
  106. }
  107. return
  108. }
  109. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  110. ctx.JSON(200, u.APIFormat())
  111. }
  112. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
  113. func DeleteUser(ctx *context.APIContext) {
  114. u := user.GetUserByParams(ctx)
  115. if ctx.Written() {
  116. return
  117. }
  118. if err := models.DeleteUser(u); err != nil {
  119. if models.IsErrUserOwnRepos(err) ||
  120. models.IsErrUserHasOrgs(err) {
  121. ctx.Error(422, "", err)
  122. } else {
  123. ctx.Error(500, "DeleteUser", err)
  124. }
  125. return
  126. }
  127. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  128. ctx.Status(204)
  129. }
  130. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
  131. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  132. u := user.GetUserByParams(ctx)
  133. if ctx.Written() {
  134. return
  135. }
  136. user.CreateUserPublicKey(ctx, form, u.ID)
  137. }