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
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (m *Memory) CreateCapabilityRequest(_ context.Context, r CapabilityRequest) (*CapabilityRequest, bool, error) {
|
|
if !r.valid() {
|
|
return nil, false, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[r.TenantID]; !ok {
|
|
return nil, false, ErrNotFound
|
|
}
|
|
for _, cur := range m.capRequests {
|
|
if cur.TenantID == r.TenantID && cur.UserID == r.UserID && cur.Capability == r.Capability {
|
|
cp := cur
|
|
return &cp, false, nil
|
|
}
|
|
}
|
|
r.ID = uuid.NewString()
|
|
r.CreatedAt = time.Now().UTC().Truncate(time.Microsecond)
|
|
m.capRequests = append(m.capRequests, r)
|
|
cp := r
|
|
return &cp, true, nil
|
|
}
|
|
|
|
func (m *Memory) ListCapabilityRequests(_ context.Context, tenantID string) ([]CapabilityRequest, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []CapabilityRequest{}
|
|
for _, r := range m.capRequests {
|
|
if r.TenantID == tenantID {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) GetCapabilityRequest(_ context.Context, tenantID, id string) (*CapabilityRequest, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
for _, r := range m.capRequests {
|
|
if r.TenantID == tenantID && r.ID == id {
|
|
cp := r
|
|
return &cp, nil
|
|
}
|
|
}
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
func (m *Memory) DeleteCapabilityRequest(_ context.Context, tenantID, id string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
n := len(m.capRequests)
|
|
m.capRequests = slices.DeleteFunc(m.capRequests, func(r CapabilityRequest) bool { return r.TenantID == tenantID && r.ID == id })
|
|
if len(m.capRequests) == n {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Memory) DeleteMemberCapabilityRequests(_ context.Context, tenantID, userID, capability string) (int, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
n := len(m.capRequests)
|
|
m.capRequests = slices.DeleteFunc(m.capRequests, func(r CapabilityRequest) bool {
|
|
return r.TenantID == tenantID && r.UserID == userID && (capability == "" || r.Capability == capability)
|
|
})
|
|
return n - len(m.capRequests), nil
|
|
}
|