fix(webui): resolve a selected org's workspaces as the caller, wire selection into gateway and harness, and let a member start tasks in the org they selected

This commit is contained in:
sam committed 2026-10-27 11:06:18 +11:00
1 parent c3d49cbc6c
commit 4bb473c0b0
12 files changed
+101 -21

No files matched your search

+1 -1
View File
@@ -131,7 +131,7 @@ func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id ide
if d.Access == nil || d.Principals == nil {
return true, nil
}
principal, err := d.Principals.PrincipalFor(ctx, id)
principal, err := d.principalIn(ctx, id, task.Org)
if err != nil {
d.Log.WarnContext(ctx, "workflow identity: principal unavailable", "task", task.ID, "identity", id, "err", err)
return false, nil
+20 -4
View File
@@ -5,6 +5,7 @@ import (
"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"
@@ -24,15 +25,30 @@ func (d *Daemon) workflowContext(ctx context.Context, task *workflow.Task) (cont
}
return ctx, nil
}
principal, err := d.Principals.PrincipalFor(ctx, d.publisherIdentity(task.Identity))
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 == "" {
principal.Org = org.DefaultOrgID
}
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)
}
+18 -2
View File
@@ -30,6 +30,20 @@ func (p workflowPrincipals) PrincipalFor(_ context.Context, id identity.Identity
return access.Principal{IdentityID: id, Org: owner}, nil
}
// memberOf is a person who serves one org and holds a membership in another.
type memberOf struct{ home, other org.OrgID }
func (m memberOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: m.home, Memberships: []org.Membership{{IdentityID: id, OrgID: m.home}, {IdentityID: id, OrgID: m.other}}}, nil
}
// listsWorkspaces lets a principal select an org, as the State Store client does.
type listsWorkspaces struct{ *identityStore }
func (listsWorkspaces) ListWorkspaces(context.Context, org.OrgID) ([]org.Workspace, error) {
return nil, nil
}
type orgBindingStore struct {
storecontract.TaskStore
storecontract.BindingStore
@@ -122,7 +136,7 @@ func TestPinWorkflowInTaskOrg(t *testing.T) {
for _, tc := range []struct {
name string
owner org.OrgID
principals workflowPrincipals
principals access.PrincipalSource
want string
refused bool
contextOrg org.OrgID
@@ -130,11 +144,13 @@ func TestPinWorkflowInTaskOrg(t *testing.T) {
{"legacy default org", "", nil, shared, false, ""},
{"non-default org definition", "acme", workflowPrincipals{"bot": "acme"}, custom, false, ""},
{"another org cannot supply the definition", "acme", workflowPrincipals{"bot": org.DefaultOrgID}, "", true, ""},
{"a member of the task's org may start it from another", "acme", memberOf{home: org.DefaultOrgID, other: "acme"}, custom, false, ""},
{"a non-member cannot start a task in another org", "acme", memberOf{home: org.DefaultOrgID, other: "beta"}, "", true, ""},
{"unresolved org is refused", "acme", nil, "", true, ""},
{"unresolved context cannot select another org", "", nil, "", true, "acme"},
} {
t.Run(tc.name, func(t *testing.T) {
d := &Daemon{Store: &identityStore{}, RootIdentityID: "bot", WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}}
d := &Daemon{Store: listsWorkspaces{&identityStore{}}, RootIdentityID: "bot", WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}}
if tc.principals != nil {
d.Principals = tc.principals
}