key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/models/errors"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/pkg/setting"
  12. "gitote/gitote/routes/api/v1/convert"
  13. "gitote/gitote/routes/api/v1/repo"
  14. api "gitlab.com/gitote/go-gitote-client"
  15. )
  16. // GetUserByParamsName get user by name
  17. func GetUserByParamsName(c *context.APIContext, name string) *models.User {
  18. user, err := models.GetUserByName(c.Params(name))
  19. if err != nil {
  20. if errors.IsUserNotExist(err) {
  21. c.Status(404)
  22. } else {
  23. c.Error(500, "GetUserByName", err)
  24. }
  25. return nil
  26. }
  27. return user
  28. }
  29. // GetUserByParams returns user whose name is presented in URL paramenter.
  30. func GetUserByParams(c *context.APIContext) *models.User {
  31. return GetUserByParamsName(c, ":username")
  32. }
  33. func composePublicKeysAPILink() string {
  34. return setting.AppURL + "api/v1/user/keys/"
  35. }
  36. func listPublicKeys(c *context.APIContext, uid int64) {
  37. keys, err := models.ListPublicKeys(uid)
  38. if err != nil {
  39. c.Error(500, "ListPublicKeys", err)
  40. return
  41. }
  42. apiLink := composePublicKeysAPILink()
  43. apiKeys := make([]*api.PublicKey, len(keys))
  44. for i := range keys {
  45. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  46. }
  47. c.JSON(200, &apiKeys)
  48. }
  49. // ListMyPublicKeys list all of the authenticated user's public keys
  50. func ListMyPublicKeys(c *context.APIContext) {
  51. listPublicKeys(c, c.User.ID)
  52. }
  53. // ListPublicKeys list the given user's public keys
  54. func ListPublicKeys(c *context.APIContext) {
  55. user := GetUserByParams(c)
  56. if c.Written() {
  57. return
  58. }
  59. listPublicKeys(c, user.ID)
  60. }
  61. // GetPublicKey get a public key
  62. func GetPublicKey(c *context.APIContext) {
  63. key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
  64. if err != nil {
  65. if models.IsErrKeyNotExist(err) {
  66. c.Status(404)
  67. } else {
  68. c.Error(500, "GetPublicKeyByID", err)
  69. }
  70. return
  71. }
  72. apiLink := composePublicKeysAPILink()
  73. c.JSON(200, convert.ToPublicKey(apiLink, key))
  74. }
  75. // CreateUserPublicKey creates new public key to given user by ID.
  76. func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
  77. content, err := models.CheckPublicKeyString(form.Key)
  78. if err != nil {
  79. repo.HandleCheckKeyStringError(c, err)
  80. return
  81. }
  82. key, err := models.AddPublicKey(uid, form.Title, content)
  83. if err != nil {
  84. repo.HandleAddKeyError(c, err)
  85. return
  86. }
  87. apiLink := composePublicKeysAPILink()
  88. c.JSON(201, convert.ToPublicKey(apiLink, key))
  89. }
  90. // CreatePublicKey create one public key for me
  91. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  92. CreateUserPublicKey(c, form, c.User.ID)
  93. }
  94. // DeletePublicKey delete one public key
  95. func DeletePublicKey(c *context.APIContext) {
  96. if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  97. if models.IsErrKeyAccessDenied(err) {
  98. c.Error(403, "", "You do not have access to this key")
  99. } else {
  100. c.Error(500, "DeletePublicKey", err)
  101. }
  102. return
  103. }
  104. c.Status(204)
  105. }