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.

gpg_key.go 5.3 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2017 The Gitea 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 user
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/routers/api/v1/utils"
  12. )
  13. func listGPGKeys(ctx *context.APIContext, uid int64, listOptions models.ListOptions) {
  14. keys, err := models.ListGPGKeys(uid, listOptions)
  15. if err != nil {
  16. ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
  17. return
  18. }
  19. apiKeys := make([]*api.GPGKey, len(keys))
  20. for i := range keys {
  21. apiKeys[i] = convert.ToGPGKey(keys[i])
  22. }
  23. ctx.JSON(http.StatusOK, &apiKeys)
  24. }
  25. //ListGPGKeys get the GPG key list of a user
  26. func ListGPGKeys(ctx *context.APIContext) {
  27. // swagger:operation GET /users/{username}/gpg_keys user userListGPGKeys
  28. // ---
  29. // summary: List the given user's GPG keys
  30. // produces:
  31. // - application/json
  32. // parameters:
  33. // - name: username
  34. // in: path
  35. // description: username of user
  36. // type: string
  37. // required: true
  38. // - name: page
  39. // in: query
  40. // description: page number of results to return (1-based)
  41. // type: integer
  42. // - name: limit
  43. // in: query
  44. // description: page size of results, maximum page size is 50
  45. // type: integer
  46. // responses:
  47. // "200":
  48. // "$ref": "#/responses/GPGKeyList"
  49. user := GetUserByParams(ctx)
  50. if ctx.Written() {
  51. return
  52. }
  53. listGPGKeys(ctx, user.ID, utils.GetListOptions(ctx))
  54. }
  55. //ListMyGPGKeys get the GPG key list of the authenticated user
  56. func ListMyGPGKeys(ctx *context.APIContext) {
  57. // swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
  58. // ---
  59. // summary: List the authenticated user's GPG keys
  60. // parameters:
  61. // - name: page
  62. // in: query
  63. // description: page number of results to return (1-based)
  64. // type: integer
  65. // - name: limit
  66. // in: query
  67. // description: page size of results, maximum page size is 50
  68. // type: integer
  69. // produces:
  70. // - application/json
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/GPGKeyList"
  74. listGPGKeys(ctx, ctx.User.ID, utils.GetListOptions(ctx))
  75. }
  76. //GetGPGKey get the GPG key based on a id
  77. func GetGPGKey(ctx *context.APIContext) {
  78. // swagger:operation GET /user/gpg_keys/{id} user userCurrentGetGPGKey
  79. // ---
  80. // summary: Get a GPG key
  81. // produces:
  82. // - application/json
  83. // parameters:
  84. // - name: id
  85. // in: path
  86. // description: id of key to get
  87. // type: integer
  88. // format: int64
  89. // required: true
  90. // responses:
  91. // "200":
  92. // "$ref": "#/responses/GPGKey"
  93. // "404":
  94. // "$ref": "#/responses/notFound"
  95. key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
  96. if err != nil {
  97. if models.IsErrGPGKeyNotExist(err) {
  98. ctx.NotFound()
  99. } else {
  100. ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
  101. }
  102. return
  103. }
  104. ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
  105. }
  106. // CreateUserGPGKey creates new GPG key to given user by ID.
  107. func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
  108. key, err := models.AddGPGKey(uid, form.ArmoredKey)
  109. if err != nil {
  110. HandleAddGPGKeyError(ctx, err)
  111. return
  112. }
  113. ctx.JSON(http.StatusCreated, convert.ToGPGKey(key))
  114. }
  115. // swagger:parameters userCurrentPostGPGKey
  116. type swaggerUserCurrentPostGPGKey struct {
  117. // in:body
  118. Form api.CreateGPGKeyOption
  119. }
  120. //CreateGPGKey create a GPG key belonging to the authenticated user
  121. func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
  122. // swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
  123. // ---
  124. // summary: Create a GPG key
  125. // consumes:
  126. // - application/json
  127. // produces:
  128. // - application/json
  129. // responses:
  130. // "201":
  131. // "$ref": "#/responses/GPGKey"
  132. // "422":
  133. // "$ref": "#/responses/validationError"
  134. CreateUserGPGKey(ctx, form, ctx.User.ID)
  135. }
  136. //DeleteGPGKey remove a GPG key belonging to the authenticated user
  137. func DeleteGPGKey(ctx *context.APIContext) {
  138. // swagger:operation DELETE /user/gpg_keys/{id} user userCurrentDeleteGPGKey
  139. // ---
  140. // summary: Remove a GPG key
  141. // produces:
  142. // - application/json
  143. // parameters:
  144. // - name: id
  145. // in: path
  146. // description: id of key to delete
  147. // type: integer
  148. // format: int64
  149. // required: true
  150. // responses:
  151. // "204":
  152. // "$ref": "#/responses/empty"
  153. // "403":
  154. // "$ref": "#/responses/forbidden"
  155. if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  156. if models.IsErrGPGKeyAccessDenied(err) {
  157. ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
  158. } else {
  159. ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
  160. }
  161. return
  162. }
  163. ctx.Status(http.StatusNoContent)
  164. }
  165. // HandleAddGPGKeyError handle add GPGKey error
  166. func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
  167. switch {
  168. case models.IsErrGPGKeyAccessDenied(err):
  169. ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key")
  170. case models.IsErrGPGKeyIDAlreadyUsed(err):
  171. ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists")
  172. default:
  173. ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
  174. }
  175. }