本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.3KB

  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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. )
  11. func listGPGKeys(ctx *context.APIContext, uid int64) {
  12. keys, err := models.ListGPGKeys(uid)
  13. if err != nil {
  14. ctx.Error(500, "ListGPGKeys", err)
  15. return
  16. }
  17. apiKeys := make([]*api.GPGKey, len(keys))
  18. for i := range keys {
  19. apiKeys[i] = convert.ToGPGKey(keys[i])
  20. }
  21. ctx.JSON(200, &apiKeys)
  22. }
  23. //ListGPGKeys get the GPG key list of a user
  24. func ListGPGKeys(ctx *context.APIContext) {
  25. // swagger:operation GET /users/{username}/gpg_keys user userListGPGKeys
  26. // ---
  27. // summary: List the given user's GPG keys
  28. // produces:
  29. // - application/json
  30. // parameters:
  31. // - name: username
  32. // in: path
  33. // description: username of user
  34. // type: string
  35. // required: true
  36. // responses:
  37. // "200":
  38. // "$ref": "#/responses/GPGKeyList"
  39. user := GetUserByParams(ctx)
  40. if ctx.Written() {
  41. return
  42. }
  43. listGPGKeys(ctx, user.ID)
  44. }
  45. //ListMyGPGKeys get the GPG key list of the authenticated user
  46. func ListMyGPGKeys(ctx *context.APIContext) {
  47. // swagger:operation GET /user/gpg_keys user userCurrentListGPGKeys
  48. // ---
  49. // summary: List the authenticated user's GPG keys
  50. // produces:
  51. // - application/json
  52. // responses:
  53. // "200":
  54. // "$ref": "#/responses/GPGKeyList"
  55. listGPGKeys(ctx, ctx.User.ID)
  56. }
  57. //GetGPGKey get the GPG key based on a id
  58. func GetGPGKey(ctx *context.APIContext) {
  59. // swagger:operation GET /user/gpg_keys/{id} user userCurrentGetGPGKey
  60. // ---
  61. // summary: Get a GPG key
  62. // produces:
  63. // - application/json
  64. // parameters:
  65. // - name: id
  66. // in: path
  67. // description: id of key to get
  68. // type: integer
  69. // format: int64
  70. // required: true
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/GPGKey"
  74. // "404":
  75. // "$ref": "#/responses/notFound"
  76. key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id"))
  77. if err != nil {
  78. if models.IsErrGPGKeyNotExist(err) {
  79. ctx.NotFound()
  80. } else {
  81. ctx.Error(500, "GetGPGKeyByID", err)
  82. }
  83. return
  84. }
  85. ctx.JSON(200, convert.ToGPGKey(key))
  86. }
  87. // CreateUserGPGKey creates new GPG key to given user by ID.
  88. func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
  89. key, err := models.AddGPGKey(uid, form.ArmoredKey)
  90. if err != nil {
  91. HandleAddGPGKeyError(ctx, err)
  92. return
  93. }
  94. ctx.JSON(201, convert.ToGPGKey(key))
  95. }
  96. // swagger:parameters userCurrentPostGPGKey
  97. type swaggerUserCurrentPostGPGKey struct {
  98. // in:body
  99. Form api.CreateGPGKeyOption
  100. }
  101. //CreateGPGKey create a GPG key belonging to the authenticated user
  102. func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
  103. // swagger:operation POST /user/gpg_keys user userCurrentPostGPGKey
  104. // ---
  105. // summary: Create a GPG key
  106. // consumes:
  107. // - application/json
  108. // produces:
  109. // - application/json
  110. // responses:
  111. // "201":
  112. // "$ref": "#/responses/GPGKey"
  113. // "422":
  114. // "$ref": "#/responses/validationError"
  115. CreateUserGPGKey(ctx, form, ctx.User.ID)
  116. }
  117. //DeleteGPGKey remove a GPG key belonging to the authenticated user
  118. func DeleteGPGKey(ctx *context.APIContext) {
  119. // swagger:operation DELETE /user/gpg_keys/{id} user userCurrentDeleteGPGKey
  120. // ---
  121. // summary: Remove a GPG key
  122. // produces:
  123. // - application/json
  124. // parameters:
  125. // - name: id
  126. // in: path
  127. // description: id of key to delete
  128. // type: integer
  129. // format: int64
  130. // required: true
  131. // responses:
  132. // "204":
  133. // "$ref": "#/responses/empty"
  134. // "403":
  135. // "$ref": "#/responses/forbidden"
  136. if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  137. if models.IsErrGPGKeyAccessDenied(err) {
  138. ctx.Error(403, "", "You do not have access to this key")
  139. } else {
  140. ctx.Error(500, "DeleteGPGKey", err)
  141. }
  142. return
  143. }
  144. ctx.Status(204)
  145. }
  146. // HandleAddGPGKeyError handle add GPGKey error
  147. func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
  148. switch {
  149. case models.IsErrGPGKeyAccessDenied(err):
  150. ctx.Error(422, "", "You do not have access to this GPG key")
  151. case models.IsErrGPGKeyIDAlreadyUsed(err):
  152. ctx.Error(422, "", "A key with the same id already exists")
  153. default:
  154. ctx.Error(500, "AddGPGKey", err)
  155. }
  156. }
上海开阖软件有限公司 沪ICP备12045867号-1