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 7.2 kB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 form.AllowCreateOrganization != nil {
  144. u.AllowCreateOrganization = *form.AllowCreateOrganization
  145. }
  146. if form.ProhibitLogin != nil {
  147. u.ProhibitLogin = *form.ProhibitLogin
  148. }
  149. if err := models.UpdateUser(u); err != nil {
  150. if models.IsErrEmailAlreadyUsed(err) {
  151. ctx.Error(422, "", err)
  152. } else {
  153. ctx.Error(500, "UpdateUser", err)
  154. }
  155. return
  156. }
  157. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  158. ctx.JSON(200, u.APIFormat())
  159. }
  160. // DeleteUser api for deleting a user
  161. func DeleteUser(ctx *context.APIContext) {
  162. // swagger:operation DELETE /admin/users/{username} admin adminDeleteUser
  163. // ---
  164. // summary: Delete a user
  165. // produces:
  166. // - application/json
  167. // parameters:
  168. // - name: username
  169. // in: path
  170. // description: username of user to delete
  171. // type: string
  172. // required: true
  173. // responses:
  174. // "204":
  175. // "$ref": "#/responses/empty"
  176. // "403":
  177. // "$ref": "#/responses/forbidden"
  178. // "422":
  179. // "$ref": "#/responses/validationError"
  180. u := user.GetUserByParams(ctx)
  181. if ctx.Written() {
  182. return
  183. }
  184. if err := models.DeleteUser(u); err != nil {
  185. if models.IsErrUserOwnRepos(err) ||
  186. models.IsErrUserHasOrgs(err) {
  187. ctx.Error(422, "", err)
  188. } else {
  189. ctx.Error(500, "DeleteUser", err)
  190. }
  191. return
  192. }
  193. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  194. ctx.Status(204)
  195. }
  196. // CreatePublicKey api for creating a public key to a user
  197. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  198. // swagger:operation POST /admin/users/{username}/keys admin adminCreatePublicKey
  199. // ---
  200. // summary: Add a public key on behalf of a user
  201. // consumes:
  202. // - application/json
  203. // produces:
  204. // - application/json
  205. // parameters:
  206. // - name: username
  207. // in: path
  208. // description: username of the user
  209. // type: string
  210. // required: true
  211. // - name: key
  212. // in: body
  213. // schema:
  214. // "$ref": "#/definitions/CreateKeyOption"
  215. // responses:
  216. // "201":
  217. // "$ref": "#/responses/PublicKey"
  218. // "403":
  219. // "$ref": "#/responses/forbidden"
  220. // "422":
  221. // "$ref": "#/responses/validationError"
  222. u := user.GetUserByParams(ctx)
  223. if ctx.Written() {
  224. return
  225. }
  226. user.CreateUserPublicKey(ctx, form, u.ID)
  227. }
  228. // DeleteUserPublicKey api for deleting a user's public key
  229. func DeleteUserPublicKey(ctx *context.APIContext) {
  230. // swagger:operation DELETE /admin/users/{username}/keys/{id} admin adminDeleteUserPublicKey
  231. // ---
  232. // summary: Delete a user's public key
  233. // produces:
  234. // - application/json
  235. // parameters:
  236. // - name: username
  237. // in: path
  238. // description: username of user
  239. // type: string
  240. // required: true
  241. // - name: id
  242. // in: path
  243. // description: id of the key to delete
  244. // type: integer
  245. // format: int64
  246. // required: true
  247. // responses:
  248. // "204":
  249. // "$ref": "#/responses/empty"
  250. // "403":
  251. // "$ref": "#/responses/forbidden"
  252. // "404":
  253. // "$ref": "#/responses/notFound"
  254. u := user.GetUserByParams(ctx)
  255. if ctx.Written() {
  256. return
  257. }
  258. if err := models.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
  259. if models.IsErrKeyNotExist(err) {
  260. ctx.Status(404)
  261. } else if models.IsErrKeyAccessDenied(err) {
  262. ctx.Error(403, "", "You do not have access to this key")
  263. } else {
  264. ctx.Error(500, "DeleteUserPublicKey", err)
  265. }
  266. return
  267. }
  268. log.Trace("Key deleted by admin(%s): %s", ctx.User.Name, u.Name)
  269. ctx.Status(204)
  270. }