key.go 3.2 KB

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