2
0

invite.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. types.SettingsScope,
  58. },
  59. },
  60. )
  61. listHandler := invite.NewInvitesListHandler(
  62. config,
  63. factory.GetResultWriter(),
  64. )
  65. routes = append(routes, &Route{
  66. Endpoint: listEndpoint,
  67. Handler: listHandler,
  68. Router: r,
  69. })
  70. // POST /api/projects/{project_id}/invites -> invite.NewInviteCreateHandler
  71. createEndpoint := factory.NewAPIEndpoint(
  72. &types.APIRequestMetadata{
  73. Verb: types.APIVerbCreate,
  74. Method: types.HTTPVerbPost,
  75. Path: &types.Path{
  76. Parent: basePath,
  77. RelativePath: "/invites",
  78. },
  79. Scopes: []types.PermissionScope{
  80. types.UserScope,
  81. types.ProjectScope,
  82. types.SettingsScope,
  83. },
  84. CheckUsage: true,
  85. UsageMetric: types.Users,
  86. },
  87. )
  88. createHandler := invite.NewInviteCreateHandler(
  89. config,
  90. factory.GetDecoderValidator(),
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &Route{
  94. Endpoint: createEndpoint,
  95. Handler: createHandler,
  96. Router: r,
  97. })
  98. // GET /api/projects/{project_id}/invites/{token} -> invite.NewInviteAcceptHandler
  99. acceptEndpoint := factory.NewAPIEndpoint(
  100. &types.APIRequestMetadata{
  101. Verb: types.APIVerbGet,
  102. Method: types.HTTPVerbGet,
  103. Path: &types.Path{
  104. Parent: basePath,
  105. RelativePath: "/invites/{token}",
  106. },
  107. // only user scope is needed here. adding the project scope will prevent the user
  108. // from joining the project, since they don't have a role in the project yet.
  109. Scopes: []types.PermissionScope{
  110. types.UserScope,
  111. },
  112. ShouldRedirect: true,
  113. CheckUsage: true,
  114. UsageMetric: types.Users,
  115. },
  116. )
  117. acceptHandler := invite.NewInviteAcceptHandler(config)
  118. routes = append(routes, &Route{
  119. Endpoint: acceptEndpoint,
  120. Handler: acceptHandler,
  121. Router: r,
  122. })
  123. // POST /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteUpdateRoleHandler
  124. updateRoleEndpoint := factory.NewAPIEndpoint(
  125. &types.APIRequestMetadata{
  126. Verb: types.APIVerbUpdate,
  127. Method: types.HTTPVerbPost,
  128. Path: &types.Path{
  129. Parent: basePath,
  130. RelativePath: relPath,
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. types.SettingsScope,
  136. types.InviteScope,
  137. },
  138. },
  139. )
  140. updateRoleHandler := invite.NewInviteUpdateRoleHandler(
  141. config,
  142. factory.GetDecoderValidator(),
  143. )
  144. routes = append(routes, &Route{
  145. Endpoint: updateRoleEndpoint,
  146. Handler: updateRoleHandler,
  147. Router: r,
  148. })
  149. // DELETE /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteGetHandler
  150. deleteEndpoint := factory.NewAPIEndpoint(
  151. &types.APIRequestMetadata{
  152. Verb: types.APIVerbDelete,
  153. Method: types.HTTPVerbDelete,
  154. Path: &types.Path{
  155. Parent: basePath,
  156. RelativePath: relPath,
  157. },
  158. Scopes: []types.PermissionScope{
  159. types.UserScope,
  160. types.ProjectScope,
  161. types.SettingsScope,
  162. types.InviteScope,
  163. },
  164. },
  165. )
  166. deleteHandler := invite.NewInviteDeleteHandler(
  167. config,
  168. )
  169. routes = append(routes, &Route{
  170. Endpoint: deleteEndpoint,
  171. Handler: deleteHandler,
  172. Router: r,
  173. })
  174. return routes, newPath
  175. }