project.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }
  15. func NewProjectScopedFactory(
  16. config *config.Config,
  17. ) *ProjectScopedFactory {
  18. return &ProjectScopedFactory{config}
  19. }
  20. func (p *ProjectScopedFactory) Middleware(next http.Handler) http.Handler {
  21. return &ProjectScopedMiddleware{next, p.config}
  22. }
  23. type ProjectScopedMiddleware struct {
  24. next http.Handler
  25. config *config.Config
  26. }
  27. func (p *ProjectScopedMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. // get the project id from the URL param context
  29. reqScopes, _ := r.Context().Value(types.RequestScopeCtxKey).(map[types.PermissionScope]*types.RequestAction)
  30. projID := reqScopes[types.ProjectScope].Resource.UInt
  31. project, err := p.config.Repo.Project().ReadProject(projID)
  32. if err != nil {
  33. if err == gorm.ErrRecordNotFound {
  34. apierrors.HandleAPIError(p.config.Logger, p.config.Alerter, w, r, apierrors.NewErrForbidden(
  35. fmt.Errorf("project not found with id %d", projID),
  36. ), true)
  37. return
  38. }
  39. apierrors.HandleAPIError(p.config.Logger, p.config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  40. return
  41. }
  42. ctx := NewProjectContext(r.Context(), project)
  43. r = r.Clone(ctx)
  44. p.next.ServeHTTP(w, r)
  45. }
  46. func NewProjectContext(ctx context.Context, project *models.Project) context.Context {
  47. return context.WithValue(ctx, types.ProjectScope, project)
  48. }