key.go 3.3 KB

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