-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
hikalkan
merged 3 commits into
abpframework:dev
from
mperk:#215-role-name-should-be-unchangeable
Jan 13, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...s/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...nagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/RoleChangingEvents_Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theUpdateProviderKeyAsync
method.Maybe it can use
IPermissionGrantRepository
directly. so some changes below can be undone.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. : )