project.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package authz
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. "gorm.io/gorm"
  11. )
  12. type ProjectScopedFactory struct {
  13. config *config.Config
  14. endpointMeta types.APIRequestMetadata
  15. }
  16. func NewProjectScopedFactory(
  17. config *config.Config,
  18. endpointMeta types.APIRequestMetadata,
  19. ) *ProjectScopedFactory {
  20. return &ProjectScopedFactory{config, endpointMeta}
  21. }
  22. func (p *ProjectScopedFactory) Middleware(next http.Handler) http.Handler {
  23. return &ProjectScopedMiddleware{next, p.endpointMeta, p.config}
  24. }
  25. type ProjectScopedMiddleware struct {
  26. next http.Handler
  27. endpointMeta types.APIRequestMetadata
  28. config *config.Config
  29. }
  30. func (p *ProjectScopedMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. // get the full map of scopes to resource actions
  32. reqScopes, reqErr := getRequestActionForEndpoint(r, p.endpointMeta)
  33. if reqErr != nil {
  34. apierrors.HandleAPIError(p.config.Logger, p.config.Alerter, w, r, reqErr, true)
  35. return
  36. }
  37. projID := reqScopes[types.ProjectScope].Resource.UInt
  38. project, err := p.config.Repo.Project().ReadProject(projID)
  39. if err != nil {
  40. if err == gorm.ErrRecordNotFound {
  41. apierrors.HandleAPIError(p.config.Logger, p.config.Alerter, w, r, apierrors.NewErrForbidden(
  42. fmt.Errorf("project not found with id %d", projID),
  43. ), true)
  44. return
  45. }
  46. apierrors.HandleAPIError(p.config.Logger, p.config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  47. return
  48. }
  49. ctx := NewProjectContext(r.Context(), project)
  50. ctx = NewRequestScopeCtx(ctx, reqScopes)
  51. r = r.Clone(ctx)
  52. p.next.ServeHTTP(w, r)
  53. }
  54. func NewProjectContext(ctx context.Context, project *models.Project) context.Context {
  55. return context.WithValue(ctx, types.ProjectScope, project)
  56. }