| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package router
- import (
- "github.com/go-chi/chi/v5"
- "github.com/porter-dev/porter/api/server/handlers/invite"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/server/shared/router"
- "github.com/porter-dev/porter/api/types"
- )
- func NewInviteScopedRegisterer(children ...*router.Registerer) *router.Registerer {
- return &router.Registerer{
- GetRoutes: GetInviteScopedRoutes,
- Children: children,
- }
- }
- func GetInviteScopedRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- children ...*router.Registerer,
- ) []*router.Route {
- routes, projPath := getInviteRoutes(r, config, basePath, factory)
- if len(children) > 0 {
- r.Route(projPath.RelativePath, func(r chi.Router) {
- for _, child := range children {
- childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
- routes = append(routes, childRoutes...)
- }
- })
- }
- return routes
- }
- func getInviteRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- ) ([]*router.Route, *types.Path) {
- relPath := "/invites/{invite_id}"
- newPath := &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- }
- routes := make([]*router.Route, 0)
- // GET /api/projects/{project_id}/invites -> invite.NewInvitesListHandler
- listEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: "/invites",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.SettingsScope,
- },
- },
- )
- listHandler := invite.NewInvitesListHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listEndpoint,
- Handler: listHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/invites -> invite.NewInviteCreateHandler
- createEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbCreate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: "/invites",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.SettingsScope,
- },
- CheckUsage: true,
- UsageMetric: types.Users,
- },
- )
- createHandler := invite.NewInviteCreateHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: createEndpoint,
- Handler: createHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/invites/{token} -> invite.NewInviteAcceptHandler
- acceptEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: "/invites/{token}",
- },
- // only user scope is needed here. adding the project scope will prevent the user
- // from joining the project, since they don't have a role in the project yet.
- Scopes: []types.PermissionScope{
- types.UserScope,
- },
- ShouldRedirect: true,
- },
- )
- acceptHandler := invite.NewInviteAcceptHandler(config)
- routes = append(routes, &router.Route{
- Endpoint: acceptEndpoint,
- Handler: acceptHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteUpdateRoleHandler
- updateRoleEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.SettingsScope,
- types.InviteScope,
- },
- },
- )
- updateRoleHandler := invite.NewInviteUpdateRoleHandler(
- config,
- factory.GetDecoderValidator(),
- )
- routes = append(routes, &router.Route{
- Endpoint: updateRoleEndpoint,
- Handler: updateRoleHandler,
- Router: r,
- })
- // DELETE /api/projects/{project_id}/invites/{invite_id} -> invite.NewInviteGetHandler
- deleteEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbDelete,
- Method: types.HTTPVerbDelete,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.SettingsScope,
- types.InviteScope,
- },
- },
- )
- deleteHandler := invite.NewInviteDeleteHandler(
- config,
- )
- routes = append(routes, &router.Route{
- Endpoint: deleteEndpoint,
- Handler: deleteHandler,
- Router: r,
- })
- return routes, newPath
- }
|