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.

282 lines
6.7 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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/routers/api/v1/user"
  11. api "code.gitea.io/sdk/gitea"
  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 create a user
  31. func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
  32. // swagger:operation POST /admin/users admin adminCreateUser
  33. // ---
  34. // summary: Create a user
  35. // consumes:
  36. // - application/json
  37. // produces:
  38. // - application/json
  39. // parameters:
  40. // - name: body
  41. // in: body
  42. // schema:
  43. // "$ref": "#/definitions/CreateUserOption"
  44. // responses:
  45. // "201":
  46. // "$ref": "#/responses/User"
  47. // "403":
  48. // "$ref": "#/responses/forbidden"
  49. // "422":
  50. // "$ref": "#/responses/validationError"
  51. u := &models.User{
  52. Name: form.Username,
  53. FullName: form.FullName,
  54. Email: form.Email,
  55. Passwd: form.Password,
  56. IsActive: true,
  57. LoginType: models.LoginPlain,
  58. }
  59. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  60. if ctx.Written() {
  61. return
  62. }
  63. if err := models.CreateUser(u); err != nil {
  64. if models.IsErrUserAlreadyExist(err) ||
  65. models.IsErrEmailAlreadyUsed(err) ||
  66. models.IsErrNameReserved(err) ||
  67. models.IsErrNamePatternNotAllowed(err) {
  68. ctx.Error(422, "", err)
  69. } else {
  70. ctx.Error(500, "CreateUser", err)
  71. }
  72. return
  73. }
  74. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  75. // Send email notification.
  76. if form.SendNotify && setting.MailService != nil {
  77. models.SendRegisterNotifyMail(ctx.Context.Context, u)
  78. }
  79. ctx.JSON(201, u.APIFormat())
  80. }
  81. // EditUser api for modifying a user's information
  82. func EditUser(ctx *context.APIContext, form api.EditUserOption) {
  83. // swagger:operation PATCH /admin/users/{username} admin adminEditUser
  84. // ---
  85. // summary: Edit an existing user
  86. // consumes:
  87. // - application/json
  88. // produces:
  89. // - application/json
  90. // parameters:
  91. // - name: username
  92. // in: path
  93. // description: username of user to edit
  94. // type: string
  95. // required: true
  96. // - name: body
  97. // in: body
  98. // schema:
  99. // "$ref": "#/definitions/EditUserOption"
  100. // responses:
  101. // "200":
  102. // "$ref": "#/responses/User"
  103. // "403":
  104. // "$ref": "#/responses/forbidden"
  105. // "422":
  106. // "$ref": "#/responses/validationError"
  107. u := user.GetUserByParams(ctx)
  108. if ctx.Written() {
  109. return
  110. }
  111. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  112. if ctx.Written() {
  113. return
  114. }
  115. if len(form.Password) > 0 {
  116. var err error
  117. if u.Salt, err = models.GetUserSalt(); err != nil {
  118. ctx.Error(500, "UpdateUser", err)
  119. return
  120. }
  121. u.HashPassword(form.Password)
  122. }
  123. u.LoginName = form.LoginName
  124. u.FullName = form.FullName
  125. u.Email = form.Email
  126. u.Website = form.Website
  127. u.Location = form.Location
  128. if form.Active != nil {
  129. u.IsActive = *form.Active
  130. }
  131. if form.Admin != nil {
  132. u.IsAdmin = *form.Admin
  133. }
  134. if form.AllowGitHook != nil {
  135. u.AllowGitHook = *form.AllowGitHook
  136. }
  137. if form.AllowImportLocal != nil {
  138. u.AllowImportLocal = *form.AllowImportLocal
  139. }
  140. if form.MaxRepoCreation != nil {
  141. u.MaxRepoCreation = *form.MaxRepoCreation
  142. }
  143. if err := models.UpdateUser(u); err != nil {
  144. if models.IsErrEmailAlreadyUsed(err) {
  145. ctx.Error(422, "", err)
  146. } else {
  147. ctx.Error(500, "UpdateUser", err)
  148. }
  149. return
  150. }
  151. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  152. ctx.JSON(200, u.APIFormat())
  153. }
  154. // DeleteUser api for deleting a user
  155. func DeleteUser(ctx *context.APIContext) {
  156. // swagger:operation DELETE /admin/users/{username} admin adminDeleteUser
  157. // ---
  158. // summary: Delete a user
  159. // produces:
  160. // - application/json
  161. // parameters:
  162. // - name: username
  163. // in: path
  164. // description: username of user to delete
  165. // type: string
  166. // required: true
  167. // responses:
  168. // "204":
  169. // "$ref": "#/responses/empty"
  170. // "403":
  171. // "$ref": "#/responses/forbidden"
  172. // "422":
  173. // "$ref": "#/responses/validationError"
  174. u := user.GetUserByParams(ctx)
  175. if ctx.Written() {
  176. return
  177. }
  178. if err := models.DeleteUser(u); err != nil {
  179. if models.IsErrUserOwnRepos(err) ||
  180. models.IsErrUserHasOrgs(err) {
  181. ctx.Error(422, "", err)
  182. } else {
  183. ctx.Error(500, "DeleteUser", err)
  184. }
  185. return
  186. }
  187. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  188. ctx.Status(204)
  189. }
  190. // CreatePublicKey api for creating a public key to a user
  191. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  192. // swagger:operation POST /admin/users/{username}/keys admin adminCreatePublicKey
  193. // ---
  194. // summary: Add a public key on behalf of a user
  195. // consumes:
  196. // - application/json
  197. // produces:
  198. // - application/json
  199. // parameters:
  200. // - name: username
  201. // in: path
  202. // description: username of the user
  203. // type: string
  204. // required: true
  205. // responses:
  206. // "201":
  207. // "$ref": "#/responses/PublicKey"
  208. // "403":
  209. // "$ref": "#/responses/forbidden"
  210. // "422":
  211. // "$ref": "#/responses/validationError"
  212. u := user.GetUserByParams(ctx)
  213. if ctx.Written() {
  214. return
  215. }
  216. user.CreateUserPublicKey(ctx, form, u.ID)
  217. }
  218. // DeleteUserPublicKey api for deleting a user's public key
  219. func DeleteUserPublicKey(ctx *context.APIContext) {
  220. // swagger:operation DELETE /admin/users/{username}/keys/{id} admin adminDeleteUserPublicKey
  221. // ---
  222. // summary: Delete a user's public key
  223. // produces:
  224. // - application/json
  225. // parameters:
  226. // - name: username
  227. // in: path
  228. // description: username of user
  229. // type: string
  230. // required: true
  231. // - name: id
  232. // in: path
  233. // description: id of the key to delete
  234. // type: integer
  235. // required: true
  236. // responses:
  237. // "204":
  238. // "$ref": "#/responses/empty"
  239. // "403":
  240. // "$ref": "#/responses/forbidden"
  241. // "404":
  242. // "$ref": "#/responses/notFound"
  243. u := user.GetUserByParams(ctx)
  244. if ctx.Written() {
  245. return
  246. }
  247. if err := models.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
  248. if models.IsErrKeyNotExist(err) {
  249. ctx.Status(404)
  250. } else if models.IsErrKeyAccessDenied(err) {
  251. ctx.Error(403, "", "You do not have access to this key")
  252. } else {
  253. ctx.Error(500, "DeleteUserPublicKey", err)
  254. }
  255. return
  256. }
  257. log.Trace("Key deleted by admin(%s): %s", ctx.User.Name, u.Name)
  258. ctx.Status(204)
  259. }