Files
coder/coderd/httpmw/organizationparam.go
T

94 lines
3.1 KiB
Go
Raw Normal View History

2022-01-24 11:07:42 -06:00
package httpmw
import (
"context"
"net/http"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/codersdk"
2022-01-24 11:07:42 -06:00
)
2023-02-18 18:32:09 -06:00
type (
organizationParamContextKey struct{}
organizationMemberParamContextKey struct{}
)
2022-01-24 11:07:42 -06:00
// OrganizationParam returns the organization from the ExtractOrganizationParam handler.
func OrganizationParam(r *http.Request) database.Organization {
organization, ok := r.Context().Value(organizationParamContextKey{}).(database.Organization)
if !ok {
panic("developer error: organization param middleware not provided")
}
return organization
}
// OrganizationMemberParam returns the organization membership that allowed the query
// from the ExtractOrganizationParam handler.
func OrganizationMemberParam(r *http.Request) database.OrganizationMember {
organizationMember, ok := r.Context().Value(organizationMemberParamContextKey{}).(database.OrganizationMember)
if !ok {
panic("developer error: organization member param middleware not provided")
2022-01-24 11:07:42 -06:00
}
return organizationMember
}
// ExtractOrganizationParam grabs an organization from the "organization" URL parameter.
2022-01-24 11:07:42 -06:00
// This middleware requires the API key middleware higher in the call stack for authentication.
func ExtractOrganizationParam(db database.Store) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2022-09-21 17:07:00 -05:00
ctx := r.Context()
orgID, ok := ParseUUIDParam(rw, r, "organization")
2022-04-01 14:42:36 -05:00
if !ok {
2022-01-24 11:07:42 -06:00
return
}
2022-04-01 14:42:36 -05:00
2022-09-21 17:07:00 -05:00
organization, err := db.GetOrganizationByID(ctx, orgID)
if httpapi.Is404Error(err) {
2022-06-14 10:14:05 -05:00
httpapi.ResourceNotFound(rw)
2022-01-24 11:07:42 -06:00
return
}
if err != nil {
2022-09-21 17:07:00 -05:00
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching organization.",
Detail: err.Error(),
2022-01-24 11:07:42 -06:00
})
return
}
2022-09-21 17:07:00 -05:00
ctx = context.WithValue(ctx, organizationParamContextKey{}, organization)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
}
// ExtractOrganizationMemberParam grabs a user membership from the "organization" and "user" URL parameter.
// This middleware requires the ExtractUser and ExtractOrganization middleware higher in the stack
func ExtractOrganizationMemberParam(db database.Store) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2022-09-21 17:07:00 -05:00
ctx := r.Context()
organization := OrganizationParam(r)
user := UserParam(r)
2022-04-01 14:42:36 -05:00
2022-09-21 17:07:00 -05:00
organizationMember, err := db.GetOrganizationMemberByUserID(ctx, database.GetOrganizationMemberByUserIDParams{
2022-01-24 11:07:42 -06:00
OrganizationID: organization.ID,
UserID: user.ID,
2022-01-24 11:07:42 -06:00
})
if httpapi.Is404Error(err) {
2022-06-14 10:14:05 -05:00
httpapi.ResourceNotFound(rw)
2022-01-24 11:07:42 -06:00
return
}
if err != nil {
2022-09-21 17:07:00 -05:00
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching organization member.",
Detail: err.Error(),
2022-01-24 11:07:42 -06:00
})
return
}
2022-09-21 17:07:00 -05:00
ctx = context.WithValue(ctx, organizationMemberParamContextKey{}, organizationMember)
2022-01-24 11:07:42 -06:00
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
}