user.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/mailer"
  11. "github.com/gogits/gogs/modules/setting"
  12. "github.com/gogits/gogs/routers/api/v1/convert"
  13. "github.com/gogits/gogs/routers/api/v1/user"
  14. )
  15. func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
  16. if sourceID == 0 {
  17. return
  18. }
  19. source, err := models.GetLoginSourceByID(sourceID)
  20. if err != nil {
  21. if models.IsErrAuthenticationNotExist(err) {
  22. ctx.Error(422, "", err)
  23. } else {
  24. ctx.Error(500, "GetLoginSourceByID", err)
  25. }
  26. return
  27. }
  28. u.LoginType = source.Type
  29. u.LoginSource = source.ID
  30. u.LoginName = loginName
  31. }
  32. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user
  33. func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
  34. u := &models.User{
  35. Name: form.Username,
  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 e-mail notification.
  58. if form.SendNotify && setting.MailService != nil {
  59. mailer.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. }