collaborators.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/database"
  10. )
  11. func ListCollaborators(c *context.APIContext) {
  12. collaborators, err := c.Repo.Repository.GetCollaborators()
  13. if err != nil {
  14. c.Error(err, "get collaborators")
  15. return
  16. }
  17. apiCollaborators := make([]*api.Collaborator, len(collaborators))
  18. for i := range collaborators {
  19. apiCollaborators[i] = collaborators[i].APIFormat()
  20. }
  21. c.JSONSuccess(&apiCollaborators)
  22. }
  23. func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) {
  24. collaborator, err := database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":collaborator"))
  25. if err != nil {
  26. if database.IsErrUserNotExist(err) {
  27. c.Status(http.StatusUnprocessableEntity)
  28. } else {
  29. c.Error(err, "get user by name")
  30. }
  31. return
  32. }
  33. if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil {
  34. c.Error(err, "add collaborator")
  35. return
  36. }
  37. if form.Permission != nil {
  38. if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, database.ParseAccessMode(*form.Permission)); err != nil {
  39. c.Error(err, "change collaboration access mode")
  40. return
  41. }
  42. }
  43. c.NoContent()
  44. }
  45. func IsCollaborator(c *context.APIContext) {
  46. collaborator, err := database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":collaborator"))
  47. if err != nil {
  48. if database.IsErrUserNotExist(err) {
  49. c.Status(http.StatusUnprocessableEntity)
  50. } else {
  51. c.Error(err, "get user by name")
  52. }
  53. return
  54. }
  55. if !c.Repo.Repository.IsCollaborator(collaborator.ID) {
  56. c.NotFound()
  57. } else {
  58. c.NoContent()
  59. }
  60. }
  61. func DeleteCollaborator(c *context.APIContext) {
  62. collaborator, err := database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":collaborator"))
  63. if err != nil {
  64. if database.IsErrUserNotExist(err) {
  65. c.Status(http.StatusUnprocessableEntity)
  66. } else {
  67. c.Error(err, "get user by name")
  68. }
  69. return
  70. }
  71. if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  72. c.Error(err, "delete collaboration")
  73. return
  74. }
  75. c.NoContent()
  76. }