project.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package auth
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/repository"
  10. )
  11. type ProjectScopedFactory struct {
  12. projectRepo repository.ProjectRepository
  13. config *shared.Config
  14. }
  15. func NewProjectScopedFactory(
  16. projectRepo repository.ProjectRepository,
  17. config *shared.Config,
  18. ) *ProjectScopedFactory {
  19. return &ProjectScopedFactory{projectRepo, config}
  20. }
  21. func (f *ProjectScopedFactory) NewProjectScoped(next http.Handler) http.Handler {
  22. return &ProjectScoped{next, f.projectRepo, f.config}
  23. }
  24. type ProjectScoped struct {
  25. next http.Handler
  26. projectRepo repository.ProjectRepository
  27. config *shared.Config
  28. }
  29. func (scope *ProjectScoped) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. // read the project id from the request
  31. projID, reqErr := GetURLParamUint(r, "project_id")
  32. if reqErr != nil {
  33. apierrors.HandleAPIError(w, scope.config.Logger, reqErr)
  34. return
  35. }
  36. fmt.Println("PROJECT ID IS", projID)
  37. // find a set of roles for this user and compute a policy document
  38. // determine if policy document allows for project scope
  39. project := types.Project{}
  40. // create a new project-scoped context and serve
  41. req := r.Clone(NewProjectContext(r.Context(), project))
  42. scope.next.ServeHTTP(w, req)
  43. }
  44. func NewProjectContext(ctx context.Context, project types.Project) context.Context {
  45. return context.WithValue(ctx, types.ProjectScope, project)
  46. }