feat(webui): act in a selected organisation the caller is a member of, enforced at the dashboard and the State Store

This commit is contained in:
sam committed 2026-10-27 11:06:18 +11:00
1 parent 2cf9d435cc
commit 2fb0084d2c
17 files changed
+356 -69

No files matched your search

+41
View File
@@ -2,6 +2,7 @@ package access
import (
"context"
"errors"
"time"
"github.com/samcharles93/archie-core/internal/domain/identity"
@@ -98,6 +99,46 @@ func (p Principal) InstanceAdmin() bool {
return false
}
// ErrNotMember refuses selecting an org the principal holds no membership in.
var ErrNotMember = errors.New("not a member of that org")
// WorkspaceLister reads an org's workspaces.
type WorkspaceLister interface {
ListWorkspaces(context.Context, org.OrgID) ([]org.Workspace, error)
}
// Select returns the principal acting in org id, which it must hold a
// membership in. Selecting the org it already serves needs no lookup. Every
// authority that honours a caller's chosen org goes through here, so the
// membership check is the single gate.
func (p Principal) Select(ctx context.Context, id org.OrgID, workspaces WorkspaceLister) (Principal, error) {
if id == "" || id == p.Org {
return p, nil
}
if !p.Member(id) {
return Principal{}, ErrNotMember
}
if workspaces == nil {
return Principal{}, errors.New("org selection unavailable")
}
list, err := workspaces.ListWorkspaces(ctx, id)
if err != nil {
return Principal{}, err
}
p.Org, p.Workspaces = id, list
return p, nil
}
// Member reports whether the principal holds any membership in org id.
func (p Principal) Member(id org.OrgID) bool {
for _, m := range p.Memberships {
if m.OrgID == id {
return true
}
}
return false
}
// Resource is the record acted on: its type, its workspace and org as
// parents, its owner and its state.
type Resource struct {