feat: add member candidates endpoint and approvals page

- serve active identities outside the org at /api/orgs/{id}/member-candidates and back the members page from it
- add an org approvals page for default owner, deadline and escalation path over /api/settings
This commit is contained in:
sam committed 2026-11-22 23:03:01 +11:00
1 parent e801306c15
commit 33f7ebdff4
11 files changed
+398 -22

No files matched your search

+42
View File
@@ -20,6 +20,7 @@ type memberDirectory interface {
EnsureMembership(context.Context, Membership) error
RemoveMembership(context.Context, Membership) error
Get(context.Context, IdentityID) (Identity, error)
ListInstanceIdentities(context.Context) ([]Identity, error)
Apply(context.Context, IdentityID, int64, IdentityCommand, IdentityAudit) (Identity, error)
}
@@ -51,6 +52,17 @@ type membersBody struct {
Members []Member `json:"members"`
}
// memberCandidate is the part of an identity needed to choose it as a member.
type memberCandidate struct {
ID IdentityID `json:"id" doc:"Identity ID"`
Kind Kind `json:"kind" doc:"Identity kind"`
DisplayName string `json:"display_name" doc:"Name shown for the identity"`
}
type candidatesBody struct {
Identities []memberCandidate `json:"identities"`
}
type membershipBody struct {
Membership Membership `json:"membership"`
}
@@ -98,6 +110,7 @@ func (m *Module) RegisterAccess(api huma.API) {
}
a := m.members()
httpapi.Register(api, httpapi.Authorized(ActionRead, KindMember), op("list-org-members", http.MethodGet, "/api/orgs/{id}/members", "List an organisation's members", 0), a.list)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("list-org-member-candidates", http.MethodGet, "/api/orgs/{id}/member-candidates", "List active identities that can be added as members", 0), a.candidates)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("add-org-member", http.MethodPost, "/api/orgs/{id}/members", "Add a person by email or provider subject", http.StatusCreated), a.add)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("set-org-member", http.MethodPut, "/api/orgs/{id}/members/{identity}", "Give a member a role", 0), a.set)
httpapi.Register(api, httpapi.Authorized(ActionManageMembers, KindMember), op("remove-org-member", http.MethodDelete, "/api/orgs/{id}/members/{identity}", "Remove a membership", http.StatusNoContent), a.remove)
@@ -118,6 +131,35 @@ func (a *memberAPI) list(ctx context.Context, in *orgPath) (*httpapi.Reply[membe
return httpapi.OK(membersBody{Members: values}), nil
}
// candidates lists the instance's active identities that have no
// organisation-wide membership. These are the identities offered when adding a
// member.
// The system identity is excluded because it cannot be a member.
func (a *memberAPI) candidates(ctx context.Context, in *orgPath) (*httpapi.Reply[candidatesBody], error) {
if _, err := actingOrg(ctx, OrgID(in.ID)); err != nil {
return nil, err
}
members, err := a.dir.ListMembers(ctx, OrgID(in.ID))
if err != nil {
return nil, a.failed(ctx, "read the members", err)
}
all, err := a.dir.ListInstanceIdentities(ctx)
if err != nil {
return nil, a.failed(ctx, "read the identities", err)
}
out := make([]memberCandidate, 0, len(all))
for _, value := range all {
if value.Kind == KindSystem || value.Lifecycle != LifecycleActive {
continue
}
if _, held := roleHeld(members, value.ID, ""); held {
continue
}
out = append(out, memberCandidate{ID: value.ID, Kind: value.Kind, DisplayName: value.DisplayName})
}
return httpapi.OK(candidatesBody{Identities: out}), nil
}
func (a *memberAPI) set(ctx context.Context, in *setMemberInput) (*httpapi.Reply[membershipBody], error) {
org, target := OrgID(in.ID), IdentityID(in.Identity)
p, err := actingOrg(ctx, org)
+66 -4
View File
@@ -5,6 +5,7 @@ import (
"errors"
"log/slog"
"net/http"
"slices"
"strings"
"testing"
@@ -15,8 +16,9 @@ import (
// fakeDirectory records the changes a member route lets through.
type fakeDirectory struct {
members []Member
changes []string
members []Member
identities []Identity
changes []string
}
func (f *fakeDirectory) ListMembers(context.Context, OrgID) ([]Member, error) { return f.members, nil }
@@ -35,6 +37,10 @@ func (f *fakeDirectory) Get(_ context.Context, id IdentityID) (Identity, error)
return Identity{ID: id, Version: 1}, nil
}
func (f *fakeDirectory) ListInstanceIdentities(context.Context) ([]Identity, error) {
return f.identities, nil
}
func (f *fakeDirectory) Apply(_ context.Context, id IdentityID, _ int64, c IdentityCommand, _ IdentityAudit) (Identity, error) {
f.changes = append(f.changes, string(c.Type)+" "+string(id))
return Identity{ID: id}, nil
@@ -83,6 +89,7 @@ func TestAccessWritesNeedTheirGrant(t *testing.T) {
writes := 0
for _, route := range httpapi.Routes(api) {
mutates := route.Method != http.MethodGet
candidates := route.ID == "list-org-member-candidates"
action, kind, ok := route.Access.Declared()
if !ok {
t.Errorf("%s declares no access", route.ID)
@@ -95,7 +102,7 @@ func TestAccessWritesNeedTheirGrant(t *testing.T) {
if strings.HasPrefix(route.Path, "/api/access/policies") {
wantAction = string(ActionManagePolicies)
}
if mutates && action != wantAction {
if (mutates || candidates) && action != wantAction {
t.Errorf("%s declares %q, want %q", route.ID, action, wantAction)
}
if !mutates && route.Path == "/api/access/denials" && action != string(ActionReadAudit) {
@@ -105,7 +112,7 @@ func TestAccessWritesNeedTheirGrant(t *testing.T) {
if !engine.Authorize(t.Context(), actor("admin", RoleAdmin), Action(action), resource, AccessContext{}).Allowed {
t.Errorf("%s: an admin is refused %s %s", route.ID, action, kind)
}
if mutates || route.Path == "/api/access/denials" {
if mutates || candidates || route.Path == "/api/access/denials" {
for _, role := range []Role{RoleViewer, RoleDeveloper} {
if engine.Authorize(t.Context(), actor("lowly", role), Action(action), resource, AccessContext{}).Allowed {
t.Errorf("%s: a %s is allowed %s %s", route.ID, role, action, kind)
@@ -118,6 +125,61 @@ func TestAccessWritesNeedTheirGrant(t *testing.T) {
}
}
// TestMemberCandidates guards the listing offered when adding a member: it is
// limited to active identities outside the organisation, never reaches another
// organisation, and never offers the system identity.
func TestMemberCandidates(t *testing.T) {
identities := []Identity{
{ID: "root", Kind: KindUser, DisplayName: "Root", Lifecycle: LifecycleActive},
{ID: "newcomer", Kind: KindUser, DisplayName: "Newcomer", Lifecycle: LifecycleActive},
{ID: "scoped", Kind: KindUser, DisplayName: "Scoped", Lifecycle: LifecycleActive},
{ID: "gone", Kind: KindUser, DisplayName: "Gone", Lifecycle: LifecycleSuspended},
{ID: SystemID, Kind: KindSystem, DisplayName: "System", Lifecycle: LifecycleActive},
}
members := []Member{
member("root", RoleOwner, LifecycleActive),
{IdentityID: "scoped", Kind: KindUser, WorkspaceID: "ws", Role: RoleViewer, Status: LifecycleActive},
}
foreign := actor("outsider", RoleOwner)
foreign.Org = "other"
foreign.Memberships = []Membership{{IdentityID: "outsider", OrgID: "other", Role: RoleOwner}}
tests := []struct {
name string
by *Principal
org OrgID
want int
ids []IdentityID
}{
{"owner sees identities outside the org", new(actor("root", RoleOwner)), DefaultOrgID, http.StatusOK, []IdentityID{"newcomer", "scoped"}},
{"member of another org", &foreign, DefaultOrgID, http.StatusNotFound, nil},
{"owner of another org asks for its own org", &foreign, "other", http.StatusOK, []IdentityID{"newcomer", "scoped"}},
{"caller without a session", nil, DefaultOrgID, http.StatusUnauthorized, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := &fakeDirectory{members: members, identities: identities}
ctx := t.Context()
if tt.by != nil {
ctx = WithPrincipal(ctx, *tt.by)
}
reply, err := memberRoutes(dir).candidates(ctx, &orgPath{ID: string(tt.org)})
if got := status(t, err); got != tt.want {
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
}
if err != nil {
return
}
var got []IdentityID
for _, c := range reply.Body.Identities {
got = append(got, c.ID)
}
if !slices.Equal(got, tt.ids) {
t.Fatalf("candidates = %v, want %v", got, tt.ids)
}
})
}
}
// TestLastOwnerIsKept guards the rule that an organisation always keeps an
// active owner: removing, demoting or suspending the only one is refused before
// anything changes.
+14
View File
@@ -115,6 +115,20 @@ func (m *Module) List(ctx context.Context) ([]Identity, error) {
return out, nil
}
// ListInstanceIdentities returns every identity on the instance, regardless of
// the caller's organisation scope.
func (m *Module) ListInstanceIdentities(ctx context.Context) ([]Identity, error) {
rows, err := m.store.ListIdentities(ctx, nil)
if err != nil {
return nil, err
}
out := make([]Identity, 0, len(rows))
for _, r := range rows {
out = append(out, identityFromRow(r))
}
return out, nil
}
// Get returns one identity by ID if visible to the caller's scope.
func (m *Module) Get(ctx context.Context, id IdentityID) (Identity, error) {
row, err := m.store.GetVisibleIdentity(ctx, string(id), scopeOrgFilter(ctx))