Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update permissions when a role name is changed #2599

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ public virtual void RemoveClaim([NotNull] Claim claim)
Claims.RemoveAll(c => c.ClaimType == claim.Type && c.ClaimValue == claim.Value);
}

public virtual void ChangeName(string name)
{
Check.NotNullOrWhiteSpace(name, nameof(name));

var oldName = Name;
Name = name;

AddLocalEvent(
new IdentityRoleNameChangedEvent
{
IdentityRole = this,
OldName = oldName
}
);
}

public override string ToString()
{
return $"{base.ToString()}, Name = {Name}";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Volo.Abp.Identity
{
public class IdentityRoleNameChangedEvent
{
public IdentityRole IdentityRole { get; set; }
public string OldName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public Task SetRoleNameAsync([NotNull] IdentityRole role, string roleName, Cance

Check.NotNull(role, nameof(role));

role.Name = roleName;
role.ChangeName(roleName);
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Identity.Domain.Shared\Volo.Abp.Identity.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\permission-management\src\Volo.Abp.PermissionManagement.Domain\Volo.Abp.PermissionManagement.Domain.csproj" />
<ProjectReference Include="..\Volo.Abp.Identity.Domain\Volo.Abp.Identity.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.Identity;

namespace Volo.Abp.PermissionManagement.Identity
{
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 async Task HandleEventAsync(IdentityRoleNameChangedEvent eventData)
{
var role = await RoleRepository.FindAsync(eventData.IdentityRole.Id, false).ConfigureAwait(false);
if (role == null)
{
return;
}

var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync(RolePermissionValueProvider.ProviderName, eventData.OldName).ConfigureAwait(false);
foreach (var permissionGrant in permissionGrantsInRole)
{
await PermissionManager.UpdateProviderKeyAsync(permissionGrant, eventData.IdentityRole.Name).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that the IPermissionManager should add the UpdateProviderKeyAsync method.

Maybe it can use IPermissionGrantRepository directly. so some changes below can be undone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I can move in the repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we have ignored the above discussion. : )

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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
{
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]
public async Task Role_Update_Event_Test()
{
var role = await RoleRepository
.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator"))
.ConfigureAwait(false);

var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name).ConfigureAwait(false);
permissionGrantsInRole.ShouldNotBeNull();
permissionGrantsInRole.Count.ShouldBeGreaterThan(0);
var count = permissionGrantsInRole.Count;

using (var uow = UowManager.Begin())
{
var identityResult = await RoleManager.SetRoleNameAsync(role, "TestModerator").ConfigureAwait(false);
identityResult.Succeeded.ShouldBeTrue();
var xx = await RoleRepository.UpdateAsync(role).ConfigureAwait(false);
await uow.CompleteAsync().ConfigureAwait(false);
}

role = await RoleRepository.GetAsync(role.Id).ConfigureAwait(false);
role.Name.ShouldBe("TestModerator");

permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name).ConfigureAwait(false);
permissionGrantsInRole.Count.ShouldBe(count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManageme
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Web", "src\Volo.Abp.PermissionManagement.Web\Volo.Abp.PermissionManagement.Web.csproj", "{97A386F8-DAE0-4BEA-ADE5-1D57C3336515}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Tests", "test\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Tests.csproj", "{48297098-79D0-413B-939C-6C432142C42D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Domain.Tests", "test\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Domain.Tests.csproj", "{48297098-79D0-413B-939C-6C432142C42D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.TestBase", "test\Volo.Abp.PermissionManagement.TestBase\Volo.Abp.PermissionManagement.TestBase.csproj", "{49259427-CAEB-4FAE-81E4-848F789487EA}"
EndProject
Expand All @@ -33,7 +33,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManageme
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.HttpApi.Client", "src\Volo.Abp.PermissionManagement.HttpApi.Client\Volo.Abp.PermissionManagement.HttpApi.Client.csproj", "{1CD80519-9431-48DB-B0EA-291A73FF9F49}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.PermissionManagement.Application.Tests", "test\Volo.Abp.PermissionManagement.Application.Tests\Volo.Abp.PermissionManagement.Application.Tests.csproj", "{A0F72F5F-3713-4E06-ADB7-15ADFDCB79B1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.PermissionManagement.Application.Tests", "test\Volo.Abp.PermissionManagement.Application.Tests\Volo.Abp.PermissionManagement.Application.Tests.csproj", "{A0F72F5F-3713-4E06-ADB7-15ADFDCB79B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface IPermissionManager
Task<List<PermissionWithGrantedProviders>> GetAllAsync([NotNull] string providerName, [NotNull] string providerKey);

Task SetAsync(string permissionName, string providerName, string providerKey, bool isGranted);

Task<PermissionGrant> UpdateProviderKeyAsync(PermissionGrant permissionGrant, string providerKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PermissionGrant : Entity<Guid>, IMultiTenant
public virtual string ProviderName { get; protected set; }

[CanBeNull]
public virtual string ProviderKey { get; protected set; }
public virtual string ProviderKey { get; protected internal set; }

protected PermissionGrant()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public async Task SetAsync(string permissionName, string providerName, string pr
await provider.SetAsync(permissionName, providerKey, isGranted).ConfigureAwait(false);
}

public async Task<PermissionGrant> UpdateProviderKeyAsync(PermissionGrant permissionGrant, string providerKey)
{
permissionGrant.ProviderKey = providerKey;
return await PermissionGrantRepository.UpdateAsync(permissionGrant).ConfigureAwait(false);
}

protected virtual async Task<PermissionWithGrantedProviders> GetInternalAsync(PermissionDefinition permission, string providerName, string providerKey)
{
var result = new PermissionWithGrantedProviders(permission.Name, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.PermissionManagement.Application\Volo.Abp.PermissionManagement.Application.csproj" />
<ProjectReference Include="..\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Tests.csproj" />
<ProjectReference Include="..\Volo.Abp.PermissionManagement.Tests\Volo.Abp.PermissionManagement.Domain.Tests.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,20 @@ await Assert.ThrowsAsync<AbpException>(async () => await _permissionManager.SetA
true).ConfigureAwait(false)).ConfigureAwait(false);
}

[Fact]
public async Task UpdateProviderKey()
{
await _permissionGrantRepository.InsertAsync(new PermissionGrant(
Guid.NewGuid(),
"MyPermission1",
"Test",
"Test")
).ConfigureAwait(false);
var permissionGrant = await _permissionGrantRepository.FindAsync("MyPermission1", "Test", "Test").ConfigureAwait(false);
permissionGrant.ProviderKey.ShouldBe("Test");

await _permissionManager.UpdateProviderKeyAsync(permissionGrant, "NewProviderKey").ConfigureAwait(false);
(await _permissionGrantRepository.FindAsync("MyPermission1", "Test", "NewProviderKey").ConfigureAwait(false)).ShouldNotBeNull();
}
}
}