diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs index f880e7264f..360b9c494c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserDelegationRepository.cs @@ -14,7 +14,7 @@ public interface IIdentityUserDelegationRepository: IBasicRepository> GetActiveDelegationsAsync( - Guid sourceUserId, + Guid targetUserId, CancellationToken cancellationToken = default); Task FindActiveDelegationByIdAsync( diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs index 14e91111d6..8fd4c444f5 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserDelegationManager.cs @@ -20,9 +20,9 @@ public class IdentityUserDelegationManager : DomainService return await IdentityUserDelegationRepository.GetListAsync(sourceUserId, targetUserId, cancellationToken: cancellationToken); } - public virtual async Task> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default) + public virtual async Task> GetActiveDelegationsAsync(Guid targetUseId, CancellationToken cancellationToken = default) { - return await IdentityUserDelegationRepository.GetActiveDelegationsAsync(sourceUserId, cancellationToken: cancellationToken); + return await IdentityUserDelegationRepository.GetActiveDelegationsAsync(targetUseId, cancellationToken: cancellationToken); } public virtual async Task FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) @@ -30,9 +30,9 @@ public class IdentityUserDelegationManager : DomainService return await IdentityUserDelegationRepository.FindActiveDelegationByIdAsync(id, cancellationToken: cancellationToken); } - public virtual async Task DelegateNewUserAsync(Guid sourceUserId, IdentityUser targetUser, DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default) + public virtual async Task DelegateNewUserAsync(Guid sourceUserId, Guid targetUserId, DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default) { - if (sourceUserId == targetUser.Id) + if (sourceUserId == targetUserId) { throw new BusinessException(IdentityErrorCodes.YouCannotDelegateYourself); } @@ -41,7 +41,7 @@ public class IdentityUserDelegationManager : DomainService new IdentityUserDelegation( GuidGenerator.Create(), sourceUserId, - targetUser.Id, + targetUserId, startTime, endTime ), @@ -58,14 +58,4 @@ public class IdentityUserDelegationManager : DomainService await IdentityUserDelegationRepository.DeleteAsync(delegation, cancellationToken: cancellationToken); } } - - public virtual Task IsExpiredAsync(IdentityUserDelegation userDelegation) - { - return Task.FromResult(userDelegation.EndTime <= Clock.Now); - } - - public virtual async Task IsValidAsync(IdentityUserDelegation userDelegation) - { - return userDelegation.StartTime <= Clock.Now && !await IsExpiredAsync(userDelegation); - } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs index 1e28b68e80..cd567ffa80 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs @@ -29,11 +29,11 @@ public class EfCoreIdentityUserDelegationRepository : EfCoreRepository> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default) + public async Task> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) { return await (await GetDbSetAsync()) .AsNoTracking() - .Where(x => x.SourceUserId == sourceUserId && + .Where(x => x.TargetUserId == targetUserId && x.StartTime <= Clock.Now && x.EndTime >= Clock.Now) .ToListAsync(cancellationToken: cancellationToken); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs index 37cfc52e9c..a782dd57c9 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs @@ -31,10 +31,10 @@ public class MongoIdentityUserDelegationRepository : MongoDbRepository> GetActiveDelegationsAsync(Guid sourceUserId, CancellationToken cancellationToken = default) + public async Task> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) { return await (await GetMongoQueryableAsync(cancellationToken)) - .Where(x => x.SourceUserId == sourceUserId) + .Where(x => x.TargetUserId == targetUserId) .Where(x => x.StartTime <= Clock.Now && x.EndTime >= Clock.Now) .As>() .ToListAsync(cancellationToken: cancellationToken); diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserDelegationManager_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserDelegationManager_Tests.cs new file mode 100644 index 0000000000..c85f17230f --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserDelegationManager_Tests.cs @@ -0,0 +1,69 @@ +using System; +using System.Threading.Tasks; +using Shouldly; +using Xunit; + +namespace Volo.Abp.Identity; + +public class IdentityUserDelegationManager_Tests : AbpIdentityDomainTestBase +{ + protected IdentityUserDelegationManager IdentityUserDelegationManager { get; } + protected IdentityTestData TestData { get; } + + public IdentityUserDelegationManager_Tests() + { + IdentityUserDelegationManager = GetRequiredService(); + TestData = GetRequiredService(); + } + + [Fact] + public async Task GetListAsync() + { + (await IdentityUserDelegationManager.GetListAsync(Guid.NewGuid(), Guid.NewGuid())).Count.ShouldBe(0); + + (await IdentityUserDelegationManager.GetListAsync(TestData.UserJohnId, null)).Count.ShouldBe(2); + + (await IdentityUserDelegationManager.GetListAsync(null, TestData.UserDavidId)).Count.ShouldBe(3); + + (await IdentityUserDelegationManager.GetListAsync(TestData.UserNeoId, TestData.UserDavidId)).Count.ShouldBe(1); + } + + [Fact] + public async Task GetActiveDelegationsAsync() + { + var activeDelegations = await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserDavidId); + activeDelegations.Count.ShouldBe(2); + activeDelegations[0].SourceUserId.ShouldBe(TestData.UserJohnId); + activeDelegations[0].TargetUserId.ShouldBe(TestData.UserDavidId); + activeDelegations[1].SourceUserId.ShouldBe(TestData.UserNeoId); + activeDelegations[1].TargetUserId.ShouldBe(TestData.UserDavidId); + } + + [Fact] + public async Task FindActiveDelegationByIdAsync() + { + var activeDelegations = await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserDavidId); + var activeDelegation = await IdentityUserDelegationManager.FindActiveDelegationByIdAsync(activeDelegations[0].Id); + activeDelegation.ShouldNotBeNull(); + activeDelegation.SourceUserId.ShouldBe(TestData.UserJohnId); + activeDelegation.TargetUserId.ShouldBe(TestData.UserDavidId); + } + + [Fact] + public async Task DelegateNewUserAsync() + { + await Should.ThrowAsync(IdentityUserDelegationManager.DelegateNewUserAsync( + TestData.UserJohnId, + TestData.UserJohnId, + DateTime.Now.AddDays(-1), + DateTime.Now)); + + await IdentityUserDelegationManager.DelegateNewUserAsync( + TestData.UserJohnId, + TestData.UserBobId, + DateTime.Now.AddDays(-1), + DateTime.Now.AddDays(1)); + + (await IdentityUserDelegationManager.GetActiveDelegationsAsync(TestData.UserBobId)).Count.ShouldBe(1); + } +} \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/IdentityUserDelegationepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/IdentityUserDelegationepository_Tests.cs new file mode 100644 index 0000000000..92648defe7 --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/IdentityUserDelegationepository_Tests.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.Identity.EntityFrameworkCore; + +public class IdentityUserDelegationepository_Tests : IdentityUserDelegationepository_Tests +{ + +} \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.MongoDB.Tests/Volo/Abp/Identity/MongoDB/IdentityUserDelegationepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.MongoDB.Tests/Volo/Abp/Identity/MongoDB/IdentityUserDelegationepository_Tests.cs new file mode 100644 index 0000000000..5fb94b614a --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.MongoDB.Tests/Volo/Abp/Identity/MongoDB/IdentityUserDelegationepository_Tests.cs @@ -0,0 +1,9 @@ +using Xunit; + +namespace Volo.Abp.Identity.MongoDB; + +[Collection(MongoTestCollection.Name)] +public class IdentityUserDelegationepository_Tests: IdentityUserDelegationepository_Tests +{ + +} \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs index 788f974b5e..dc2b28690a 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs @@ -22,6 +22,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency private readonly OrganizationUnitManager _organizationUnitManager; private readonly IIdentityLinkUserRepository _identityLinkUserRepository; private readonly IdentityLinkUserManager _identityLinkUserManager; + private readonly IIdentityUserDelegationRepository _identityUserDelegationRepository; private IdentityRole _adminRole; private IdentityRole _moderatorRole; @@ -42,7 +43,8 @@ public class AbpIdentityTestDataBuilder : ITransientDependency IdentityTestData testData, OrganizationUnitManager organizationUnitManager, IIdentityLinkUserRepository identityLinkUserRepository, - IdentityLinkUserManager identityLinkUserManager) + IdentityLinkUserManager identityLinkUserManager, + IIdentityUserDelegationRepository identityUserDelegationRepository) { _guidGenerator = guidGenerator; _userRepository = userRepository; @@ -55,6 +57,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency _organizationUnitManager = organizationUnitManager; _identityLinkUserRepository = identityLinkUserRepository; _identityLinkUserManager = identityLinkUserManager; + _identityUserDelegationRepository = identityUserDelegationRepository; _identitySecurityLogRepository = identitySecurityLogRepository; } @@ -66,6 +69,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency await AddLinkUsers(); await AddClaimTypes(); await AddSecurityLogs(); + await AddUserDelegations(); } private async Task AddRoles() @@ -197,4 +201,28 @@ public class AbpIdentityTestDataBuilder : ITransientDependency CreationTime = new DateTime(2020, 01, 02, 10, 0, 0) })); } + + private async Task AddUserDelegations() + { + await _identityUserDelegationRepository.InsertAsync( + new IdentityUserDelegation(_guidGenerator.Create(), + _testData.UserJohnId, + _testData.UserDavidId, + DateTime.Now.AddDays(-2), + DateTime.Now.AddDays(-1))); + + await _identityUserDelegationRepository.InsertAsync( + new IdentityUserDelegation(_guidGenerator.Create(), + _testData.UserJohnId, + _testData.UserDavidId, + DateTime.Now.AddDays(-1), + DateTime.Now.AddDays(1))); + + await _identityUserDelegationRepository.InsertAsync( + new IdentityUserDelegation(_guidGenerator.Create(), + _testData.UserNeoId, + _testData.UserDavidId, + DateTime.Now.AddDays(-1), + DateTime.Now.AddDays(1))); + } } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserDelegationepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserDelegationepository_Tests.cs new file mode 100644 index 0000000000..50d5dd8a6f --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserDelegationepository_Tests.cs @@ -0,0 +1,55 @@ +using System; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.Identity; + +public abstract class IdentityUserDelegationepository_Tests : AbpIdentityTestBase + where TStartupModule : IAbpModule +{ + protected IIdentityUserRepository UserRepository { get; } + protected IIdentityUserDelegationRepository IdentityUserDelegationRepository { get; } + protected IdentityTestData TestData { get; } + + public IdentityUserDelegationepository_Tests() + { + UserRepository = GetRequiredService(); + IdentityUserDelegationRepository = GetRequiredService(); + TestData = GetRequiredService(); + } + + [Fact] + public async Task GetListAsync() + { + (await IdentityUserDelegationRepository.GetListAsync(Guid.NewGuid(), Guid.NewGuid())).Count.ShouldBe(0); + + (await IdentityUserDelegationRepository.GetListAsync(TestData.UserJohnId, null)).Count.ShouldBe(2); + + (await IdentityUserDelegationRepository.GetListAsync(null, TestData.UserDavidId)).Count.ShouldBe(3); + + (await IdentityUserDelegationRepository.GetListAsync(TestData.UserNeoId, TestData.UserDavidId)).Count.ShouldBe(1); + } + + [Fact] + public async Task GetActiveDelegationsAsync() + { + var activeDelegations = await IdentityUserDelegationRepository.GetActiveDelegationsAsync(TestData.UserDavidId); + activeDelegations.Count.ShouldBe(2); + activeDelegations[0].SourceUserId.ShouldBe(TestData.UserJohnId); + activeDelegations[0].TargetUserId.ShouldBe(TestData.UserDavidId); + activeDelegations[1].SourceUserId.ShouldBe(TestData.UserNeoId); + activeDelegations[1].TargetUserId.ShouldBe(TestData.UserDavidId); + } + + [Fact] + public async Task GetActiveDelegationOrNullAsync() + { + var activeDelegations = await IdentityUserDelegationRepository.GetActiveDelegationsAsync(TestData.UserDavidId); + var activeDelegation = await IdentityUserDelegationRepository.FindActiveDelegationByIdAsync(activeDelegations[0].Id); + activeDelegation.ShouldNotBeNull(); + activeDelegation.SourceUserId.ShouldBe(TestData.UserJohnId); + activeDelegation.TargetUserId.ShouldBe(TestData.UserDavidId); + } +} \ No newline at end of file