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).
25 lines
906 B
Go
25 lines
906 B
Go
package org
|
|
|
|
import "context"
|
|
|
|
// orgContextKey is the unexported key the acting org travels under. A named
|
|
// struct type, not a string, so no other package can collide with it.
|
|
type orgContextKey struct{}
|
|
|
|
// WithOrg attaches the org a request acts in. Every control-plane request
|
|
// reads it back with OrgFromContext, so the org reaches the wire from one
|
|
// place: the context the caller's principal put it on.
|
|
func WithOrg(ctx context.Context, id OrgID) context.Context {
|
|
return context.WithValue(ctx, orgContextKey{}, id)
|
|
}
|
|
|
|
// OrgFromContext returns the org a request acts in, or DefaultOrgID when the
|
|
// context carries none. DefaultOrgID is the single-operator install's org and
|
|
// the org every record written before orgs existed belongs to.
|
|
func OrgFromContext(ctx context.Context) OrgID {
|
|
if id, ok := ctx.Value(orgContextKey{}).(OrgID); ok && id != "" {
|
|
return id
|
|
}
|
|
return DefaultOrgID
|
|
}
|