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