key.go 2.7 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. convert2 "gogs.io/gogs/internal/route/api/v1/convert"
  8. repo2 "gogs.io/gogs/internal/route/api/v1/repo"
  9. "net/http"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/db"
  13. "gogs.io/gogs/internal/db/errors"
  14. )
  15. func GetUserByParamsName(c *context.APIContext, name string) *db.User {
  16. user, err := db.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) *db.User {
  25. return GetUserByParamsName(c, ":username")
  26. }
  27. func composePublicKeysAPILink() string {
  28. return conf.Server.ExternalURL + "api/v1/user/keys/"
  29. }
  30. func listPublicKeys(c *context.APIContext, uid int64) {
  31. keys, err := db.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] = convert2.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 := db.GetPublicKeyByID(c.ParamsInt64(":id"))
  55. if err != nil {
  56. c.NotFoundOrServerError("GetPublicKeyByID", db.IsErrKeyNotExist, err)
  57. return
  58. }
  59. apiLink := composePublicKeysAPILink()
  60. c.JSONSuccess(convert2.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 := db.CheckPublicKeyString(form.Key)
  65. if err != nil {
  66. repo2.HandleCheckKeyStringError(c, err)
  67. return
  68. }
  69. key, err := db.AddPublicKey(uid, form.Title, content)
  70. if err != nil {
  71. repo2.HandleAddKeyError(c, err)
  72. return
  73. }
  74. apiLink := composePublicKeysAPILink()
  75. c.JSON(http.StatusCreated, convert2.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 := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
  82. if db.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. }