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.

125 lines
3.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. // Copyright 2015 The Gogs 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. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/routers/api/v1/convert"
  11. "code.gitea.io/gitea/routers/api/v1/repo"
  12. )
  13. // GetUserByParamsName get user by name
  14. func GetUserByParamsName(ctx *context.APIContext, name string) *models.User {
  15. user, err := models.GetUserByName(ctx.Params(name))
  16. if err != nil {
  17. if models.IsErrUserNotExist(err) {
  18. ctx.Status(404)
  19. } else {
  20. ctx.Error(500, "GetUserByName", err)
  21. }
  22. return nil
  23. }
  24. return user
  25. }
  26. // GetUserByParams returns user whose name is presented in URL paramenter.
  27. func GetUserByParams(ctx *context.APIContext) *models.User {
  28. return GetUserByParamsName(ctx, ":username")
  29. }
  30. func composePublicKeysAPILink() string {
  31. return setting.AppURL + "api/v1/user/keys/"
  32. }
  33. func listPublicKeys(ctx *context.APIContext, uid int64) {
  34. keys, err := models.ListPublicKeys(uid)
  35. if err != nil {
  36. ctx.Error(500, "ListPublicKeys", err)
  37. return
  38. }
  39. apiLink := composePublicKeysAPILink()
  40. apiKeys := make([]*api.PublicKey, len(keys))
  41. for i := range keys {
  42. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  43. }
  44. ctx.JSON(200, &apiKeys)
  45. }
  46. // ListMyPublicKeys list all my public keys
  47. // see https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys
  48. func ListMyPublicKeys(ctx *context.APIContext) {
  49. listPublicKeys(ctx, ctx.User.ID)
  50. }
  51. // ListPublicKeys list all user's public keys
  52. // see https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user
  53. func ListPublicKeys(ctx *context.APIContext) {
  54. user := GetUserByParams(ctx)
  55. if ctx.Written() {
  56. return
  57. }
  58. listPublicKeys(ctx, user.ID)
  59. }
  60. // GetPublicKey get one public key
  61. // see https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key
  62. func GetPublicKey(ctx *context.APIContext) {
  63. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  64. if err != nil {
  65. if models.IsErrKeyNotExist(err) {
  66. ctx.Status(404)
  67. } else {
  68. ctx.Error(500, "GetPublicKeyByID", err)
  69. }
  70. return
  71. }
  72. apiLink := composePublicKeysAPILink()
  73. ctx.JSON(200, convert.ToPublicKey(apiLink, key))
  74. }
  75. // CreateUserPublicKey creates new public key to given user by ID.
  76. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
  77. content, err := models.CheckPublicKeyString(form.Key)
  78. if err != nil {
  79. repo.HandleCheckKeyStringError(ctx, err)
  80. return
  81. }
  82. key, err := models.AddPublicKey(uid, form.Title, content)
  83. if err != nil {
  84. repo.HandleAddKeyError(ctx, err)
  85. return
  86. }
  87. apiLink := composePublicKeysAPILink()
  88. ctx.JSON(201, convert.ToPublicKey(apiLink, key))
  89. }
  90. // CreatePublicKey create one public key for me
  91. // see https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key
  92. func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
  93. CreateUserPublicKey(ctx, form, ctx.User.ID)
  94. }
  95. // DeletePublicKey delete one public key of mine
  96. // see https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key
  97. func DeletePublicKey(ctx *context.APIContext) {
  98. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  99. if models.IsErrKeyAccessDenied(err) {
  100. ctx.Error(403, "", "You do not have access to this key")
  101. } else {
  102. ctx.Error(500, "DeletePublicKey", err)
  103. }
  104. return
  105. }
  106. ctx.Status(204)
  107. }