key.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "net/http"
  8. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/models/errors"
  10. "github.com/gogs/gogs/pkg/context"
  11. "github.com/gogs/gogs/pkg/setting"
  12. "github.com/gogs/gogs/routes/api/v1/convert"
  13. "github.com/gogs/gogs/routes/api/v1/repo"
  14. )
  15. func GetUserByParamsName(c *context.APIContext, name string) *models.User {
  16. user, err := models.GetUserByName(c.Params(name))
  17. if err != nil {
  18. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  19. return nil
  20. }
  21. return user
  22. }
  23. // GetUserByParams returns user whose name is presented in URL paramenter.
  24. func GetUserByParams(c *context.APIContext) *models.User {
  25. return GetUserByParamsName(c, ":username")
  26. }
  27. func composePublicKeysAPILink() string {
  28. return setting.AppURL + "api/v1/user/keys/"
  29. }
  30. func listPublicKeys(c *context.APIContext, uid int64) {
  31. keys, err := models.ListPublicKeys(uid)
  32. if err != nil {
  33. c.ServerError("ListPublicKeys", err)
  34. return
  35. }
  36. apiLink := composePublicKeysAPILink()
  37. apiKeys := make([]*api.PublicKey, len(keys))
  38. for i := range keys {
  39. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  40. }
  41. c.JSONSuccess(&apiKeys)
  42. }
  43. func ListMyPublicKeys(c *context.APIContext) {
  44. listPublicKeys(c, c.User.ID)
  45. }
  46. func ListPublicKeys(c *context.APIContext) {
  47. user := GetUserByParams(c)
  48. if c.Written() {
  49. return
  50. }
  51. listPublicKeys(c, user.ID)
  52. }
  53. func GetPublicKey(c *context.APIContext) {
  54. key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
  55. if err != nil {
  56. c.NotFoundOrServerError("GetPublicKeyByID", models.IsErrKeyNotExist, err)
  57. return
  58. }
  59. apiLink := composePublicKeysAPILink()
  60. c.JSONSuccess(convert.ToPublicKey(apiLink, key))
  61. }
  62. // CreateUserPublicKey creates new public key to given user by ID.
  63. func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
  64. content, err := models.CheckPublicKeyString(form.Key)
  65. if err != nil {
  66. repo.HandleCheckKeyStringError(c, err)
  67. return
  68. }
  69. key, err := models.AddPublicKey(uid, form.Title, content)
  70. if err != nil {
  71. repo.HandleAddKeyError(c, err)
  72. return
  73. }
  74. apiLink := composePublicKeysAPILink()
  75. c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key))
  76. }
  77. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  78. CreateUserPublicKey(c, form, c.User.ID)
  79. }
  80. func DeletePublicKey(c *context.APIContext) {
  81. if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  82. if models.IsErrKeyAccessDenied(err) {
  83. c.Error(http.StatusForbidden, "", "you do not have access to this key")
  84. } else {
  85. c.Error(http.StatusInternalServerError, "DeletePublicKey", err)
  86. }
  87. return
  88. }
  89. c.NoContent()
  90. }