invite.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package router
  2. import (
  3. "github.com/go-chi/chi/v5"
  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. // POST /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteUpdateRoleHandler
  100. updateRoleEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbUpdate,
  103. Method: types.HTTPVerbPost,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: relPath,
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. types.SettingsScope,
  112. types.InviteScope,
  113. },
  114. },
  115. )
  116. updateRoleHandler := invite.NewInviteUpdateRoleHandler(
  117. config,
  118. factory.GetDecoderValidator(),
  119. )
  120. routes = append(routes, &router.Route{
  121. Endpoint: updateRoleEndpoint,
  122. Handler: updateRoleHandler,
  123. Router: r,
  124. })
  125. // DELETE /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteGetHandler
  126. deleteEndpoint := factory.NewAPIEndpoint(
  127. &types.APIRequestMetadata{
  128. Verb: types.APIVerbDelete,
  129. Method: types.HTTPVerbDelete,
  130. Path: &types.Path{
  131. Parent: basePath,
  132. RelativePath: relPath,
  133. },
  134. Scopes: []types.PermissionScope{
  135. types.UserScope,
  136. types.ProjectScope,
  137. types.SettingsScope,
  138. types.InviteScope,
  139. },
  140. },
  141. )
  142. deleteHandler := invite.NewInviteDeleteHandler(
  143. config,
  144. )
  145. routes = append(routes, &router.Route{
  146. Endpoint: deleteEndpoint,
  147. Handler: deleteHandler,
  148. Router: r,
  149. })
  150. return routes, newPath
  151. }