diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs index f57425d84a..03c34e3170 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs @@ -14,8 +14,6 @@ namespace Volo.Abp.Identity.Organizations Task> GetListAsync(IEnumerable ids, CancellationToken cancellationToken = default); - Task> GetListAsync(bool includeDetails = true, CancellationToken cancellationToken = default); - Task GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/OrganizationUnitManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/OrganizationUnitManager.cs index f4062a4d6c..e3fe4c5571 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/OrganizationUnitManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/OrganizationUnitManager.cs @@ -2,9 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; -using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Services; using Volo.Abp.Identity.Localization; using Volo.Abp.Threading; @@ -17,11 +15,10 @@ namespace Volo.Abp.Identity.Organizations /// public class OrganizationUnitManager : DomainService { - protected IOrganizationUnitRepository _organizationUnitRepository { get; private set; } - - private readonly IStringLocalizer _localizer; - private readonly IIdentityRoleRepository _identityRoleRepository; - private readonly ICancellationTokenProvider _cancellationTokenProvider; + protected IOrganizationUnitRepository OrganizationUnitRepository { get; } + protected IStringLocalizer Localizer { get; } + protected IIdentityRoleRepository IdentityRoleRepository { get; } + protected ICancellationTokenProvider CancellationTokenProvider { get; } public OrganizationUnitManager( IOrganizationUnitRepository organizationUnitRepository, @@ -29,10 +26,10 @@ namespace Volo.Abp.Identity.Organizations IIdentityRoleRepository identityRoleRepository, ICancellationTokenProvider cancellationTokenProvider) { - _organizationUnitRepository = organizationUnitRepository; - _localizer = localizer; - _identityRoleRepository = identityRoleRepository; - _cancellationTokenProvider = cancellationTokenProvider; + OrganizationUnitRepository = organizationUnitRepository; + Localizer = localizer; + IdentityRoleRepository = identityRoleRepository; + CancellationTokenProvider = cancellationTokenProvider; } [UnitOfWork] @@ -40,30 +37,36 @@ namespace Volo.Abp.Identity.Organizations { organizationUnit.Code = await GetNextChildCodeAsync(organizationUnit.ParentId); await ValidateOrganizationUnitAsync(organizationUnit); - await _organizationUnitRepository.InsertAsync(organizationUnit); + await OrganizationUnitRepository.InsertAsync(organizationUnit); } public virtual async Task UpdateAsync(OrganizationUnit organizationUnit) { await ValidateOrganizationUnitAsync(organizationUnit); - await _organizationUnitRepository.UpdateAsync(organizationUnit); + await OrganizationUnitRepository.UpdateAsync(organizationUnit); } public virtual async Task GetNextChildCodeAsync(Guid? parentId) { var lastChild = await GetLastChildOrNullAsync(parentId); - if (lastChild == null) + if (lastChild != null) { - var parentCode = parentId != null ? await GetCodeOrDefaultAsync(parentId.Value) : null; - return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1)); + return OrganizationUnit.CalculateNextCode(lastChild.Code); } - return OrganizationUnit.CalculateNextCode(lastChild.Code); + var parentCode = parentId != null + ? await GetCodeOrDefaultAsync(parentId.Value) + : null; + + return OrganizationUnit.AppendCode( + parentCode, + OrganizationUnit.CreateCode(1) + ); } public virtual async Task GetLastChildOrNullAsync(Guid? parentId) { - var children = await _organizationUnitRepository.GetChildrenAsync(parentId); + var children = await OrganizationUnitRepository.GetChildrenAsync(parentId); return children.OrderBy(c => c.Code).LastOrDefault(); } @@ -74,16 +77,16 @@ namespace Volo.Abp.Identity.Organizations foreach (var child in children) { - await _organizationUnitRepository.DeleteAsync(child); + await OrganizationUnitRepository.DeleteAsync(child); } - await _organizationUnitRepository.DeleteAsync(id); + await OrganizationUnitRepository.DeleteAsync(id); } [UnitOfWork] public virtual async Task MoveAsync(Guid id, Guid? parentId) { - var organizationUnit = await _organizationUnitRepository.GetAsync(id); + var organizationUnit = await OrganizationUnitRepository.GetAsync(id); if (organizationUnit.ParentId == parentId) { return; @@ -110,7 +113,7 @@ namespace Volo.Abp.Identity.Organizations public virtual async Task GetCodeOrDefaultAsync(Guid id) { - var ou = await _organizationUnitRepository.GetAsync(id); + var ou = await OrganizationUnitRepository.GetAsync(id); return ou?.Code; } @@ -122,7 +125,7 @@ namespace Volo.Abp.Identity.Organizations if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName)) { - throw new UserFriendlyException(_localizer["OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName]); + throw new UserFriendlyException(Localizer["OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName]); } } @@ -130,17 +133,17 @@ namespace Volo.Abp.Identity.Organizations { if (!recursive) { - return await _organizationUnitRepository.GetChildrenAsync(parentId); + return await OrganizationUnitRepository.GetChildrenAsync(parentId); } if (!parentId.HasValue) { - return await _organizationUnitRepository.GetListAsync(); + return await OrganizationUnitRepository.GetListAsync(includeDetails: true); } var code = await GetCodeOrDefaultAsync(parentId.Value); - return await _organizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId); + return await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId); } public virtual Task IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) @@ -151,8 +154,8 @@ namespace Volo.Abp.Identity.Organizations public virtual async Task AddRoleToOrganizationUnitAsync(Guid roleId, Guid ouId) { await AddRoleToOrganizationUnitAsync( - await _identityRoleRepository.GetAsync(roleId), - await _organizationUnitRepository.GetAsync(ouId, true) + await IdentityRoleRepository.GetAsync(roleId), + await OrganizationUnitRepository.GetAsync(ouId, true) ); } @@ -171,8 +174,8 @@ namespace Volo.Abp.Identity.Organizations public virtual async Task RemoveRoleFromOrganizationUnitAsync(Guid roleId, Guid ouId) { await RemoveRoleFromOrganizationUnitAsync( - await _identityRoleRepository.GetAsync(roleId), - await _organizationUnitRepository.GetAsync(ouId, true) + await IdentityRoleRepository.GetAsync(roleId), + await OrganizationUnitRepository.GetAsync(ouId, true) ); } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs index f3e0eac922..0aa90df8ac 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; @@ -11,38 +10,48 @@ using Volo.Abp.Identity.Organizations; namespace Volo.Abp.Identity.EntityFrameworkCore { - public class EfCoreOrganizationUnitRepository : EfCoreRepository, IOrganizationUnitRepository + public class EfCoreOrganizationUnitRepository + : EfCoreRepository, + IOrganizationUnitRepository { - public EfCoreOrganizationUnitRepository(IDbContextProvider dbContextProvider) + public EfCoreOrganizationUnitRepository( + IDbContextProvider dbContextProvider) : base(dbContextProvider) { } - public async Task> GetChildrenAsync(Guid? parentId, CancellationToken cancellationToken = default) + public async Task> GetChildrenAsync( + Guid? parentId, + CancellationToken cancellationToken = default) { - return await DbSet.Where(x => x.ParentId == parentId) + return await DbSet + .Where(x => x.ParentId == parentId) .ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId, CancellationToken cancellationToken = default) + public async Task> GetAllChildrenWithParentCodeAsync( + string code, + Guid? parentId, + CancellationToken cancellationToken = default) { - return await DbSet.Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) + return await DbSet + .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) .ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task> GetListAsync(IEnumerable ids, CancellationToken cancellationToken = default) - { - return await DbSet.Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken)); - } - - public override async Task> GetListAsync(bool includeDetails = true, CancellationToken cancellationToken = default) + public async Task> GetListAsync( + IEnumerable ids, + CancellationToken cancellationToken = default) { return await DbSet - .IncludeDetails(includeDetails) + .Where(t => ids.Contains(t.Id)) .ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default) + public async Task GetOrganizationUnitAsync( + string displayName, + bool includeDetails = false, + CancellationToken cancellationToken = default) { return await DbSet .IncludeDetails(includeDetails) diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index d11a180c46..879bd0acb5 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -3,11 +3,9 @@ using MongoDB.Driver.Linq; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; -using Volo.Abp.Guids; using Volo.Abp.Identity.Organizations; using Volo.Abp.MongoDB; @@ -15,42 +13,48 @@ namespace Volo.Abp.Identity.MongoDB { public class MongoOrganizationUnitRepository : MongoDbRepository, IOrganizationUnitRepository { - private readonly IGuidGenerator _guidGenerator; - public MongoOrganizationUnitRepository( - IMongoDbContextProvider dbContextProvider, - IGuidGenerator guidGenerator) + IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) { - _guidGenerator = guidGenerator; } - public async Task> GetChildrenAsync(Guid? parentId, CancellationToken cancellationToken = default) + public async Task> GetChildrenAsync( + Guid? parentId, + CancellationToken cancellationToken = default) { - return await DbContext.OrganizationUnits.AsQueryable().Where(ou => ou.ParentId == parentId) - .ToListAsync(GetCancellationToken(cancellationToken)); + return await GetMongoQueryable() + .Where(ou => ou.ParentId == parentId) + .ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId, CancellationToken cancellationToken = default) + public async Task> GetAllChildrenWithParentCodeAsync( + string code, + Guid? parentId, + CancellationToken cancellationToken = default) { - return await DbContext.OrganizationUnits.AsQueryable() + return await GetMongoQueryable() .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) .ToListAsync(GetCancellationToken(cancellationToken)); } public async Task> GetListAsync(IEnumerable ids, CancellationToken cancellationToken = default) { - return await DbContext.OrganizationUnits.AsQueryable() - .Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken)); + return await GetMongoQueryable() + .Where(t => ids.Contains(t.Id)) + .ToListAsync(GetCancellationToken(cancellationToken)); } - public async Task GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default) + public async Task GetOrganizationUnitAsync( + string displayName, + bool includeDetails = false, + CancellationToken cancellationToken = default) { - return await DbContext.OrganizationUnits.AsQueryable() - .FirstOrDefaultAsync( - ou => ou.DisplayName == displayName, - GetCancellationToken(cancellationToken) - ); + return await GetMongoQueryable() + .FirstOrDefaultAsync( + ou => ou.DisplayName == displayName, + GetCancellationToken(cancellationToken) + ); } } } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs index 8dc81ceb5e..5d09eace23 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs @@ -44,7 +44,7 @@ namespace Volo.Abp.Identity [Fact] public async Task GetListAsync() { - var ouIds = (await OrganizationUnitRepository.GetListAsync()) + var ouIds = (await OrganizationUnitRepository.GetListAsync(includeDetails: true)) .Select(ou => ou.Id).Take(2); var ous = await OrganizationUnitRepository.GetListAsync(ouIds); ous.Count.ShouldBe(2); @@ -67,7 +67,7 @@ namespace Volo.Abp.Identity [Fact] public async Task Should_Eager_Load_OrganizationUnit_Collections() { - var ou = (await OrganizationUnitRepository.GetListAsync(true)) + var ou = (await OrganizationUnitRepository.GetListAsync(includeDetails: true)) .FirstOrDefault(ou => ou.DisplayName == "OU111"); ou.Roles.ShouldNotBeNull(); ou.Roles.Any().ShouldBeTrue();