-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MenuContributorBase to check permissions in advance.
Resolve #7788
- Loading branch information
Showing
7 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuContributorBase.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,26 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
|
||
namespace Volo.Abp.UI.Navigation | ||
{ | ||
public abstract class MenuContributorBase : IMenuContributor | ||
{ | ||
public List<string> PreCheckPermissions { get; } | ||
public IReadOnlyList<string> GrantedPermissions => _grantedPermissions.ToImmutableList(); | ||
private readonly List<string> _grantedPermissions; | ||
|
||
protected MenuContributorBase() | ||
{ | ||
PreCheckPermissions = new List<string>(); | ||
_grantedPermissions = new List<string>(); | ||
} | ||
|
||
public void AddGrantedPermission(string permission) | ||
{ | ||
_grantedPermissions.Add(permission); | ||
} | ||
|
||
public abstract Task ConfigureMenuAsync(MenuConfigurationContext context); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/FakePermissionStore.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,28 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Authorization.Permissions; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace Volo.Abp.Authorization | ||
{ | ||
public class FakePermissionStore : IPermissionStore, ITransientDependency | ||
{ | ||
public Task<bool> IsGrantedAsync(string name, string providerName, string providerKey) | ||
{ | ||
var result = name == "MenuPermission1" || name == "MenuPermission4"; | ||
return Task.FromResult(result); | ||
} | ||
|
||
public Task<MultiplePermissionGrantResult> IsGrantedAsync(string[] names, string providerName, string providerKey) | ||
{ | ||
var result = new MultiplePermissionGrantResult(); | ||
foreach (var name in names) | ||
{ | ||
result.Result.Add(name, name == "MenuPermission1" || name == "MenuPermission4" | ||
? PermissionGrantResult.Granted | ||
: PermissionGrantResult.Prohibited); | ||
} | ||
|
||
return Task.FromResult(result); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...est/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/MenuContributorPermission_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,73 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shouldly; | ||
using Volo.Abp.UI.Navigation; | ||
using Xunit; | ||
|
||
namespace Volo.Abp.Authorization | ||
{ | ||
public class MenuContributorPermission_Test : AuthorizationTestBase | ||
{ | ||
public static readonly List<string> GrantedPermissions = new List<string>(); | ||
private readonly IMenuManager _menuManager; | ||
|
||
public MenuContributorPermission_Test() | ||
{ | ||
_menuManager = GetRequiredService<IMenuManager>(); | ||
} | ||
|
||
protected override void AfterAddApplication(IServiceCollection services) | ||
{ | ||
services.Configure<AbpNavigationOptions>(options => | ||
{ | ||
options.MenuContributors.Add(new TestMenuContributor1()); | ||
options.MenuContributors.Add(new TestMenuContributor2()); | ||
}); | ||
|
||
base.AfterAddApplication(services); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Check_MenuContributor_PreCheckPermissions() | ||
{ | ||
await _menuManager.GetAsync(StandardMenus.Main); | ||
|
||
GrantedPermissions.Count.ShouldBe(2); | ||
GrantedPermissions.ShouldContain("MenuPermission1"); | ||
GrantedPermissions.ShouldContain("MenuPermission4"); | ||
} | ||
|
||
class TestMenuContributor1 : MenuContributorBase | ||
{ | ||
public TestMenuContributor1() | ||
{ | ||
PreCheckPermissions.Add("MenuPermission1"); | ||
PreCheckPermissions.Add("MenuPermission2"); | ||
} | ||
|
||
public override Task ConfigureMenuAsync(MenuConfigurationContext context) | ||
{ | ||
MenuContributorPermission_Test.GrantedPermissions.AddRange(GrantedPermissions); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
class TestMenuContributor2 : MenuContributorBase | ||
{ | ||
public TestMenuContributor2() | ||
{ | ||
PreCheckPermissions.Add("MenuPermission3"); | ||
PreCheckPermissions.Add("MenuPermission4"); | ||
} | ||
|
||
public override Task ConfigureMenuAsync(MenuConfigurationContext context) | ||
{ | ||
MenuContributorPermission_Test.GrantedPermissions.AddRange(GrantedPermissions); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
} |
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