55 lines
2.2 KiB
Go
55 lines
2.2 KiB
Go
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 (d *Daemon) workflowContext(ctx context.Context, task *workflow.Task) (context.Context, error) {
|
|
owner := task.Org
|
|
if owner == "" {
|
|
owner = org.DefaultOrgID
|
|
}
|
|
if d.Principals == nil {
|
|
principal, forwarded := access.PrincipalFromContext(ctx)
|
|
if owner != org.DefaultOrgID || org.OrgFromContext(ctx) != owner || (forwarded && principal.Org != "" && principal.Org != owner) {
|
|
return nil, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("workflow org %s: principal unavailable", owner)}
|
|
}
|
|
return ctx, nil
|
|
}
|
|
principal, err := d.principalIn(ctx, d.publisherIdentity(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 (d *Daemon) principalIn(ctx context.Context, id identity.IdentityID, owner org.OrgID) (access.Principal, error) {
|
|
principal, err := d.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
|
|
}
|
|
lister, _ := d.Store.(access.WorkspaceLister)
|
|
return principal.Select(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org), owner, lister)
|
|
}
|