key.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. func GetUserByParamsName(c *context.APIContext, name string) *models.User {
  17. user, err := models.GetUserByName(c.Params(name))
  18. if err != nil {
  19. if errors.IsUserNotExist(err) {
  20. c.Status(404)
  21. } else {
  22. c.Error(500, "GetUserByName", err)
  23. }
  24. return nil
  25. }
  26. return user
  27. }
  28. // GetUserByParams returns user whose name is presented in URL paramenter.
  29. func GetUserByParams(c *context.APIContext) *models.User {
  30. return GetUserByParamsName(c, ":username")
  31. }
  32. func composePublicKeysAPILink() string {
  33. return setting.AppURL + "api/v1/user/keys/"
  34. }
  35. func listPublicKeys(c *context.APIContext, uid int64) {
  36. keys, err := models.ListPublicKeys(uid)
  37. if err != nil {
  38. c.Error(500, "ListPublicKeys", err)
  39. return
  40. }
  41. apiLink := composePublicKeysAPILink()
  42. apiKeys := make([]*api.PublicKey, len(keys))
  43. for i := range keys {
  44. apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
  45. }
  46. c.JSON(200, &apiKeys)
  47. }
  48. func ListMyPublicKeys(c *context.APIContext) {
  49. listPublicKeys(c, c.User.ID)
  50. }
  51. func ListPublicKeys(c *context.APIContext) {
  52. user := GetUserByParams(c)
  53. if c.Written() {
  54. return
  55. }
  56. listPublicKeys(c, user.ID)
  57. }
  58. func GetPublicKey(c *context.APIContext) {
  59. key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
  60. if err != nil {
  61. if models.IsErrKeyNotExist(err) {
  62. c.Status(404)
  63. } else {
  64. c.Error(500, "GetPublicKeyByID", err)
  65. }
  66. return
  67. }
  68. apiLink := composePublicKeysAPILink()
  69. c.JSON(200, convert.ToPublicKey(apiLink, key))
  70. }
  71. // CreateUserPublicKey creates new public key to given user by ID.
  72. func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
  73. content, err := models.CheckPublicKeyString(form.Key)
  74. if err != nil {
  75. repo.HandleCheckKeyStringError(c, err)
  76. return
  77. }
  78. key, err := models.AddPublicKey(uid, form.Title, content)
  79. if err != nil {
  80. repo.HandleAddKeyError(c, err)
  81. return
  82. }
  83. apiLink := composePublicKeysAPILink()
  84. c.JSON(201, convert.ToPublicKey(apiLink, key))
  85. }
  86. func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
  87. CreateUserPublicKey(c, form, c.User.ID)
  88. }
  89. func DeletePublicKey(c *context.APIContext) {
  90. if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  91. if models.IsErrKeyAccessDenied(err) {
  92. c.Error(403, "", "You do not have access to this key")
  93. } else {
  94. c.Error(500, "DeletePublicKey", err)
  95. }
  96. return
  97. }
  98. c.Status(204)
  99. }