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.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
  32. // swagger:route POST /admin/users admin adminCreateUser
  33. //
  34. // Consumes:
  35. // - application/json
  36. //
  37. // Produces:
  38. // - application/json
  39. //
  40. // Responses:
  41. // 201: User
  42. // 403: forbidden
  43. // 422: validationError
  44. // 500: error
  45. u := &models.User{
  46. Name: form.Username,
  47. FullName: form.FullName,
  48. Email: form.Email,
  49. Passwd: form.Password,
  50. IsActive: true,
  51. LoginType: models.LoginPlain,
  52. }
  53. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  54. if ctx.Written() {
  55. return
  56. }
  57. if err := models.CreateUser(u); err != nil {
  58. if models.IsErrUserAlreadyExist(err) ||
  59. models.IsErrEmailAlreadyUsed(err) ||
  60. models.IsErrNameReserved(err) ||
  61. models.IsErrNamePatternNotAllowed(err) {
  62. ctx.Error(422, "", err)
  63. } else {
  64. ctx.Error(500, "CreateUser", err)
  65. }
  66. return
  67. }
  68. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  69. // Send email notification.
  70. if form.SendNotify && setting.MailService != nil {
  71. models.SendRegisterNotifyMail(ctx.Context.Context, u)
  72. }
  73. ctx.JSON(201, u.APIFormat())
  74. }
  75. // EditUser api for modifying a user's information
  76. func EditUser(ctx *context.APIContext, form api.EditUserOption) {
  77. // swagger:route PATCH /admin/users/{username} admin adminEditUser
  78. //
  79. // Consumes:
  80. // - application/json
  81. //
  82. // Produces:
  83. // - application/json
  84. //
  85. // Responses:
  86. // 200: User
  87. // 403: forbidden
  88. // 422: validationError
  89. // 500: error
  90. u := user.GetUserByParams(ctx)
  91. if ctx.Written() {
  92. return
  93. }
  94. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  95. if ctx.Written() {
  96. return
  97. }
  98. if len(form.Password) > 0 {
  99. u.Passwd = form.Password
  100. var err error
  101. if u.Salt, err = models.GetUserSalt(); err != nil {
  102. ctx.Error(500, "UpdateUser", err)
  103. return
  104. }
  105. u.EncodePasswd()
  106. }
  107. u.LoginName = form.LoginName
  108. u.FullName = form.FullName
  109. u.Email = form.Email
  110. u.Website = form.Website
  111. u.Location = form.Location
  112. if form.Active != nil {
  113. u.IsActive = *form.Active
  114. }
  115. if form.Admin != nil {
  116. u.IsAdmin = *form.Admin
  117. }
  118. if form.AllowGitHook != nil {
  119. u.AllowGitHook = *form.AllowGitHook
  120. }
  121. if form.AllowImportLocal != nil {
  122. u.AllowImportLocal = *form.AllowImportLocal
  123. }
  124. if form.MaxRepoCreation != nil {
  125. u.MaxRepoCreation = *form.MaxRepoCreation
  126. }
  127. if err := models.UpdateUser(u); err != nil {
  128. if models.IsErrEmailAlreadyUsed(err) {
  129. ctx.Error(422, "", err)
  130. } else {
  131. ctx.Error(500, "UpdateUser", err)
  132. }
  133. return
  134. }
  135. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  136. ctx.JSON(200, u.APIFormat())
  137. }
  138. // DeleteUser api for deleting a user
  139. func DeleteUser(ctx *context.APIContext) {
  140. // swagger:route DELETE /admin/users/{username} admin adminDeleteUser
  141. //
  142. // Produces:
  143. // - application/json
  144. //
  145. // Responses:
  146. // 204: empty
  147. // 403: forbidden
  148. // 422: validationError
  149. // 500: error
  150. u := user.GetUserByParams(ctx)
  151. if ctx.Written() {
  152. return
  153. }
  154. if err := models.DeleteUser(u); err != nil {
  155. if models.IsErrUserOwnRepos(err) ||
  156. models.IsErrUserHasOrgs(err) {
  157. ctx.Error(422, "", err)
  158. } else {
  159. ctx.Error(500, "DeleteUser", err)
  160. }
  161. return
  162. }
  163. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  164. ctx.Status(204)
  165. }
  166. // CreatePublicKey api for creating a public key to a user
  167. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  168. // swagger:route POST /admin/users/{username}/keys admin adminCreatePublicKey
  169. //
  170. // Consumes:
  171. // - application/json
  172. //
  173. // Produces:
  174. // - application/json
  175. //
  176. // Responses:
  177. // 201: PublicKey
  178. // 403: forbidden
  179. // 422: validationError
  180. // 500: error
  181. u := user.GetUserByParams(ctx)
  182. if ctx.Written() {
  183. return
  184. }
  185. user.CreateUserPublicKey(ctx, form, u.ID)
  186. }