mirror of https://github.com/abpframework/abp.git
committed by
GitHub
70 changed files with 1039 additions and 9 deletions
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.Identity; |
|||
|
|||
public interface IIdentityUserDelegationRepository: IBasicRepository<IdentityUserDelegation, Guid> |
|||
{ |
|||
Task<List<IdentityUserDelegation>> GetListAsync( |
|||
Guid? sourceUserId, |
|||
Guid? targetUserId, |
|||
CancellationToken cancellationToken = default); |
|||
|
|||
Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync( |
|||
Guid targetUserId, |
|||
CancellationToken cancellationToken = default); |
|||
|
|||
Task<IdentityUserDelegation> FindActiveDelegationByIdAsync( |
|||
Guid id, |
|||
CancellationToken cancellationToken = default); |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.Identity; |
|||
|
|||
public class IdentityUserDelegation : BasicAggregateRoot<Guid>, IMultiTenant |
|||
{ |
|||
public virtual Guid? TenantId { get; protected set; } |
|||
|
|||
public virtual Guid SourceUserId { get; protected set; } |
|||
|
|||
public virtual Guid TargetUserId { get; protected set; } |
|||
|
|||
public virtual DateTime StartTime { get; protected set; } |
|||
|
|||
public virtual DateTime EndTime { get; protected set; } |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of <see cref="IdentityUserDelegation"/>.
|
|||
/// </summary>
|
|||
protected IdentityUserDelegation() |
|||
{ |
|||
} |
|||
|
|||
public IdentityUserDelegation( |
|||
Guid id, |
|||
Guid sourceUserId, |
|||
Guid targetUserId, |
|||
DateTime startTime, |
|||
DateTime endTime, |
|||
Guid? tenantId = null) |
|||
: base(id) |
|||
{ |
|||
TenantId = tenantId; |
|||
SourceUserId = sourceUserId; |
|||
TargetUserId = targetUserId; |
|||
StartTime = startTime; |
|||
EndTime = endTime; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
|
|||
namespace Volo.Abp.Identity; |
|||
|
|||
public class IdentityUserDelegationManager : DomainService |
|||
{ |
|||
protected IIdentityUserDelegationRepository IdentityUserDelegationRepository { get; } |
|||
|
|||
public IdentityUserDelegationManager(IIdentityUserDelegationRepository identityUserDelegationRepository) |
|||
{ |
|||
IdentityUserDelegationRepository = identityUserDelegationRepository; |
|||
} |
|||
|
|||
public virtual async Task<List<IdentityUserDelegation>> GetListAsync(Guid? sourceUserId = null, Guid? targetUserId = null, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await IdentityUserDelegationRepository.GetListAsync(sourceUserId, targetUserId, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public virtual async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUseId, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await IdentityUserDelegationRepository.GetActiveDelegationsAsync(targetUseId, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public virtual async Task<IdentityUserDelegation> FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await IdentityUserDelegationRepository.FindActiveDelegationByIdAsync(id, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public virtual async Task DelegateNewUserAsync(Guid sourceUserId, Guid targetUserId, DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default) |
|||
{ |
|||
if (sourceUserId == targetUserId) |
|||
{ |
|||
throw new BusinessException(IdentityErrorCodes.YouCannotDelegateYourself); |
|||
} |
|||
|
|||
await IdentityUserDelegationRepository.InsertAsync( |
|||
new IdentityUserDelegation( |
|||
GuidGenerator.Create(), |
|||
sourceUserId, |
|||
targetUserId, |
|||
startTime, |
|||
endTime |
|||
), |
|||
cancellationToken: cancellationToken |
|||
); |
|||
} |
|||
|
|||
public virtual async Task DeleteDelegationAsync(Guid id, Guid sourceUserId, CancellationToken cancellationToken = default) |
|||
{ |
|||
var delegation = await IdentityUserDelegationRepository.FindAsync(id, cancellationToken: cancellationToken); |
|||
|
|||
if (delegation != null && delegation.SourceUserId == sourceUserId) |
|||
{ |
|||
await IdentityUserDelegationRepository.DeleteAsync(delegation, cancellationToken: cancellationToken); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.Identity.EntityFrameworkCore; |
|||
|
|||
public class EfCoreIdentityUserDelegationRepository : EfCoreRepository<IIdentityDbContext, IdentityUserDelegation, Guid>, IIdentityUserDelegationRepository |
|||
{ |
|||
protected IClock Clock { get; } |
|||
|
|||
public EfCoreIdentityUserDelegationRepository(IDbContextProvider<IIdentityDbContext> dbContextProvider, IClock clock) |
|||
: base(dbContextProvider) |
|||
{ |
|||
Clock = clock; |
|||
} |
|||
|
|||
public async Task<List<IdentityUserDelegation>> GetListAsync(Guid? sourceUserId, Guid? targetUserId, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetDbSetAsync()) |
|||
.AsNoTracking() |
|||
.WhereIf(sourceUserId.HasValue, x => x.SourceUserId == sourceUserId) |
|||
.WhereIf(targetUserId.HasValue, x => x.TargetUserId == targetUserId) |
|||
.ToListAsync(cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetDbSetAsync()) |
|||
.AsNoTracking() |
|||
.Where(x => x.TargetUserId == targetUserId && |
|||
x.StartTime <= Clock.Now && |
|||
x.EndTime >= Clock.Now) |
|||
.ToListAsync(cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public async Task<IdentityUserDelegation> FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetDbSetAsync()) |
|||
.AsNoTracking() |
|||
.FirstOrDefaultAsync(x => |
|||
x.Id == id && |
|||
x.StartTime <= Clock.Now && |
|||
x.EndTime >= Clock.Now |
|||
, cancellationToken: GetCancellationToken(cancellationToken)); |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using MongoDB.Driver; |
|||
using MongoDB.Driver.Linq; |
|||
using Volo.Abp.Domain.Repositories.MongoDB; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.Identity.MongoDB; |
|||
|
|||
public class MongoIdentityUserDelegationRepository : MongoDbRepository<IAbpIdentityMongoDbContext, IdentityUserDelegation, Guid>, IIdentityUserDelegationRepository |
|||
{ |
|||
protected IClock Clock { get; } |
|||
|
|||
public MongoIdentityUserDelegationRepository(IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider, IClock clock) |
|||
: base(dbContextProvider) |
|||
{ |
|||
Clock = clock; |
|||
} |
|||
|
|||
public async Task<List<IdentityUserDelegation>> GetListAsync(Guid? sourceUserId, Guid? targetUserId, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetMongoQueryableAsync(cancellationToken)) |
|||
.WhereIf(sourceUserId.HasValue, x => x.SourceUserId == sourceUserId) |
|||
.WhereIf(targetUserId.HasValue, x => x.TargetUserId == targetUserId) |
|||
.As<IMongoQueryable<IdentityUserDelegation>>() |
|||
.ToListAsync(cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public async Task<List<IdentityUserDelegation>> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetMongoQueryableAsync(cancellationToken)) |
|||
.Where(x => x.TargetUserId == targetUserId) |
|||
.Where(x => x.StartTime <= Clock.Now && x.EndTime >= Clock.Now) |
|||
.As<IMongoQueryable<IdentityUserDelegation>>() |
|||
.ToListAsync(cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
public async Task<IdentityUserDelegation> FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await (await GetMongoQueryableAsync(cancellationToken)) |
|||
.FirstOrDefaultAsync(x => |
|||
x.Id == id && |
|||
x.StartTime <= Clock.Now && |
|||
x.EndTime >= Clock.Now |
|||
, cancellationToken: GetCancellationToken(cancellationToken)); |
|||
} |
|||
} |
|||
@ -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<IdentityUserDelegationManager>(); |
|||
TestData = GetRequiredService<IdentityTestData>(); |
|||
} |
|||
|
|||
[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<BusinessException>(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); |
|||
} |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
namespace Volo.Abp.Identity.EntityFrameworkCore; |
|||
|
|||
public class IdentityUserDelegationepository_Tests : IdentityUserDelegationepository_Tests<AbpIdentityEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity.MongoDB; |
|||
|
|||
[Collection(MongoTestCollection.Name)] |
|||
public class IdentityUserDelegationepository_Tests: IdentityUserDelegationepository_Tests<AbpIdentityMongoDbTestModule> |
|||
{ |
|||
|
|||
} |
|||
@ -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<TStartupModule> : AbpIdentityTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected IIdentityUserRepository UserRepository { get; } |
|||
protected IIdentityUserDelegationRepository IdentityUserDelegationRepository { get; } |
|||
protected IdentityTestData TestData { get; } |
|||
|
|||
public IdentityUserDelegationepository_Tests() |
|||
{ |
|||
UserRepository = GetRequiredService<IIdentityUserRepository>(); |
|||
IdentityUserDelegationRepository = GetRequiredService<IIdentityUserDelegationRepository>(); |
|||
TestData = GetRequiredService<IdentityTestData>(); |
|||
} |
|||
|
|||
[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); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue