feat(rbac): lift org membership from per-user to per-tenant (Plan 3, #1303)
Before this change, the `organization_members` table keyed on user_id — joining an org as a Viewer in tenant T meant only that one user got visibility on shared KBs/agents, while the rest of T (including its Owner) saw nothing. The mismatch surfaced as confusing UX bugs: "my colleague shared this with us, why can't I see it?" This change replaces `organization_members` with `organization_tenant_members`. Membership and Org-RBAC role now key on (org_id, tenant_id); `representative_user_id` stays as a display-only label of the user who first brought the tenant in. Permission resolution gains a 3-D cap so tenant-RBAC stays honest under cross-tenant sharing: effective = min(share.Permission, tenant_org_role, tenant_role_cap) where tenant_role_cap pins tenant Viewers to OrgRoleViewer regardless of the org-level grant. A Viewer in their own tenant cannot edit a shared KB even if the org granted Editor — preserves the "Viewer = read-only everywhere" promise. Migration 000045 backfills `organization_tenant_members` from the old table by collapsing each (org, tenant) group to its max role, then renames the old table to `organization_members_pre_plan3` for safe rollback. Sqlite mirror (Lite mode) is fresh-bootstrap: the init schema replaces the old table directly. Routes: member-management URLs change from `:user_id` to `:tenant_id`. Org create / join / leave now require Admin+ in the caller's tenant — joining an Org affects everyone in T, so the decision can't sit with a Viewer. Frontend: OrganizationSettingsModal + store update to address members by tenant_id.
This commit is contained in:
@@ -18,7 +18,12 @@ var (
|
||||
ErrInviteCodeExpired = errors.New("invite code has expired")
|
||||
)
|
||||
|
||||
// organizationRepository implements OrganizationRepository interface
|
||||
// organizationRepository implements OrganizationRepository.
|
||||
//
|
||||
// Plan 3 of #1303 lifts membership from per-user to per-tenant (see
|
||||
// migration 000045). All "member" methods on this repo now operate on
|
||||
// the (org, tenant) tuple; the underlying table is
|
||||
// organization_tenant_members.
|
||||
type organizationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
@@ -60,14 +65,13 @@ func (r *organizationRepository) GetByInviteCode(ctx context.Context, inviteCode
|
||||
return &org, nil
|
||||
}
|
||||
|
||||
// ListByUserID lists organizations that a user belongs to
|
||||
func (r *organizationRepository) ListByUserID(ctx context.Context, userID string) ([]*types.Organization, error) {
|
||||
// ListByTenantID lists organizations whose tenant participates as a member.
|
||||
func (r *organizationRepository) ListByTenantID(ctx context.Context, tenantID uint64) ([]*types.Organization, error) {
|
||||
var orgs []*types.Organization
|
||||
|
||||
// Get organizations where user is a member
|
||||
err := r.db.WithContext(ctx).
|
||||
Joins("JOIN organization_members ON organization_members.organization_id = organizations.id").
|
||||
Where("organization_members.user_id = ?", userID).
|
||||
Joins("JOIN organization_tenant_members otm ON otm.organization_id = organizations.id").
|
||||
Where("otm.tenant_id = ?", tenantID).
|
||||
Order("organizations.created_at DESC").
|
||||
Find(&orgs).Error
|
||||
|
||||
@@ -108,12 +112,14 @@ func (r *organizationRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&types.Organization{}).Error
|
||||
}
|
||||
|
||||
// AddMember adds a member to an organization
|
||||
func (r *organizationRepository) AddMember(ctx context.Context, member *types.OrganizationMember) error {
|
||||
// Check if member already exists
|
||||
// AddTenantMember inserts a new (org, tenant) membership row. Returns
|
||||
// ErrOrgMemberAlreadyExists if a row already exists for this tuple — the
|
||||
// service layer treats that as a no-op when the caller is just confirming
|
||||
// an idempotent join.
|
||||
func (r *organizationRepository) AddTenantMember(ctx context.Context, member *types.OrganizationTenantMember) error {
|
||||
var count int64
|
||||
r.db.WithContext(ctx).Model(&types.OrganizationMember{}).
|
||||
Where("organization_id = ? AND user_id = ?", member.OrganizationID, member.UserID).
|
||||
r.db.WithContext(ctx).Model(&types.OrganizationTenantMember{}).
|
||||
Where("organization_id = ? AND tenant_id = ?", member.OrganizationID, member.TenantID).
|
||||
Count(&count)
|
||||
|
||||
if count > 0 {
|
||||
@@ -123,11 +129,11 @@ func (r *organizationRepository) AddMember(ctx context.Context, member *types.Or
|
||||
return r.db.WithContext(ctx).Create(member).Error
|
||||
}
|
||||
|
||||
// RemoveMember removes a member from an organization
|
||||
func (r *organizationRepository) RemoveMember(ctx context.Context, orgID string, userID string) error {
|
||||
// RemoveTenantMember removes the (org, tenant) membership row.
|
||||
func (r *organizationRepository) RemoveTenantMember(ctx context.Context, orgID string, tenantID uint64) error {
|
||||
result := r.db.WithContext(ctx).
|
||||
Where("organization_id = ? AND user_id = ?", orgID, userID).
|
||||
Delete(&types.OrganizationMember{})
|
||||
Where("organization_id = ? AND tenant_id = ?", orgID, tenantID).
|
||||
Delete(&types.OrganizationTenantMember{})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -138,11 +144,11 @@ func (r *organizationRepository) RemoveMember(ctx context.Context, orgID string,
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateMemberRole updates a member's role in an organization
|
||||
func (r *organizationRepository) UpdateMemberRole(ctx context.Context, orgID string, userID string, role types.OrgMemberRole) error {
|
||||
// UpdateTenantMemberRole updates the role for a (org, tenant) membership.
|
||||
func (r *organizationRepository) UpdateTenantMemberRole(ctx context.Context, orgID string, tenantID uint64, role types.OrgMemberRole) error {
|
||||
result := r.db.WithContext(ctx).
|
||||
Model(&types.OrganizationMember{}).
|
||||
Where("organization_id = ? AND user_id = ?", orgID, userID).
|
||||
Model(&types.OrganizationTenantMember{}).
|
||||
Where("organization_id = ? AND tenant_id = ?", orgID, tenantID).
|
||||
Update("role", role)
|
||||
|
||||
if result.Error != nil {
|
||||
@@ -154,11 +160,11 @@ func (r *organizationRepository) UpdateMemberRole(ctx context.Context, orgID str
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListMembers lists all members of an organization
|
||||
func (r *organizationRepository) ListMembers(ctx context.Context, orgID string) ([]*types.OrganizationMember, error) {
|
||||
var members []*types.OrganizationMember
|
||||
// ListTenantMembers lists all tenant memberships for an organization.
|
||||
func (r *organizationRepository) ListTenantMembers(ctx context.Context, orgID string) ([]*types.OrganizationTenantMember, error) {
|
||||
var members []*types.OrganizationTenantMember
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("User").
|
||||
Preload("RepresentativeUser").
|
||||
Where("organization_id = ?", orgID).
|
||||
Order("created_at ASC").
|
||||
Find(&members).Error
|
||||
@@ -169,11 +175,14 @@ func (r *organizationRepository) ListMembers(ctx context.Context, orgID string)
|
||||
return members, nil
|
||||
}
|
||||
|
||||
// GetMember gets a specific member of an organization
|
||||
func (r *organizationRepository) GetMember(ctx context.Context, orgID string, userID string) (*types.OrganizationMember, error) {
|
||||
var member types.OrganizationMember
|
||||
// GetTenantMember returns the (org, tenant) membership row, or
|
||||
// ErrOrgMemberNotFound when missing. This is the canonical permission
|
||||
// check primitive — callers compose this with the share permission
|
||||
// to compute effective access.
|
||||
func (r *organizationRepository) GetTenantMember(ctx context.Context, orgID string, tenantID uint64) (*types.OrganizationTenantMember, error) {
|
||||
var member types.OrganizationTenantMember
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("organization_id = ? AND user_id = ?", orgID, userID).
|
||||
Where("organization_id = ? AND tenant_id = ?", orgID, tenantID).
|
||||
First(&member).Error
|
||||
|
||||
if err != nil {
|
||||
@@ -185,19 +194,20 @@ func (r *organizationRepository) GetMember(ctx context.Context, orgID string, us
|
||||
return &member, nil
|
||||
}
|
||||
|
||||
// ListMembersByUserForOrgs returns one member record per org where the user is a member (batch).
|
||||
func (r *organizationRepository) ListMembersByUserForOrgs(ctx context.Context, userID string, orgIDs []string) (map[string]*types.OrganizationMember, error) {
|
||||
// ListTenantMembersByTenantForOrgs returns one membership row per org where
|
||||
// the tenant participates (batch).
|
||||
func (r *organizationRepository) ListTenantMembersByTenantForOrgs(ctx context.Context, tenantID uint64, orgIDs []string) (map[string]*types.OrganizationTenantMember, error) {
|
||||
if len(orgIDs) == 0 {
|
||||
return make(map[string]*types.OrganizationMember), nil
|
||||
return make(map[string]*types.OrganizationTenantMember), nil
|
||||
}
|
||||
var members []*types.OrganizationMember
|
||||
var members []*types.OrganizationTenantMember
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND organization_id IN ?", userID, orgIDs).
|
||||
Where("tenant_id = ? AND organization_id IN ?", tenantID, orgIDs).
|
||||
Find(&members).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make(map[string]*types.OrganizationMember, len(members))
|
||||
out := make(map[string]*types.OrganizationTenantMember, len(members))
|
||||
for _, m := range members {
|
||||
if m != nil {
|
||||
out[m.OrganizationID] = m
|
||||
@@ -206,11 +216,11 @@ func (r *organizationRepository) ListMembersByUserForOrgs(ctx context.Context, u
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// CountMembers counts the number of members in an organization
|
||||
func (r *organizationRepository) CountMembers(ctx context.Context, orgID string) (int64, error) {
|
||||
// CountTenantMembers counts the number of tenant members in an organization.
|
||||
func (r *organizationRepository) CountTenantMembers(ctx context.Context, orgID string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).
|
||||
Model(&types.OrganizationMember{}).
|
||||
Model(&types.OrganizationTenantMember{}).
|
||||
Where("organization_id = ?", orgID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
@@ -252,11 +262,15 @@ func (r *organizationRepository) GetJoinRequestByID(ctx context.Context, id stri
|
||||
return &request, nil
|
||||
}
|
||||
|
||||
// GetPendingJoinRequest gets a pending join request for a user in an organization (any type)
|
||||
func (r *organizationRepository) GetPendingJoinRequest(ctx context.Context, orgID string, userID string) (*types.OrganizationJoinRequest, error) {
|
||||
// GetPendingJoinRequestByTenant returns a pending request for the given
|
||||
// (org, tenant). After Plan 3 the dedup key is per-tenant, not per-user
|
||||
// — the user requesting and the user reviewing may be different humans
|
||||
// in the same tenant, but the resource being granted is tenant-level
|
||||
// access.
|
||||
func (r *organizationRepository) GetPendingJoinRequestByTenant(ctx context.Context, orgID string, tenantID uint64) (*types.OrganizationJoinRequest, error) {
|
||||
var request types.OrganizationJoinRequest
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("organization_id = ? AND user_id = ? AND status = ?", orgID, userID, types.JoinRequestStatusPending).
|
||||
Where("organization_id = ? AND tenant_id = ? AND status = ?", orgID, tenantID, types.JoinRequestStatusPending).
|
||||
First(&request).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -267,11 +281,12 @@ func (r *organizationRepository) GetPendingJoinRequest(ctx context.Context, orgI
|
||||
return &request, nil
|
||||
}
|
||||
|
||||
// GetPendingRequestByType gets a pending request for a user filtered by request type
|
||||
func (r *organizationRepository) GetPendingRequestByType(ctx context.Context, orgID string, userID string, requestType types.JoinRequestType) (*types.OrganizationJoinRequest, error) {
|
||||
// GetPendingRequestByTenantAndType narrows the (org, tenant) pending
|
||||
// dedup query to a specific request_type (join | upgrade).
|
||||
func (r *organizationRepository) GetPendingRequestByTenantAndType(ctx context.Context, orgID string, tenantID uint64, requestType types.JoinRequestType) (*types.OrganizationJoinRequest, error) {
|
||||
var request types.OrganizationJoinRequest
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("organization_id = ? AND user_id = ? AND status = ? AND request_type = ?", orgID, userID, types.JoinRequestStatusPending, requestType).
|
||||
Where("organization_id = ? AND tenant_id = ? AND status = ? AND request_type = ?", orgID, tenantID, types.JoinRequestStatusPending, requestType).
|
||||
First(&request).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
Reference in New Issue
Block a user