mirror of https://github.com/abpframework/abp.git
46 changed files with 650 additions and 266 deletions
@ -0,0 +1,11 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public class CmsKitCommentOptions |
|||
{ |
|||
[NotNull] |
|||
public List<CommentEntityTypeDefinition> EntityTypes { get; } = new List<CommentEntityTypeDefinition>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public class CommentEntityTypeDefinition : IEquatable<CommentEntityTypeDefinition> |
|||
{ |
|||
public CommentEntityTypeDefinition([NotNull] string entityType) |
|||
{ |
|||
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType)); |
|||
} |
|||
|
|||
public string EntityType { get; } |
|||
|
|||
public bool Equals(CommentEntityTypeDefinition other) |
|||
{ |
|||
return EntityType == other?.EntityType; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
|
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.CmsKit.Users; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public class CommentManager : DomainService |
|||
{ |
|||
protected ICommentEntityTypeDefinitionStore DefinitionStore { get; } |
|||
|
|||
public CommentManager(ICommentEntityTypeDefinitionStore definitionStore) |
|||
{ |
|||
DefinitionStore = definitionStore; |
|||
} |
|||
|
|||
public virtual async Task<Comment> CreateAsync([NotNull] CmsUser creator, |
|||
[NotNull] string entityType, |
|||
[NotNull] string entityId, |
|||
[NotNull] string text, |
|||
[CanBeNull] Guid? repliedCommentId = null) |
|||
{ |
|||
Check.NotNull(creator, nameof(creator)); |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType), CommentConsts.MaxEntityTypeLength); |
|||
Check.NotNullOrWhiteSpace(entityId, nameof(entityId), CommentConsts.MaxEntityIdLength); |
|||
Check.NotNullOrWhiteSpace(text, nameof(text), CommentConsts.MaxTextLength); |
|||
|
|||
if (!await DefinitionStore.IsDefinedAsync(entityType)) |
|||
{ |
|||
throw new EntityNotCommentableException(entityType); |
|||
} |
|||
|
|||
return new Comment( |
|||
GuidGenerator.Create(), |
|||
entityType, |
|||
entityId, |
|||
text, |
|||
repliedCommentId, |
|||
creator.Id, |
|||
CurrentTenant.Id); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Options; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public class DefaultCommentEntityTypeDefinitionStore : ICommentEntityTypeDefinitionStore, ITransientDependency |
|||
{ |
|||
protected CmsKitCommentOptions Options { get; } |
|||
|
|||
public DefaultCommentEntityTypeDefinitionStore(IOptions<CmsKitCommentOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual Task<CommentEntityTypeDefinition> GetDefinitionAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var result = Options.EntityTypes.SingleOrDefault(x => x.EntityType.Equals(entityType, StringComparison.InvariantCultureIgnoreCase)) ?? |
|||
throw new EntityNotCommentableException(entityType); |
|||
|
|||
return Task.FromResult(result); |
|||
} |
|||
|
|||
public virtual Task<bool> IsDefinedAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var isDefined = Options.EntityTypes.Any(x => x.EntityType == entityType); |
|||
|
|||
return Task.FromResult(isDefined); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Runtime.Serialization; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
[Serializable] |
|||
public class EntityNotCommentableException : BusinessException |
|||
{ |
|||
public EntityNotCommentableException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) |
|||
{ |
|||
} |
|||
|
|||
public EntityNotCommentableException(string entityType) |
|||
{ |
|||
Code = CmsKitErrorCodes.Comments.EntityNotCommentable; |
|||
EntityType = entityType; |
|||
WithData(nameof(EntityType), EntityType); |
|||
} |
|||
|
|||
public string EntityType { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.CmsKit.Comments; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public interface ICommentEntityTypeDefinitionStore |
|||
{ |
|||
Task<CommentEntityTypeDefinition> GetDefinitionAsync([NotNull] string entityType); |
|||
|
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
using Shouldly; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.CmsKit.Users; |
|||
using Xunit; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public class CommentManager_Test : CmsKitDomainTestBase |
|||
{ |
|||
private readonly CommentManager commentManager; |
|||
private readonly CmsKitTestData testData; |
|||
private readonly ICmsUserRepository userRepository; |
|||
|
|||
public CommentManager_Test() |
|||
{ |
|||
commentManager = GetRequiredService<CommentManager>(); |
|||
testData = GetRequiredService<CmsKitTestData>(); |
|||
userRepository = GetRequiredService<ICmsUserRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldWorkProperly_WithCorrectData() |
|||
{ |
|||
var creator = await userRepository.GetAsync(testData.User1Id); |
|||
|
|||
var text = "Thank you for the article. It's awesome"; |
|||
|
|||
var comment = await commentManager.CreateAsync(creator, testData.EntityType1, testData.EntityId1, text); |
|||
|
|||
comment.Id.ShouldNotBe(Guid.Empty); |
|||
comment.CreatorId.ShouldBe(creator.Id); |
|||
comment.EntityType.ShouldBe(testData.EntityType1); |
|||
comment.EntityId.ShouldBe(testData.EntityId1); |
|||
comment.Text.ShouldBe(text); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldThrowException_WithNotConfiguredEntityType() |
|||
{ |
|||
var creator = await userRepository.GetAsync(testData.User1Id); |
|||
var notConfiguredEntityType = "Some.New.Entity"; |
|||
var text = "Thank you for the article. It's awesome"; |
|||
|
|||
var exception = await Should.ThrowAsync<EntityNotCommentableException>(async () => |
|||
await commentManager.CreateAsync( |
|||
creator, |
|||
notConfiguredEntityType, |
|||
testData.EntityId1, |
|||
text)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
exception.EntityType.ShouldBe(notConfiguredEntityType); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
[Serializable] |
|||
public class IdentityRoleNameChangedEto |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public Guid? TenantId { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string OldName { get; set; } |
|||
} |
|||
} |
|||
@ -1,26 +1,27 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Identity; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.Identity |
|||
{ |
|||
// public class RoleDeletedEventHandler :
|
|||
// ILocalEventHandler<EntityDeletedEventData<IdentityRole>>,
|
|||
// ITransientDependency
|
|||
// {
|
|||
// protected IPermissionManager PermissionManager { get; }
|
|||
//
|
|||
// public RoleDeletedEventHandler(IPermissionManager permissionManager)
|
|||
// {
|
|||
// PermissionManager = permissionManager;
|
|||
// }
|
|||
//
|
|||
// public virtual async Task HandleEventAsync(EntityDeletedEventData<IdentityRole> eventData)
|
|||
// {
|
|||
// await PermissionManager.DeleteAsync(RolePermissionValueProvider.ProviderName, eventData.Entity.Name);
|
|||
// }
|
|||
// }
|
|||
public class RoleDeletedEventHandler : |
|||
IDistributedEventHandler<EntityDeletedEto<IdentityRoleEto>>, |
|||
ITransientDependency |
|||
{ |
|||
protected IPermissionManager PermissionManager { get; } |
|||
|
|||
public RoleDeletedEventHandler(IPermissionManager permissionManager) |
|||
{ |
|||
PermissionManager = permissionManager; |
|||
} |
|||
|
|||
public async Task HandleEventAsync(EntityDeletedEto<IdentityRoleEto> eventData) |
|||
{ |
|||
await PermissionManager.DeleteAsync(RolePermissionValueProvider.ProviderName, eventData.Entity.Name); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,45 +1,33 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.EventBus; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Identity; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.Identity |
|||
{ |
|||
//TODO: This code can not be here!
|
|||
public class RoleUpdateEventHandler : |
|||
IDistributedEventHandler<IdentityRoleNameChangedEto>, |
|||
ITransientDependency |
|||
{ |
|||
protected IPermissionManager PermissionManager { get; } |
|||
protected IPermissionGrantRepository PermissionGrantRepository { get; } |
|||
|
|||
public RoleUpdateEventHandler( |
|||
IPermissionManager permissionManager, |
|||
IPermissionGrantRepository permissionGrantRepository) |
|||
{ |
|||
PermissionManager = permissionManager; |
|||
PermissionGrantRepository = permissionGrantRepository; |
|||
} |
|||
|
|||
// public class RoleUpdateEventHandler :
|
|||
// ILocalEventHandler<IdentityRoleNameChangedEvent>,
|
|||
// ITransientDependency
|
|||
// {
|
|||
// protected IIdentityRoleRepository RoleRepository { get; }
|
|||
// protected IPermissionManager PermissionManager { get; }
|
|||
// protected IPermissionGrantRepository PermissionGrantRepository { get; }
|
|||
//
|
|||
// public RoleUpdateEventHandler(
|
|||
// IIdentityRoleRepository roleRepository,
|
|||
// IPermissionManager permissionManager,
|
|||
// IPermissionGrantRepository permissionGrantRepository)
|
|||
// {
|
|||
// RoleRepository = roleRepository;
|
|||
// PermissionManager = permissionManager;
|
|||
// PermissionGrantRepository = permissionGrantRepository;
|
|||
// }
|
|||
//
|
|||
// public virtual async Task HandleEventAsync(IdentityRoleNameChangedEvent eventData)
|
|||
// {
|
|||
// var role = await RoleRepository.FindAsync(eventData.IdentityRole.Id, false);
|
|||
// if (role == null)
|
|||
// {
|
|||
// return;
|
|||
// }
|
|||
//
|
|||
// var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync(RolePermissionValueProvider.ProviderName, eventData.OldName);
|
|||
// foreach (var permissionGrant in permissionGrantsInRole)
|
|||
// {
|
|||
// await PermissionManager.UpdateProviderKeyAsync(permissionGrant, eventData.IdentityRole.Name);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
public async Task HandleEventAsync(IdentityRoleNameChangedEto eventData) |
|||
{ |
|||
var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync(RolePermissionValueProvider.ProviderName, eventData.OldName); |
|||
foreach (var permissionGrant in permissionGrantsInRole) |
|||
{ |
|||
await PermissionManager.UpdateProviderKeyAsync(permissionGrant, eventData.Name); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,105 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Shouldly; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.PermissionManagement; |
|||
using Volo.Abp.PermissionManagement.Identity; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class Distributed_Role_Change_Events_Test : AbpIdentityDomainTestBase |
|||
{ |
|||
protected readonly IIdentityRoleRepository RoleRepository; |
|||
protected readonly IPermissionGrantRepository PermissionGrantRepository; |
|||
protected readonly IdentityRoleManager RoleManager; |
|||
protected readonly ILookupNormalizer LookupNormalizer; |
|||
protected readonly IUnitOfWorkManager UowManager; |
|||
protected readonly IDistributedCache<PermissionGrantCacheItem> Cache; |
|||
|
|||
public Distributed_Role_Change_Events_Test() |
|||
{ |
|||
RoleRepository = GetRequiredService<IIdentityRoleRepository>(); |
|||
; |
|||
PermissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); |
|||
; |
|||
RoleManager = GetRequiredService<IdentityRoleManager>(); |
|||
; |
|||
LookupNormalizer = GetRequiredService<ILookupNormalizer>(); |
|||
; |
|||
UowManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
Cache = GetRequiredService<IDistributedCache<PermissionGrantCacheItem>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Register_Handler() |
|||
{ |
|||
var x = GetRequiredService<IOptions<AbpDistributedEntityEventOptions>>(); |
|||
GetRequiredService<IOptions<AbpDistributedEntityEventOptions>>() |
|||
.Value |
|||
.AutoEventSelectors |
|||
.ShouldContain(m => m.Name == "Entity:" + typeof(IdentityRole).FullName); |
|||
|
|||
GetRequiredService<IOptions<AbpDistributedEventBusOptions>>() |
|||
.Value |
|||
.Handlers |
|||
.ShouldContain(h => h == typeof(RoleUpdateEventHandler) || h == typeof(RoleDeletedEventHandler)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Role_Updated_Distributed_Event_Test() |
|||
{ |
|||
var role = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator")); |
|||
|
|||
var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name); |
|||
permissionGrantsInRole.ShouldNotBeNull(); |
|||
permissionGrantsInRole.Count.ShouldBeGreaterThan(0); |
|||
var count = permissionGrantsInRole.Count; |
|||
|
|||
using (var uow = UowManager.Begin()) |
|||
{ |
|||
var identityResult = await RoleManager.SetRoleNameAsync(role, "TestModerator"); |
|||
identityResult.Succeeded.ShouldBeTrue(); |
|||
await RoleRepository.UpdateAsync(role); |
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
role = await RoleRepository.GetAsync(role.Id); |
|||
role.Name.ShouldBe("TestModerator"); |
|||
|
|||
permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name); |
|||
permissionGrantsInRole.Count.ShouldBe(count); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Role_Deleted_Distributed_Event_Test() |
|||
{ |
|||
var role = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator")); |
|||
var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name); |
|||
|
|||
var caches = permissionGrantsInRole.Select(x => new KeyValuePair<string, PermissionGrantCacheItem>( |
|||
PermissionGrantCacheItem.CalculateCacheKey(x.Name, x.ProviderName, x.ProviderKey), |
|||
new PermissionGrantCacheItem(true))).ToList(); |
|||
await Cache.SetManyAsync(caches); |
|||
|
|||
|
|||
using (var uow = UowManager.Begin()) |
|||
{ |
|||
await RoleRepository.DeleteAsync(role); |
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
var permissionGrantCaches = await Cache.GetManyAsync(caches.Select(x=>x.Key)); |
|||
foreach (var cache in permissionGrantCaches) |
|||
{ |
|||
cache.Value.ShouldBeNull(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,60 +0,0 @@ |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Shouldly; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.PermissionManagement; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
//TODO: This code can not be here!
|
|||
//https://github.com/abpframework/abp/commit/847f526041145b62376b760776829d5ce257da1c
|
|||
// public class RoleChangingEvents_Test : AbpIdentityDomainTestBase
|
|||
// {
|
|||
// protected readonly IIdentityRoleRepository RoleRepository;
|
|||
// protected readonly IPermissionGrantRepository PermissionGrantRepository;
|
|||
// protected readonly IdentityRoleManager RoleManager;
|
|||
// protected readonly ILookupNormalizer LookupNormalizer;
|
|||
// protected readonly IGuidGenerator GuidGenerator;
|
|||
// protected readonly IUnitOfWorkManager UowManager;
|
|||
//
|
|||
// public RoleChangingEvents_Test()
|
|||
// {
|
|||
// RoleRepository = GetRequiredService<IIdentityRoleRepository>(); ;
|
|||
// PermissionGrantRepository = GetRequiredService<IPermissionGrantRepository>(); ;
|
|||
// RoleManager = GetRequiredService<IdentityRoleManager>(); ;
|
|||
// LookupNormalizer = GetRequiredService<ILookupNormalizer>(); ;
|
|||
// GuidGenerator = GetRequiredService<IGuidGenerator>();
|
|||
// UowManager = GetRequiredService<IUnitOfWorkManager>();
|
|||
// }
|
|||
//
|
|||
// [Fact(Skip = "https://github.com/abpframework/abp/actions/runs/454248191")]
|
|||
// public async Task Role_Update_Event_Test()
|
|||
// {
|
|||
// var role = await RoleRepository
|
|||
// .FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator"))
|
|||
// ;
|
|||
//
|
|||
// var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name);
|
|||
// permissionGrantsInRole.ShouldNotBeNull();
|
|||
// permissionGrantsInRole.Count.ShouldBeGreaterThan(0);
|
|||
// var count = permissionGrantsInRole.Count;
|
|||
//
|
|||
// using (var uow = UowManager.Begin())
|
|||
// {
|
|||
// var identityResult = await RoleManager.SetRoleNameAsync(role, "TestModerator");
|
|||
// identityResult.Succeeded.ShouldBeTrue();
|
|||
// var xx = await RoleRepository.UpdateAsync(role);
|
|||
// await uow.CompleteAsync();
|
|||
// }
|
|||
//
|
|||
// role = await RoleRepository.GetAsync(role.Id);
|
|||
// role.Name.ShouldBe("TestModerator");
|
|||
//
|
|||
// permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name);
|
|||
// permissionGrantsInRole.Count.ShouldBe(count);
|
|||
// }
|
|||
// }
|
|||
} |
|||
Loading…
Reference in new issue