A member can hold capabilities on top of their bundle. They live in the
Keycloak user attribute extra_capabilities, which the tenant-context
mapper puts into the token; the registry writes it like org_roles and
clears it when the member leaves.
- POST/DELETE /v1/tenants/{id}/members/{uid}/capabilities[/{cap}]
- a person who holds registry:members.manage grants, never on
themselves, and only what they hold themselves
- an administration capability waits for a second person: open
requests in capability_requests (0029), approve or reject under
/v1/tenants/{id}/capability-requests
- a role change keeps the extras; the principal parses the claim, the
registry's own checks still go by org_roles
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
const capRequestSelect = `
|
|
SELECT id::text, tenant_id::text, user_id, user_email, capability, requested_by, requested_email, created_at
|
|
FROM capability_requests `
|
|
|
|
func scanCapRequest(row pgx.Row) (*CapabilityRequest, error) {
|
|
var r CapabilityRequest
|
|
if err := row.Scan(&r.ID, &r.TenantID, &r.UserID, &r.UserEmail, &r.Capability, &r.RequestedBy, &r.RequestedEmail, &r.CreatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
r.CreatedAt = r.CreatedAt.UTC()
|
|
return &r, nil
|
|
}
|
|
|
|
func (p *Postgres) CreateCapabilityRequest(ctx context.Context, r CapabilityRequest) (*CapabilityRequest, bool, error) {
|
|
if !r.valid() {
|
|
return nil, false, ErrInvalidInput
|
|
}
|
|
got, err := scanCapRequest(p.pool.QueryRow(ctx, `
|
|
INSERT INTO capability_requests (tenant_id, user_id, user_email, capability, requested_by, requested_email)
|
|
VALUES ($1::uuid, $2, $3, $4, $5, $6)
|
|
ON CONFLICT (tenant_id, user_id, capability) DO NOTHING
|
|
RETURNING id::text, tenant_id::text, user_id, user_email, capability, requested_by, requested_email, created_at`,
|
|
r.TenantID, r.UserID, r.UserEmail, r.Capability, r.RequestedBy, r.RequestedEmail))
|
|
switch {
|
|
case err == nil:
|
|
return got, true, nil
|
|
case errors.Is(err, ErrNotFound):
|
|
// The request exists already.
|
|
cur, err := scanCapRequest(p.pool.QueryRow(ctx, capRequestSelect+`WHERE tenant_id = $1::uuid AND user_id = $2 AND capability = $3`,
|
|
r.TenantID, r.UserID, r.Capability))
|
|
return cur, false, err
|
|
case isFKViolation(err), isInvalidUUID(err):
|
|
return nil, false, ErrNotFound
|
|
case isCheckViolation(err):
|
|
return nil, false, ErrInvalidInput
|
|
default:
|
|
return nil, false, err
|
|
}
|
|
}
|
|
|
|
func (p *Postgres) ListCapabilityRequests(ctx context.Context, tenantID string) ([]CapabilityRequest, error) {
|
|
rows, err := p.pool.Query(ctx, capRequestSelect+`WHERE tenant_id = $1::uuid ORDER BY created_at, id`, tenantID)
|
|
if isInvalidUUID(err) {
|
|
return []CapabilityRequest{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []CapabilityRequest{}
|
|
for rows.Next() {
|
|
r, err := scanCapRequest(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *r)
|
|
}
|
|
if err := rows.Err(); isInvalidUUID(err) {
|
|
return []CapabilityRequest{}, nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (p *Postgres) GetCapabilityRequest(ctx context.Context, tenantID, id string) (*CapabilityRequest, error) {
|
|
r, err := scanCapRequest(p.pool.QueryRow(ctx, capRequestSelect+`WHERE tenant_id = $1::uuid AND id = $2::uuid`, tenantID, id))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
func (p *Postgres) DeleteCapabilityRequest(ctx context.Context, tenantID, id string) error {
|
|
tag, err := p.pool.Exec(ctx, `DELETE FROM capability_requests WHERE tenant_id = $1::uuid AND id = $2::uuid`, tenantID, id)
|
|
switch {
|
|
case isInvalidUUID(err):
|
|
return ErrNotFound
|
|
case err != nil:
|
|
return err
|
|
case tag.RowsAffected() == 0:
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Postgres) DeleteMemberCapabilityRequests(ctx context.Context, tenantID, userID, capability string) (int, error) {
|
|
tag, err := p.pool.Exec(ctx, `DELETE FROM capability_requests WHERE tenant_id = $1::uuid AND user_id = $2 AND ($3 = '' OR capability = $3)`,
|
|
tenantID, userID, capability)
|
|
if isInvalidUUID(err) {
|
|
return 0, nil
|
|
}
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(tag.RowsAffected()), nil
|
|
}
|