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

user.go 4.1 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/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. // CreateUser api for creating a user
  31. // see 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.LoginPlain,
  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, u.APIFormat())
  62. }
  63. // EditUser api for modifying a user's information
  64. // see https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
  65. func EditUser(ctx *context.APIContext, form api.EditUserOption) {
  66. u := user.GetUserByParams(ctx)
  67. if ctx.Written() {
  68. return
  69. }
  70. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  71. if ctx.Written() {
  72. return
  73. }
  74. if len(form.Password) > 0 {
  75. u.Passwd = form.Password
  76. var err error
  77. if u.Salt, err = models.GetUserSalt(); err != nil {
  78. ctx.Error(500, "UpdateUser", err)
  79. return
  80. }
  81. u.EncodePasswd()
  82. }
  83. u.LoginName = form.LoginName
  84. u.FullName = form.FullName
  85. u.Email = form.Email
  86. u.Website = form.Website
  87. u.Location = form.Location
  88. if form.Active != nil {
  89. u.IsActive = *form.Active
  90. }
  91. if form.Admin != nil {
  92. u.IsAdmin = *form.Admin
  93. }
  94. if form.AllowGitHook != nil {
  95. u.AllowGitHook = *form.AllowGitHook
  96. }
  97. if form.AllowImportLocal != nil {
  98. u.AllowImportLocal = *form.AllowImportLocal
  99. }
  100. if form.MaxRepoCreation != nil {
  101. u.MaxRepoCreation = *form.MaxRepoCreation
  102. }
  103. if err := models.UpdateUser(u); err != nil {
  104. if models.IsErrEmailAlreadyUsed(err) {
  105. ctx.Error(422, "", err)
  106. } else {
  107. ctx.Error(500, "UpdateUser", err)
  108. }
  109. return
  110. }
  111. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  112. ctx.JSON(200, u.APIFormat())
  113. }
  114. // DeleteUser api for deleting a user
  115. // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
  116. func DeleteUser(ctx *context.APIContext) {
  117. u := user.GetUserByParams(ctx)
  118. if ctx.Written() {
  119. return
  120. }
  121. if err := models.DeleteUser(u); err != nil {
  122. if models.IsErrUserOwnRepos(err) ||
  123. models.IsErrUserHasOrgs(err) {
  124. ctx.Error(422, "", err)
  125. } else {
  126. ctx.Error(500, "DeleteUser", err)
  127. }
  128. return
  129. }
  130. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  131. ctx.Status(204)
  132. }
  133. // CreatePublicKey api for creating a public key to a user
  134. // see https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
  135. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  136. u := user.GetUserByParams(ctx)
  137. if ctx.Written() {
  138. return
  139. }
  140. user.CreateUserPublicKey(ctx, form, u.ID)
  141. }