fix(postgres): keep every organisation an active owner
This commit is contained in:
8 files changed
+95
-4
No files matched your search
@@ -174,6 +174,11 @@ func (s *Store) Apply(ctx context.Context, id identity.IdentityID, expectedVersi
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
next.UpdatedAt = now
|
||||
if cur.Lifecycle == identity.LifecycleActive && next.Lifecycle != identity.LifecycleActive {
|
||||
if err := keepActiveOwner(ctx, tx, string(id), ""); err != nil {
|
||||
return identity.Identity{}, err
|
||||
}
|
||||
}
|
||||
|
||||
n, err := q.UpdateIdentity(ctx, postgresdb.UpdateIdentityParams{
|
||||
ID: string(id), DisplayName: next.DisplayName, Lifecycle: string(next.Lifecycle),
|
||||
|
||||
@@ -140,11 +140,17 @@ func (s *Store) EnsureMembership(ctx context.Context, value org.Membership) erro
|
||||
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.queries().EnsureMembership(ctx, postgresdb.EnsureMembershipParams{
|
||||
params := postgresdb.EnsureMembershipParams{
|
||||
IdentityID: string(value.IdentityID),
|
||||
OrgID: string(value.OrgID),
|
||||
WorkspaceID: workspace,
|
||||
Role: string(value.Role),
|
||||
}
|
||||
if value.WorkspaceID != "" || value.Role == org.RoleOwner {
|
||||
return s.queries().EnsureMembership(ctx, params)
|
||||
}
|
||||
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
||||
return q.EnsureMembership(ctx, params)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -187,8 +193,14 @@ func (s *Store) RemoveMembership(ctx context.Context, value org.Membership) erro
|
||||
if value.IdentityID == "" || value.OrgID == "" {
|
||||
return fmt.Errorf("%w: identity and org are required", org.ErrInvalidMembership)
|
||||
}
|
||||
return s.queries().RemoveMembership(ctx, postgresdb.RemoveMembershipParams{
|
||||
params := postgresdb.RemoveMembershipParams{
|
||||
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID), WorkspaceID: string(value.WorkspaceID),
|
||||
}
|
||||
if value.WorkspaceID != "" {
|
||||
return s.queries().RemoveMembership(ctx, params)
|
||||
}
|
||||
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
||||
return q.RemoveMembership(ctx, params)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// keepActiveOwner refuses, inside tx, a change after which an org leaving owns
|
||||
// org-wide (only orgID, when it is set) has no other active owner. The owner rows are locked first, so two
|
||||
// owners suspending or removing each other concurrently serialise and the
|
||||
// second sees the first's change.
|
||||
func keepActiveOwner(ctx context.Context, tx pgx.Tx, leaving, orgID string) error {
|
||||
rows, err := tx.Query(ctx, `SELECT org_id FROM memberships
|
||||
WHERE identity_id = $1 AND workspace_id IS NULL AND role = 'owner' AND ($2 = '' OR org_id = $2)`, leaving, orgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
orgIDs, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(orgIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `SELECT 1 FROM memberships
|
||||
WHERE org_id = ANY($1) AND workspace_id IS NULL AND role = 'owner'
|
||||
ORDER BY org_id, identity_id FOR UPDATE`, orgIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
var stranded int
|
||||
err = tx.QueryRow(ctx, `SELECT count(*) FROM unnest($1::text[]) AS o(org_id)
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM memberships m JOIN identities i ON i.id = m.identity_id
|
||||
WHERE m.org_id = o.org_id AND m.workspace_id IS NULL AND m.role = 'owner'
|
||||
AND i.lifecycle = 'active' AND m.identity_id <> $2)`, orgIDs, leaving).Scan(&stranded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if stranded > 0 {
|
||||
return org.ErrLastOwner
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// guardOwners runs fn in a transaction after keepActiveOwner allows leaving
|
||||
// to stop owning orgID.
|
||||
func (s *Store) guardOwners(ctx context.Context, leaving, orgID string, fn func(*postgresdb.Queries) error) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
if err := keepActiveOwner(ctx, tx, leaving, orgID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fn(postgresdb.New(tx)); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
Reference in new issue
Block a user