feat: several owners per organization, and handing ownership over
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m19s
ci / test (pull_request) Successful in 9m25s
ci / build (pull_request) Successful in 15m40s
ci / image (pull_request) Successful in 4m26s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m19s
ci / test (pull_request) Successful in 9m25s
ci / build (pull_request) Successful in 15m40s
ci / image (pull_request) Successful in 4m26s
An organization may now have several owners; the unique index allowing only one is gone. Owners make members owners or take it back, their own included, through PUT /api/v1/org/members/:user/owner; handing an organization over is making someone else owner and stepping down. The last owner cannot step down, and changes serialize on a per-organization lock so two owners stepping down at once cannot leave none. A former owner without roles keeps working as Admin. Owners still cannot be removed while they are owners. Deleting an account now takes along only the organizations it is the last owner of, counted under the same lock; it just leaves those with further owners. The members page offers "Make owner", "Remove as owner" and "Step down as owner" to owners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
8d7c1f856c
commit
db3ff19983
23 files changed
+558
-115
No files matched your search
@@ -260,7 +260,8 @@ func (q *Queries) OrgMembersExport(ctx context.Context, orgID uuid.UUID) ([]OrgM
|
||||
|
||||
const ownedOrgs = `-- name: OwnedOrgs :many
|
||||
SELECT o.id, o.slug, o.name,
|
||||
(SELECT count(*) FROM memberships m2 WHERE m2.org_id = o.id)::bigint AS members
|
||||
(SELECT count(*) FROM memberships m2 WHERE m2.org_id = o.id)::bigint AS members,
|
||||
(SELECT count(*) FROM memberships m3 WHERE m3.org_id = o.id AND m3.is_owner)::bigint AS owners
|
||||
FROM orgs o JOIN memberships m ON m.org_id = o.id
|
||||
WHERE m.user_id = $1 AND m.is_owner
|
||||
ORDER BY o.name
|
||||
@@ -271,9 +272,10 @@ type OwnedOrgsRow struct {
|
||||
Slug string
|
||||
Name string
|
||||
Members int64
|
||||
Owners int64
|
||||
}
|
||||
|
||||
// Organizations user owns, with their member count.
|
||||
// Organizations user owns, with their member and owner counts.
|
||||
func (q *Queries) OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error) {
|
||||
rows, err := q.db.Query(ctx, ownedOrgs, userID)
|
||||
if err != nil {
|
||||
@@ -288,6 +290,7 @@ func (q *Queries) OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsR
|
||||
&i.Slug,
|
||||
&i.Name,
|
||||
&i.Members,
|
||||
&i.Owners,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -27,6 +27,17 @@ func (q *Queries) AddMembership(ctx context.Context, arg AddMembershipParams) er
|
||||
return err
|
||||
}
|
||||
|
||||
const countOwners = `-- name: CountOwners :one
|
||||
SELECT count(*) FROM memberships WHERE org_id = $1 AND is_owner
|
||||
`
|
||||
|
||||
func (q *Queries) CountOwners(ctx context.Context, orgID uuid.UUID) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countOwners, orgID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const getMembership = `-- name: GetMembership :one
|
||||
SELECT org_id, user_id, is_owner, created_at FROM memberships WHERE org_id = $1 AND user_id = $2
|
||||
`
|
||||
@@ -196,6 +207,17 @@ func (q *Queries) ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUse
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockOrgOwners = `-- name: LockOrgOwners :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('owners:' || $1::uuid::text, 0))
|
||||
`
|
||||
|
||||
// Serializes ownership changes of an organization, so two owners stepping
|
||||
// down at once cannot leave it without one.
|
||||
func (q *Queries) LockOrgOwners(ctx context.Context, orgID uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, lockOrgOwners, orgID)
|
||||
return err
|
||||
}
|
||||
|
||||
const removeMembership = `-- name: RemoveMembership :execrows
|
||||
DELETE FROM memberships WHERE org_id = $1 AND user_id = $2 AND NOT is_owner
|
||||
`
|
||||
@@ -212,3 +234,21 @@ func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipPara
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setOwner = `-- name: SetOwner :execrows
|
||||
UPDATE memberships SET is_owner = $1 WHERE org_id = $2 AND user_id = $3
|
||||
`
|
||||
|
||||
type SetOwnerParams struct {
|
||||
Owner bool
|
||||
OrgID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) SetOwner(ctx context.Context, arg SetOwnerParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, setOwner, arg.Owner, arg.OrgID, arg.UserID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -50,6 +50,7 @@ type Querier interface {
|
||||
ClearStatusPageMonitors(ctx context.Context, pageID uuid.UUID) error
|
||||
CountMonitors(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountOwnedOrgs(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
CountOwners(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
|
||||
CountStatusPages(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountUnusedRecoveryCodes(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
@@ -231,6 +232,9 @@ type Querier interface {
|
||||
// Row-level security limits this to platform regions and the scope's org;
|
||||
// the org's own regions come first.
|
||||
ListVisibleRegions(ctx context.Context) ([]Region, error)
|
||||
// Serializes ownership changes of an organization, so two owners stepping
|
||||
// down at once cannot leave it without one.
|
||||
LockOrgOwners(ctx context.Context, orgID uuid.UUID) error
|
||||
// Serializes creations counted against org's limits until the transaction
|
||||
// ends.
|
||||
LockOrgUsage(ctx context.Context, orgID uuid.UUID) error
|
||||
@@ -265,7 +269,7 @@ type Querier interface {
|
||||
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||
OrgIncidentsExport(ctx context.Context, orgID uuid.UUID) ([]OrgIncidentsExportRow, error)
|
||||
OrgMembersExport(ctx context.Context, orgID uuid.UUID) ([]OrgMembersExportRow, error)
|
||||
// Organizations user owns, with their member count.
|
||||
// Organizations user owns, with their member and owner counts.
|
||||
OwnedOrgs(ctx context.Context, userID uuid.UUID) ([]OwnedOrgsRow, error)
|
||||
// Resolved and long silent alerts are history the monitors' results keep.
|
||||
PurgeAlertmanagerAlerts(ctx context.Context, updatedAt time.Time) (int64, error)
|
||||
@@ -309,6 +313,7 @@ type Querier interface {
|
||||
SetAgentAllowPrivate(ctx context.Context, arg SetAgentAllowPrivateParams) (int64, error)
|
||||
SetOrgDisabledCheckTypes(ctx context.Context, arg SetOrgDisabledCheckTypesParams) error
|
||||
SetOrgLimits(ctx context.Context, arg SetOrgLimitsParams) (SetOrgLimitsRow, error)
|
||||
SetOwner(ctx context.Context, arg SetOwnerParams) (int64, error)
|
||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||
SetPlatformSetting(ctx context.Context, arg SetPlatformSettingParams) error
|
||||
// Changing the domain drops a previous verification.
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
-- name: OwnedOrgs :many
|
||||
-- Organizations user owns, with their member count.
|
||||
-- Organizations user owns, with their member and owner counts.
|
||||
SELECT o.id, o.slug, o.name,
|
||||
(SELECT count(*) FROM memberships m2 WHERE m2.org_id = o.id)::bigint AS members
|
||||
(SELECT count(*) FROM memberships m2 WHERE m2.org_id = o.id)::bigint AS members,
|
||||
(SELECT count(*) FROM memberships m3 WHERE m3.org_id = o.id AND m3.is_owner)::bigint AS owners
|
||||
FROM orgs o JOIN memberships m ON m.org_id = o.id
|
||||
WHERE m.user_id = $1 AND m.is_owner
|
||||
ORDER BY o.name;
|
||||
|
||||
@@ -4,6 +4,17 @@ INSERT INTO memberships (org_id, user_id, is_owner) VALUES ($1, $2, $3);
|
||||
-- name: RemoveMembership :execrows
|
||||
DELETE FROM memberships WHERE org_id = $1 AND user_id = $2 AND NOT is_owner;
|
||||
|
||||
-- name: LockOrgOwners :exec
|
||||
-- Serializes ownership changes of an organization, so two owners stepping
|
||||
-- down at once cannot leave it without one.
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('owners:' || sqlc.arg(org_id)::uuid::text, 0));
|
||||
|
||||
-- name: CountOwners :one
|
||||
SELECT count(*) FROM memberships WHERE org_id = $1 AND is_owner;
|
||||
|
||||
-- name: SetOwner :execrows
|
||||
UPDATE memberships SET is_owner = sqlc.arg(owner) WHERE org_id = sqlc.arg(org_id) AND user_id = sqlc.arg(user_id);
|
||||
|
||||
-- name: ListOrgMembers :many
|
||||
SELECT u.id, u.email, u.display_name, m.is_owner, m.created_at AS joined_at
|
||||
FROM memberships m
|
||||
|
||||
Reference in new issue
Block a user