key.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "github.com/gogs/go-gogs-client"
  7. "github.com/gogs/gogs/models"
  8. "github.com/gogs/gogs/models/errors"
  9. "github.com/gogs/gogs/pkg/context"
  10. "github.com/gogs/gogs/pkg/setting"
  11. "github.com/gogs/gogs/routes/api/v1/convert"
  12. "github.com/gogs/gogs/routes/api/v1/repo"
  13. )
  14. func GetUserByParamsName(c *context.APIContext, name string) *models.User {
  15. user, err := models.GetUserByName(c.Params(name))
  16. if err != nil {
  17. if errors.IsUserNotExist(err) {
  18. c.Status(404)
  19. } else {
  20. c.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(c *context.APIContext) *models.User {
  28. return GetUserByParamsName(c, ":username")
  29. }
  30. func composePublicKeysAPILink() string {
  31. return setting.AppURL + "api/v1/user/keys/"
  32. }
  33. func listPublicKeys(c *context.APIContext, uid int64) {
  34. keys, err := models.ListPublicKeys(uid)
  35. if err != nil {
  36. c.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. c.JSON(200, &apiKeys)
  45. }
  46. // https://github.com/gogs/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys
  47. func ListMyPublicKeys(c *context.APIContext) {
  48. listPublicKeys(c, c.User.ID)
  49. }
  50. // https://github.com/gogs/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user
  51. func ListPublicKeys(c *context.APIContext) {
  52. user := GetUserByParams(c)
  53. if c.Written() {
  54. return
  55. }
  56. listPublicKeys(c, user.ID)
  57. }
  58. // https://github.com/gogs/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key
  59. func GetPublicKey(c *context.APIContext) {
  60. key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
  61. if err != nil {
  62. if models.IsErrKeyNotExist(err) {
  63. c.Status(404)
  64. } else {
  65. c.Error(500, "GetPublicKeyByID", err)
  66. }
  67. return
  68. }
  69. apiLink := composePublicKeysAPILink()
  70. c.JSON(200, convert.ToPublicKey(apiLink, key))
  71. }
  72. // CreateUserPublicKey creates new public key to given user by ID.
  73. func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
  74. content, err := models.CheckPublicKeyString(form.Key)
  75. if err != nil {
  76. repo.HandleCheckKeyStringError(c, err)
  77. return
  78. }
  79. key, err := models.AddPublicKey(uid, form.Title, content)
  80. if err != nil {
  81. repo.HandleAddKeyError(c, err)
  82. return
  83. }
  84. apiLink := composePublicKeysAPILink()
  85. c.JSON(201, convert.ToPublicKey(apiLink, key))
  86. }
  87. // https://github.com/gogs/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key
  88. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  89. CreateUserPublicKey(c, form, c.User.ID)
  90. }
  91. // https://github.com/gogs/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key
  92. func DeletePublicKey(c *context.APIContext) {
  93. if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  94. if models.IsErrKeyAccessDenied(err) {
  95. c.Error(403, "", "You do not have access to this key")
  96. } else {
  97. c.Error(500, "DeletePublicKey", err)
  98. }
  99. return
  100. }
  101. c.Status(204)
  102. }