From 1e9a3fcc349a8fc4a8dcc4af2fcdf1c0c3835741 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 2 May 2026 16:20:47 +0800 Subject: [PATCH] Lookup parent organization unit under the target tenant --- .../Abp/Identity/OrganizationUnitManager.cs | 9 +++++-- .../Identity/OrganizationUnitManager_Tests.cs | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs index cb8fa17a77..ed4fe22367 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/OrganizationUnitManager.cs @@ -159,8 +159,13 @@ public class OrganizationUnitManager : DomainService return; } - var parent = await OrganizationUnitRepository.FindAsync(parentId.Value); - if (parent == null || parent.TenantId != tenantId) + OrganizationUnit parent; + using (CurrentTenant.Change(tenantId)) + { + parent = await OrganizationUnitRepository.FindAsync(parentId.Value); + } + + if (parent == null) { throw new BusinessException(IdentityErrorCodes.OrganizationUnitParentTenantMismatch) .WithData("ParentId", parentId); diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs index e891330efa..af8a0ba31a 100644 --- a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs @@ -169,4 +169,28 @@ public class OrganizationUnitManager_Tests : AbpIdentityDomainTestBase ex.Code.ShouldBe(IdentityErrorCodes.OrganizationUnitParentTenantMismatch); } + + [Fact] + public async Task Should_Allow_Host_To_Create_Tenant_Organization_Unit_Under_Same_Tenant_Parent() + { + var tenantId = Guid.NewGuid(); + OrganizationUnit tenantRoot; + + using (_currentTenant.Change(tenantId)) + { + tenantRoot = new OrganizationUnit(_guidGenerator.Create(), "TenantRoot", null, tenantId); + await _organizationUnitManager.CreateAsync(tenantRoot); + } + + var child = new OrganizationUnit(_guidGenerator.Create(), "TenantChild", tenantRoot.Id, tenantId); + await _organizationUnitManager.CreateAsync(child); + + using (_currentTenant.Change(tenantId)) + { + var loaded = await _organizationUnitRepository.FindAsync(child.Id); + loaded.ShouldNotBeNull(); + loaded.ParentId.ShouldBe(tenantRoot.Id); + loaded.TenantId.ShouldBe(tenantId); + } + } }