You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.8 KiB

  1. // Copyright 2016 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 repo
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. )
  10. // ListCollaborators list a repository's collaborators
  11. func ListCollaborators(ctx *context.APIContext) {
  12. if !ctx.Repo.IsWriter() {
  13. ctx.Error(403, "", "User does not have push access")
  14. return
  15. }
  16. collaborators, err := ctx.Repo.Repository.GetCollaborators()
  17. if err != nil {
  18. ctx.Error(500, "ListCollaborators", err)
  19. return
  20. }
  21. users := make([]*api.User, len(collaborators))
  22. for i, collaborator := range collaborators {
  23. users[i] = collaborator.APIFormat()
  24. }
  25. ctx.JSON(200, users)
  26. }
  27. // IsCollaborator check if a user is a collaborator of a repository
  28. func IsCollaborator(ctx *context.APIContext) {
  29. if !ctx.Repo.IsWriter() {
  30. ctx.Error(403, "", "User does not have push access")
  31. return
  32. }
  33. user, err := models.GetUserByName(ctx.Params(":collaborator"))
  34. if err != nil {
  35. if models.IsErrUserNotExist(err) {
  36. ctx.Error(422, "", err)
  37. } else {
  38. ctx.Error(500, "GetUserByName", err)
  39. }
  40. return
  41. }
  42. isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID)
  43. if err != nil {
  44. ctx.Error(500, "IsCollaborator", err)
  45. return
  46. }
  47. if isColab {
  48. ctx.Status(204)
  49. } else {
  50. ctx.Status(404)
  51. }
  52. }
  53. // AddCollaborator add a collaborator of a repository
  54. func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) {
  55. if !ctx.Repo.IsWriter() {
  56. ctx.Error(403, "", "User does not have push access")
  57. return
  58. }
  59. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  60. if err != nil {
  61. if models.IsErrUserNotExist(err) {
  62. ctx.Error(422, "", err)
  63. } else {
  64. ctx.Error(500, "GetUserByName", err)
  65. }
  66. return
  67. }
  68. if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil {
  69. ctx.Error(500, "AddCollaborator", err)
  70. return
  71. }
  72. if form.Permission != nil {
  73. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  74. ctx.Error(500, "ChangeCollaborationAccessMode", err)
  75. return
  76. }
  77. }
  78. ctx.Status(204)
  79. }
  80. // DeleteCollaborator delete a collaborator from a repository
  81. func DeleteCollaborator(ctx *context.APIContext) {
  82. if !ctx.Repo.IsWriter() {
  83. ctx.Error(403, "", "User does not have push access")
  84. return
  85. }
  86. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  87. if err != nil {
  88. if models.IsErrUserNotExist(err) {
  89. ctx.Error(422, "", err)
  90. } else {
  91. ctx.Error(500, "GetUserByName", err)
  92. }
  93. return
  94. }
  95. if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  96. ctx.Error(500, "DeleteCollaboration", err)
  97. return
  98. }
  99. ctx.Status(204)
  100. }