invite.go 4.6 KB

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