mirror of https://github.com/abpframework/abp.git
12 changed files with 201 additions and 11 deletions
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Session; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public static class UserPermissionManagerExtensions |
|||
{ |
|||
public static Task<bool> IsGrantedForUserAsync(this IPermissionManager permissionManager, [NotNull] string name, Guid userId, bool fallback = true) |
|||
{ |
|||
return permissionManager.IsGrantedAsync(name, UserPermissionValueProvider.ProviderName, userId.ToString(), fallback); |
|||
} |
|||
|
|||
public static Task<bool> IsGrantedForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool fallback = true) |
|||
{ |
|||
return permissionManager.IsGrantedAsync(name, UserPermissionValueProvider.ProviderName, null, fallback); |
|||
} |
|||
|
|||
public static Task<List<PermissionGrantInfo>> GetAllForUserAsync(this IPermissionManager permissionManager, Guid userId, bool fallback = true) |
|||
{ |
|||
return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, userId.ToString(), fallback); |
|||
} |
|||
|
|||
public static Task<List<PermissionGrantInfo>> GetAllForCurrentUserAsync(this IPermissionManager permissionManager, bool fallback = true) |
|||
{ |
|||
return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, null, fallback); |
|||
} |
|||
|
|||
public static Task SetForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name, bool? isGranted, bool forceToSet = false) |
|||
{ |
|||
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, userId.ToString(), forceToSet); |
|||
} |
|||
|
|||
public static Task SetForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool? isGranted, bool forceToSet = false) |
|||
{ |
|||
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, null, forceToSet); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Permissions; |
|||
|
|||
namespace Volo.Abp.Session |
|||
{ |
|||
public class UserPermissionValueProvider : PermissionValueProvider |
|||
{ |
|||
public const string ProviderName = "User"; |
|||
|
|||
public override string Name => ProviderName; |
|||
|
|||
protected ICurrentUser CurrentUser { get; } |
|||
|
|||
public UserPermissionValueProvider(IPermissionStore permissionStore, ICurrentUser currentUser) |
|||
: base(permissionStore) |
|||
{ |
|||
CurrentUser = currentUser; |
|||
} |
|||
|
|||
public override async Task<bool?> IsGrantedAsync(PermissionDefinition permission, string providerKey) |
|||
{ |
|||
if (providerKey == null) |
|||
{ |
|||
if (CurrentUser.Id == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
providerKey = CurrentUser.Id.ToString(); |
|||
} |
|||
|
|||
return await PermissionStore.IsGrantedAsync(permission.Name, Name, providerKey); |
|||
} |
|||
|
|||
public override Task SetAsync(PermissionDefinition permission, bool isGranted, string providerKey) |
|||
{ |
|||
return PermissionStore.SetAsync(permission.Name, isGranted, Name, providerKey); |
|||
} |
|||
|
|||
public override Task ClearAsync(PermissionDefinition permission, string providerKey) |
|||
{ |
|||
return PermissionStore.DeleteAsync(permission.Name, Name, providerKey); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Session; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Permissions |
|||
{ |
|||
public class PermissionManager_User_Tests : AbpPermissionTestBase |
|||
{ |
|||
private readonly IPermissionManager _permissionManager; |
|||
|
|||
private Guid? _currentUserId; |
|||
|
|||
public PermissionManager_User_Tests() |
|||
{ |
|||
_permissionManager = GetRequiredService<IPermissionManager>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
var currentUser = Substitute.For<ICurrentUser>(); |
|||
currentUser.Id.Returns(ci => _currentUserId); |
|||
services.AddSingleton(currentUser); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_True_For_Granted_User() |
|||
{ |
|||
(await _permissionManager.IsGrantedForUserAsync( |
|||
"MyPermission1", |
|||
AbpPermissionTestDataBuilder.User1Id |
|||
)).ShouldBeTrue(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_True_For_Granted_Current_User() |
|||
{ |
|||
_currentUserId = AbpPermissionTestDataBuilder.User1Id; |
|||
|
|||
(await _permissionManager.IsGrantedForCurrentUserAsync( |
|||
"MyPermission1" |
|||
)).ShouldBeTrue(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_False_For_Non_Granted_User() |
|||
{ |
|||
(await _permissionManager.IsGrantedForUserAsync( |
|||
"MyPermission1", |
|||
AbpPermissionTestDataBuilder.User2Id |
|||
)).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_False_For_Non_Granted_Current_User() |
|||
{ |
|||
_currentUserId = AbpPermissionTestDataBuilder.User2Id; |
|||
|
|||
(await _permissionManager.IsGrantedForCurrentUserAsync( |
|||
"MyPermission1" |
|||
)).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Return_False_For_Current_User_If_Anonymous() |
|||
{ |
|||
(await _permissionManager.IsGrantedForCurrentUserAsync( |
|||
"MyPermission1" |
|||
)).ShouldBeFalse(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue