Browse Source

Use distributed events to handle role delete & rename for permission management.

pull/7935/head
Halil İbrahim Kalkan 6 years ago
parent
commit
06a318fca3
  1. 16
      modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityRoleNameChangedEto.cs
  2. 3
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentityDomainModule.cs
  3. 12
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs
  4. 5
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs
  5. 34
      modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleDeletedEventHandler.cs
  6. 60
      modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs

16
modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityRoleNameChangedEto.cs

@ -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; }
}
}

3
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentityDomainModule.cs

@ -40,6 +40,9 @@ namespace Volo.Abp.Identity
options.EtoMappings.Add<IdentityClaimType, IdentityClaimTypeEto>(typeof(AbpIdentityDomainModule)); options.EtoMappings.Add<IdentityClaimType, IdentityClaimTypeEto>(typeof(AbpIdentityDomainModule));
options.EtoMappings.Add<IdentityRole, IdentityRoleEto>(typeof(AbpIdentityDomainModule)); options.EtoMappings.Add<IdentityRole, IdentityRoleEto>(typeof(AbpIdentityDomainModule));
options.EtoMappings.Add<OrganizationUnit, OrganizationUnitEto>(typeof(AbpIdentityDomainModule)); options.EtoMappings.Add<OrganizationUnit, OrganizationUnitEto>(typeof(AbpIdentityDomainModule));
options.AutoEventSelectors.Add<IdentityUser>();
options.AutoEventSelectors.Add<IdentityRole>();
}); });
var identityBuilder = context.Services.AddAbpIdentity(options => var identityBuilder = context.Services.AddAbpIdentity(options =>

12
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs

@ -108,12 +108,24 @@ namespace Volo.Abp.Identity
Name = name; Name = name;
AddLocalEvent( AddLocalEvent(
#pragma warning disable 618
new IdentityRoleNameChangedEvent new IdentityRoleNameChangedEvent
#pragma warning restore 618
{ {
IdentityRole = this, IdentityRole = this,
OldName = oldName OldName = oldName
} }
); );
AddDistributedEvent(
new IdentityRoleNameChangedEto
{
Id = Id,
Name = Name,
OldName = oldName,
TenantId = TenantId
}
);
} }
public override string ToString() public override string ToString()

5
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs

@ -1,5 +1,8 @@
namespace Volo.Abp.Identity using System;
namespace Volo.Abp.Identity
{ {
[Obsolete("Use the distributed event (IdentityRoleNameChangedEto) instead.")]
public class IdentityRoleNameChangedEvent public class IdentityRoleNameChangedEvent
{ {
public IdentityRole IdentityRole { get; set; } public IdentityRole IdentityRole { get; set; }

34
modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleDeletedEventHandler.cs

@ -1,26 +1,26 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions; using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection; 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;
using Volo.Abp.Identity; using Volo.Abp.Identity;
namespace Volo.Abp.PermissionManagement.Identity namespace Volo.Abp.PermissionManagement.Identity
{ {
// public class RoleDeletedEventHandler : public class RoleDeletedEventHandler :
// ILocalEventHandler<EntityDeletedEventData<IdentityRole>>, ILocalEventHandler<EntityDeletedEto<IdentityRoleEto>>,
// ITransientDependency ITransientDependency
// { {
// protected IPermissionManager PermissionManager { get; } protected IPermissionManager PermissionManager { get; }
//
// public RoleDeletedEventHandler(IPermissionManager permissionManager) public RoleDeletedEventHandler(IPermissionManager permissionManager)
// { {
// PermissionManager = permissionManager; PermissionManager = permissionManager;
// } }
//
// public virtual async Task HandleEventAsync(EntityDeletedEventData<IdentityRole> eventData) public async Task HandleEventAsync(EntityDeletedEto<IdentityRoleEto> eventData)
// { {
// await PermissionManager.DeleteAsync(RolePermissionValueProvider.ProviderName, eventData.Entity.Name); await PermissionManager.DeleteAsync(RolePermissionValueProvider.ProviderName, eventData.Entity.Name);
// } }
// } }
} }

60
modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs

@ -1,45 +1,33 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions; using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus;
using Volo.Abp.Identity; using Volo.Abp.Identity;
namespace Volo.Abp.PermissionManagement.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 : public async Task HandleEventAsync(IdentityRoleNameChangedEto eventData)
// ILocalEventHandler<IdentityRoleNameChangedEvent>, {
// ITransientDependency var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync(RolePermissionValueProvider.ProviderName, eventData.OldName);
// { foreach (var permissionGrant in permissionGrantsInRole)
// protected IIdentityRoleRepository RoleRepository { get; } {
// protected IPermissionManager PermissionManager { get; } await PermissionManager.UpdateProviderKeyAsync(permissionGrant, eventData.Name);
// 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);
// }
// }
// }
} }

Loading…
Cancel
Save