fix(rbac): allow source-tenant Admin+ to manage their cross-tenant shares

Plan 3 of #1303 lifts share ownership to the tenant level, but
RemoveShare / UpdateSharePermission still gated on the original sharer
user. If that user left the tenant or moved on, the source tenant lost
the ability to revoke its own share — only the target org's admin
could clean up, which is governance theatre rather than ownership.

Extend the authz envelope on KB-share and agent-share mutations:

  (1) original sharer (unchanged);
  (2) source-tenant Admin+ — new, encodes "ownership lives in the
      tenant" by reading callerTenantRole from ctx;
  (3) target-org admin (unchanged).

Factored out as kbShareService.callerCanManageShare so Update and
Remove cannot drift apart.
This commit is contained in:
wizardchen
2026-05-15 22:52:19 +08:00
committed by lyingbug
parent eda6b1011a
commit f6684f226b
2 changed files with 56 additions and 18 deletions
+11 -3
View File
@@ -154,7 +154,8 @@ func (s *agentShareService) ShareAgent(ctx context.Context, agentID string, orgI
}
// RemoveShare removes an agent share.
// Same authz envelope as KB-share remove: original sharer or org admin.
// Same authz envelope as KB-share remove (see kbshare.callerCanManageShare):
// original sharer, OR source-tenant Admin+, OR target-org admin.
func (s *agentShareService) RemoveShare(ctx context.Context, shareID string, userID string, tenantID uint64) error {
share, err := s.shareRepo.GetByID(ctx, shareID)
if err != nil {
@@ -163,11 +164,18 @@ func (s *agentShareService) RemoveShare(ctx context.Context, shareID string, use
}
return err
}
// (1) Original sharer.
if share.SharedByUserID == userID {
return s.shareRepo.Delete(ctx, shareID)
}
tm, err := s.orgRepo.GetTenantMember(ctx, share.OrganizationID, tenantID)
if err == nil && tm.Role == types.OrgRoleAdmin {
// (2) Source-tenant Admin+ — Plan 3 ownership is tenant-level.
if tenantID != 0 && tenantID == share.SourceTenantID {
if types.TenantRoleFromContext(ctx).HasPermission(types.TenantRoleAdmin) {
return s.shareRepo.Delete(ctx, shareID)
}
}
// (3) Org admin in the target org (governance / sharer-left repair).
if tm, err := s.orgRepo.GetTenantMember(ctx, share.OrganizationID, tenantID); err == nil && tm.Role == types.OrgRoleAdmin {
return s.shareRepo.Delete(ctx, shareID)
}
return ErrAgentSharePermission