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