package daemon import ( "context" "fmt" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/taskstate" ) // workflowContext forwards a task's identity so the State Store selects its // org's definitions. An identity assigned elsewhere cannot supply the pin. func (g *IdentityGate) workflowContext(ctx context.Context, task *workflow.Task) (context.Context, error) { owner := task.Org if owner == "" { owner = org.DefaultOrgID } principal, err := g.principalIn(ctx, identity.IdentityID(task.Identity), owner) if err != nil { return nil, pinFailure{taskstate.ParkTransient, fmt.Errorf("workflow principal: %w", err)} } if principal.Org != owner { return nil, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("workflow identity belongs to org %s, task belongs to %s", principal.Org, owner)} } return org.WithOrg(access.WithPrincipal(ctx, principal), owner), nil } // principalIn is id's principal acting in owner: the org it serves, or one it // is a member of, since a person's task belongs to the org they selected. An // identity that is no member keeps its own org, which callers refuse. func (g *IdentityGate) principalIn(ctx context.Context, id identity.IdentityID, owner org.OrgID) (access.Principal, error) { principal, err := g.principals.PrincipalFor(ctx, id) if err != nil { return access.Principal{}, err } if principal.Org == "" { principal.Org = org.DefaultOrgID } if principal.Org == owner || !principal.Member(owner) { return principal, nil } return principal.Select(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org), owner, g.workspaces) }