keys.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/middleware"
  9. "github.com/gogits/gogs/modules/setting"
  10. "github.com/gogits/gogs/routers/api/v1/repo"
  11. to "github.com/gogits/gogs/routers/api/v1/utils"
  12. )
  13. // GetUserByParams returns user whose name is presented in URL paramenter.
  14. func GetUserByParams(ctx *middleware.Context) *models.User {
  15. user, err := models.GetUserByName(ctx.Params(":username"))
  16. if err != nil {
  17. if models.IsErrUserNotExist(err) {
  18. ctx.Error(404)
  19. } else {
  20. ctx.APIError(500, "GetUserByName", err)
  21. }
  22. return nil
  23. }
  24. return user
  25. }
  26. func composePublicKeysAPILink() string {
  27. return setting.AppUrl + "api/v1/user/keys/"
  28. }
  29. func listPublicKeys(ctx *middleware.Context, uid int64) {
  30. keys, err := models.ListPublicKeys(uid)
  31. if err != nil {
  32. ctx.APIError(500, "ListPublicKeys", err)
  33. return
  34. }
  35. apiLink := composePublicKeysAPILink()
  36. apiKeys := make([]*api.PublicKey, len(keys))
  37. for i := range keys {
  38. apiKeys[i] = to.ApiPublicKey(apiLink, keys[i])
  39. }
  40. ctx.JSON(200, &apiKeys)
  41. }
  42. // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys
  43. func ListMyPublicKeys(ctx *middleware.Context) {
  44. listPublicKeys(ctx, ctx.User.Id)
  45. }
  46. // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user
  47. func ListPublicKeys(ctx *middleware.Context) {
  48. user := GetUserByParams(ctx)
  49. if ctx.Written() {
  50. return
  51. }
  52. listPublicKeys(ctx, user.Id)
  53. }
  54. // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key
  55. func GetPublicKey(ctx *middleware.Context) {
  56. key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
  57. if err != nil {
  58. if models.IsErrKeyNotExist(err) {
  59. ctx.Error(404)
  60. } else {
  61. ctx.Handle(500, "GetPublicKeyByID", err)
  62. }
  63. return
  64. }
  65. apiLink := composePublicKeysAPILink()
  66. ctx.JSON(200, to.ApiPublicKey(apiLink, key))
  67. }
  68. // CreateUserPublicKey creates new public key to given user by ID.
  69. func CreateUserPublicKey(ctx *middleware.Context, form api.CreateKeyOption, uid int64) {
  70. content, err := models.CheckPublicKeyString(form.Key)
  71. if err != nil {
  72. repo.HandleCheckKeyStringError(ctx, err)
  73. return
  74. }
  75. key, err := models.AddPublicKey(uid, form.Title, content)
  76. if err != nil {
  77. repo.HandleAddKeyError(ctx, err)
  78. return
  79. }
  80. apiLink := composePublicKeysAPILink()
  81. ctx.JSON(201, to.ApiPublicKey(apiLink, key))
  82. }
  83. // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key
  84. func CreatePublicKey(ctx *middleware.Context, form api.CreateKeyOption) {
  85. CreateUserPublicKey(ctx, form, ctx.User.Id)
  86. }
  87. // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key
  88. func DeletePublicKey(ctx *middleware.Context) {
  89. if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
  90. if models.IsErrKeyAccessDenied(err) {
  91. ctx.APIError(403, "", "You do not have access to this key")
  92. } else {
  93. ctx.APIError(500, "DeletePublicKey", err)
  94. }
  95. return
  96. }
  97. ctx.Status(204)
  98. }