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

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:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-23 22:38:04 +01:00
1 parent 8d7c1f856c
commit db3ff19983
23 files changed
+558 -115

No files matched your search

+79
View File
@@ -3,6 +3,7 @@ package api
import (
"errors"
"net/http"
"slices"
"strings"
"time"
@@ -495,3 +496,81 @@ func (s *Server) updatePolicy(c *gin.Context) {
}
var errNoSSOProvider = errors.New("api: no enabled sso provider")
// adminRole is the built-in Admin role.
var adminRole = uuid.MustParse("00000000-0000-0000-0000-00000000a001")
type ownerRequest struct {
Owner bool `json:"owner"`
}
// setOwner makes a member an owner or takes it back, the actor's own
// ownership included: handing an organization over is making someone else
// owner and stepping down. Only owners decide, and the last owner stays.
// A former owner without roles keeps working as Admin.
func (s *Server) setOwner(c *gin.Context) {
target, err := uuid.Parse(c.Param("user"))
if err != nil {
fail(c, http.StatusNotFound, "not_found")
return
}
var req ownerRequest
if !bind(c, &req) {
return
}
p := current(c)
if !p.Grants.Owner {
fail(c, http.StatusForbidden, "owner_only")
return
}
auditDetail(c, "user_id", target.String())
auditDetail(c, "owner", req.Owner)
ctx := c.Request.Context()
var code string
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
if err := q.LockOrgOwners(ctx, p.Org.ID); err != nil {
return err
}
m, err := q.GetMembership(ctx, store.GetMembershipParams{OrgID: p.Org.ID, UserID: target})
switch {
case errors.Is(err, pgx.ErrNoRows):
code = "not_found"
return nil
case err != nil:
return err
case m.IsOwner == req.Owner:
return nil
}
if !req.Owner {
n, err := q.CountOwners(ctx, p.Org.ID)
if err != nil {
return err
}
if n <= 1 {
code = "last_owner"
return nil
}
assigned, err := q.ListAssignments(ctx, p.Org.ID)
if err != nil {
return err
}
if !slices.ContainsFunc(assigned, func(a store.ListAssignmentsRow) bool { return a.UserID == target }) {
if _, err := q.CreateAssignment(ctx, store.CreateAssignmentParams{OrgID: p.Org.ID, UserID: target, RoleID: adminRole}); err != nil {
return err
}
}
}
_, err = q.SetOwner(ctx, store.SetOwnerParams{OrgID: p.Org.ID, UserID: target, Owner: req.Owner})
return err
})
switch {
case err != nil:
internalError(c, err)
case code == "not_found":
fail(c, http.StatusNotFound, code)
case code != "":
fail(c, http.StatusConflict, code)
default:
c.Status(http.StatusNoContent)
}
}