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.

158 lines
3.9 KiB

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