invite.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/invite"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewInviteScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetInviteScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetInviteScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getInviteRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getInviteRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/invites/{invite_id}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/invites -> invite.NewInvitesListHandler
  46. listEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: "/invites",
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. },
  58. },
  59. )
  60. listHandler := invite.NewInvitesListHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: listEndpoint,
  66. Handler: listHandler,
  67. Router: r,
  68. })
  69. // POST /api/projects/{project_id}/invites -> invite.NewInviteCreateHandler
  70. createEndpoint := factory.NewAPIEndpoint(
  71. &types.APIRequestMetadata{
  72. Verb: types.APIVerbCreate,
  73. Method: types.HTTPVerbPost,
  74. Path: &types.Path{
  75. Parent: basePath,
  76. RelativePath: "/invites",
  77. },
  78. Scopes: []types.PermissionScope{
  79. types.UserScope,
  80. types.ProjectScope,
  81. },
  82. },
  83. )
  84. createHandler := invite.NewInviteCreateHandler(
  85. config,
  86. factory.GetDecoderValidator(),
  87. factory.GetResultWriter(),
  88. )
  89. routes = append(routes, &Route{
  90. Endpoint: createEndpoint,
  91. Handler: createHandler,
  92. Router: r,
  93. })
  94. // GET /api/projects/{project_id}/invites/{token} -> invite.NewInviteAcceptHandler
  95. acceptEndpoint := factory.NewAPIEndpoint(
  96. &types.APIRequestMetadata{
  97. Verb: types.APIVerbGet,
  98. Method: types.HTTPVerbGet,
  99. Path: &types.Path{
  100. Parent: basePath,
  101. RelativePath: "/invites/{token}",
  102. },
  103. Scopes: []types.PermissionScope{
  104. types.UserScope,
  105. },
  106. ShouldRedirect: true,
  107. },
  108. )
  109. acceptHandler := invite.NewInviteAcceptHandler(config)
  110. routes = append(routes, &Route{
  111. Endpoint: acceptEndpoint,
  112. Handler: acceptHandler,
  113. Router: r,
  114. })
  115. // POST /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteUpdateRoleHandler
  116. updateRoleEndpoint := factory.NewAPIEndpoint(
  117. &types.APIRequestMetadata{
  118. Verb: types.APIVerbUpdate,
  119. Method: types.HTTPVerbPost,
  120. Path: &types.Path{
  121. Parent: basePath,
  122. RelativePath: relPath,
  123. },
  124. Scopes: []types.PermissionScope{
  125. types.UserScope,
  126. types.ProjectScope,
  127. types.InviteScope,
  128. },
  129. },
  130. )
  131. updateRoleHandler := invite.NewInviteUpdateRoleHandler(
  132. config,
  133. factory.GetDecoderValidator(),
  134. )
  135. routes = append(routes, &Route{
  136. Endpoint: updateRoleEndpoint,
  137. Handler: updateRoleHandler,
  138. Router: r,
  139. })
  140. // DELETE /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteGetHandler
  141. deleteEndpoint := factory.NewAPIEndpoint(
  142. &types.APIRequestMetadata{
  143. Verb: types.APIVerbDelete,
  144. Method: types.HTTPVerbDelete,
  145. Path: &types.Path{
  146. Parent: basePath,
  147. RelativePath: relPath,
  148. },
  149. Scopes: []types.PermissionScope{
  150. types.UserScope,
  151. types.ProjectScope,
  152. types.InviteScope,
  153. },
  154. },
  155. )
  156. deleteHandler := invite.NewInviteDeleteHandler(
  157. config,
  158. )
  159. routes = append(routes, &Route{
  160. Endpoint: deleteEndpoint,
  161. Handler: deleteHandler,
  162. Router: r,
  163. })
  164. return routes, newPath
  165. }