The storage and wire contract already key resources by (org, kind), but no caller sent an org, so every request acted in the default org. The org now travels on the request context: webui's authorize middleware attaches the verified principal's org, and one request-constructor per wire shape (controlplanerpc's Query/History/Watch/CommandRequest) stamps it back, so a call site cannot send the wrong org and a new one cannot forget it. No behaviour change today: the shared token's principal is the default org's owner, no production path creates another org, and a context with no principal yields DefaultOrgID (what the server already derived from an empty org_id).
38 lines
1.6 KiB
Go
38 lines
1.6 KiB
Go
package controlplanerpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// Every control-plane request acts in one org's copy of a resource. The org
|
|
// travels on the request context (org.WithOrg), attached once by whatever
|
|
// established the caller's principal, and these constructors are the one place
|
|
// it is read back: a call site cannot send the wrong org, and a new call site
|
|
// cannot forget it. org.OrgFromContext yields the default org when the context
|
|
// carries none, so a process with no principal still acts where it always did.
|
|
|
|
// QueryRequest builds a resource query for the caller's org.
|
|
func QueryRequest(ctx context.Context, kind string) *pb.QueryRequest {
|
|
return &pb.QueryRequest{Kind: kind, OrgId: string(org.OrgFromContext(ctx))}
|
|
}
|
|
|
|
// HistoryRequest builds an audit-history request for the caller's org.
|
|
func HistoryRequest(ctx context.Context, kind string) *pb.HistoryRequest {
|
|
return &pb.HistoryRequest{Kind: kind, OrgId: string(org.OrgFromContext(ctx))}
|
|
}
|
|
|
|
// WatchRequest builds a resource watch for the caller's org.
|
|
func WatchRequest(ctx context.Context, kind string, afterVersion int64) *pb.WatchRequest {
|
|
return &pb.WatchRequest{Kind: kind, AfterVersion: afterVersion, OrgId: string(org.OrgFromContext(ctx))}
|
|
}
|
|
|
|
// CommandRequest stamps the caller's org onto a command request the caller
|
|
// assembled, whose remaining fields differ per command.
|
|
func CommandRequest(ctx context.Context, request *pb.CommandRequest) *pb.CommandRequest {
|
|
request.OrgId = string(org.OrgFromContext(ctx))
|
|
return request
|
|
}
|