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.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package admin
  6. import (
  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. api "code.gitea.io/sdk/gitea"
  13. )
  14. func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
  15. if sourceID == 0 {
  16. return
  17. }
  18. source, err := models.GetLoginSourceByID(sourceID)
  19. if err != nil {
  20. if models.IsErrLoginSourceNotExist(err) {
  21. ctx.Error(422, "", err)
  22. } else {
  23. ctx.Error(500, "GetLoginSourceByID", err)
  24. }
  25. return
  26. }
  27. u.LoginType = source.Type
  28. u.LoginSource = source.ID
  29. u.LoginName = loginName
  30. }
  31. // CreateUser create a user
  32. func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
  33. // swagger:operation POST /admin/users admin adminCreateUser
  34. // ---
  35. // summary: Create a user
  36. // consumes:
  37. // - application/json
  38. // produces:
  39. // - application/json
  40. // parameters:
  41. // - name: body
  42. // in: body
  43. // schema:
  44. // "$ref": "#/definitions/CreateUserOption"
  45. // responses:
  46. // "201":
  47. // "$ref": "#/responses/User"
  48. // "403":
  49. // "$ref": "#/responses/forbidden"
  50. // "422":
  51. // "$ref": "#/responses/validationError"
  52. u := &models.User{
  53. Name: form.Username,
  54. FullName: form.FullName,
  55. Email: form.Email,
  56. Passwd: form.Password,
  57. IsActive: true,
  58. LoginType: models.LoginPlain,
  59. }
  60. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  61. if ctx.Written() {
  62. return
  63. }
  64. if err := models.CreateUser(u); err != nil {
  65. if models.IsErrUserAlreadyExist(err) ||
  66. models.IsErrEmailAlreadyUsed(err) ||
  67. models.IsErrNameReserved(err) ||
  68. models.IsErrNamePatternNotAllowed(err) {
  69. ctx.Error(422, "", err)
  70. } else {
  71. ctx.Error(500, "CreateUser", err)
  72. }
  73. return
  74. }
  75. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  76. // Send email notification.
  77. if form.SendNotify && setting.MailService != nil {
  78. models.SendRegisterNotifyMail(ctx.Context.Context, u)
  79. }
  80. ctx.JSON(201, u.APIFormat())
  81. }
  82. // EditUser api for modifying a user's information
  83. func EditUser(ctx *context.APIContext, form api.EditUserOption) {
  84. // swagger:operation PATCH /admin/users/{username} admin adminEditUser
  85. // ---
  86. // summary: Edit an existing user
  87. // consumes:
  88. // - application/json
  89. // produces:
  90. // - application/json
  91. // parameters:
  92. // - name: username
  93. // in: path
  94. // description: username of user to edit
  95. // type: string
  96. // required: true
  97. // - name: body
  98. // in: body
  99. // schema:
  100. // "$ref": "#/definitions/EditUserOption"
  101. // responses:
  102. // "200":
  103. // "$ref": "#/responses/User"
  104. // "403":
  105. // "$ref": "#/responses/forbidden"
  106. // "422":
  107. // "$ref": "#/responses/validationError"
  108. u := user.GetUserByParams(ctx)
  109. if ctx.Written() {
  110. return
  111. }
  112. parseLoginSource(ctx, u, form.SourceID, form.LoginName)
  113. if ctx.Written() {
  114. return
  115. }
  116. if len(form.Password) > 0 {
  117. var err error
  118. if u.Salt, err = models.GetUserSalt(); err != nil {
  119. ctx.Error(500, "UpdateUser", err)
  120. return
  121. }
  122. u.HashPassword(form.Password)
  123. }
  124. u.LoginName = form.LoginName
  125. u.FullName = form.FullName
  126. u.Email = form.Email
  127. u.Website = form.Website
  128. u.Location = form.Location
  129. if form.Active != nil {
  130. u.IsActive = *form.Active
  131. }
  132. if form.Admin != nil {
  133. u.IsAdmin = *form.Admin
  134. }
  135. if form.AllowGitHook != nil {
  136. u.AllowGitHook = *form.AllowGitHook
  137. }
  138. if form.AllowImportLocal != nil {
  139. u.AllowImportLocal = *form.AllowImportLocal
  140. }
  141. if form.MaxRepoCreation != nil {
  142. u.MaxRepoCreation = *form.MaxRepoCreation
  143. }
  144. if form.AllowCreateOrganization != nil {
  145. u.AllowCreateOrganization = *form.AllowCreateOrganization
  146. }
  147. if form.ProhibitLogin != nil {
  148. u.ProhibitLogin = *form.ProhibitLogin
  149. }
  150. if err := models.UpdateUser(u); err != nil {
  151. if models.IsErrEmailAlreadyUsed(err) {
  152. ctx.Error(422, "", err)
  153. } else {
  154. ctx.Error(500, "UpdateUser", err)
  155. }
  156. return
  157. }
  158. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  159. ctx.JSON(200, u.APIFormat())
  160. }
  161. // DeleteUser api for deleting a user
  162. func DeleteUser(ctx *context.APIContext) {
  163. // swagger:operation DELETE /admin/users/{username} admin adminDeleteUser
  164. // ---
  165. // summary: Delete a user
  166. // produces:
  167. // - application/json
  168. // parameters:
  169. // - name: username
  170. // in: path
  171. // description: username of user to delete
  172. // type: string
  173. // required: true
  174. // responses:
  175. // "204":
  176. // "$ref": "#/responses/empty"
  177. // "403":
  178. // "$ref": "#/responses/forbidden"
  179. // "422":
  180. // "$ref": "#/responses/validationError"
  181. u := user.GetUserByParams(ctx)
  182. if ctx.Written() {
  183. return
  184. }
  185. if err := models.DeleteUser(u); err != nil {
  186. if models.IsErrUserOwnRepos(err) ||
  187. models.IsErrUserHasOrgs(err) {
  188. ctx.Error(422, "", err)
  189. } else {
  190. ctx.Error(500, "DeleteUser", err)
  191. }
  192. return
  193. }
  194. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  195. ctx.Status(204)
  196. }
  197. // CreatePublicKey api for creating a public key to a user
  198. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  199. // swagger:operation POST /admin/users/{username}/keys admin adminCreatePublicKey
  200. // ---
  201. // summary: Add a public key on behalf of a user
  202. // consumes:
  203. // - application/json
  204. // produces:
  205. // - application/json
  206. // parameters:
  207. // - name: username
  208. // in: path
  209. // description: username of the user
  210. // type: string
  211. // required: true
  212. // - name: key
  213. // in: body
  214. // schema:
  215. // "$ref": "#/definitions/CreateKeyOption"
  216. // responses:
  217. // "201":
  218. // "$ref": "#/responses/PublicKey"
  219. // "403":
  220. // "$ref": "#/responses/forbidden"
  221. // "422":
  222. // "$ref": "#/responses/validationError"
  223. u := user.GetUserByParams(ctx)
  224. if ctx.Written() {
  225. return
  226. }
  227. user.CreateUserPublicKey(ctx, form, u.ID)
  228. }
  229. // DeleteUserPublicKey api for deleting a user's public key
  230. func DeleteUserPublicKey(ctx *context.APIContext) {
  231. // swagger:operation DELETE /admin/users/{username}/keys/{id} admin adminDeleteUserPublicKey
  232. // ---
  233. // summary: Delete a user's public key
  234. // produces:
  235. // - application/json
  236. // parameters:
  237. // - name: username
  238. // in: path
  239. // description: username of user
  240. // type: string
  241. // required: true
  242. // - name: id
  243. // in: path
  244. // description: id of the key to delete
  245. // type: integer
  246. // format: int64
  247. // required: true
  248. // responses:
  249. // "204":
  250. // "$ref": "#/responses/empty"
  251. // "403":
  252. // "$ref": "#/responses/forbidden"
  253. // "404":
  254. // "$ref": "#/responses/notFound"
  255. u := user.GetUserByParams(ctx)
  256. if ctx.Written() {
  257. return
  258. }
  259. if err := models.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
  260. if models.IsErrKeyNotExist(err) {
  261. ctx.Status(404)
  262. } else if models.IsErrKeyAccessDenied(err) {
  263. ctx.Error(403, "", "You do not have access to this key")
  264. } else {
  265. ctx.Error(500, "DeleteUserPublicKey", err)
  266. }
  267. return
  268. }
  269. log.Trace("Key deleted by admin(%s): %s", ctx.User.Name, u.Name)
  270. ctx.Status(204)
  271. }
  272. //GetAllUsers API for getting information of all the users
  273. func GetAllUsers(ctx *context.APIContext) {
  274. // swagger:operation GET /admin/users admin adminGetAllUsers
  275. // ---
  276. // summary: List all users
  277. // produces:
  278. // - application/json
  279. // responses:
  280. // "200":
  281. // "$ref": "#/responses/UserList"
  282. // "403":
  283. // "$ref": "#/responses/forbidden"
  284. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  285. Type: models.UserTypeIndividual,
  286. OrderBy: models.SearchOrderByAlphabetically,
  287. PageSize: -1,
  288. })
  289. if err != nil {
  290. ctx.Error(500, "SearchUsers", err)
  291. return
  292. }
  293. ctx.JSON(200, &users)
  294. }