Skip to content

Commit

Permalink
Merge pull request #17342 from abpframework/IPermissionFinder
Browse files Browse the repository at this point in the history
Introduce `IPermissionFinder`.
  • Loading branch information
hikalkan authored Aug 10, 2023
2 parents 525ab12 + 38b96ec commit 400c1fd
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Volo.Abp.Identity.Integration;

public class IdentityUserIntegrationService : IdentityAppServiceBase, IIdentityUserIntegrationService
{
protected IIdentityUserRepository UserRepository { get; }
protected IUserRoleFinder UserRoleFinder { get; }

public IdentityUserIntegrationService(IIdentityUserRepository userRepository)
public IdentityUserIntegrationService(IUserRoleFinder userRoleFinder)
{
UserRepository = userRepository;
UserRoleFinder = userRoleFinder;
}

public async Task<string[]> GetRoleNamesAsync(Guid id)
public virtual async Task<string[]> GetRoleNamesAsync(Guid id)
{
return (await UserRepository.GetRoleNamesAsync(id)).ToArray();
return await UserRoleFinder.GetRoleNamesAsync(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Volo.Abp.PermissionManagement.Integration;
[IntegrationService]
public interface IPermissionIntegrationService : IApplicationService
{
Task<ListResultDto<PermissionGrantOutput>> IsGrantedAsync(List<PermissionGrantInput> input);
Task<ListResultDto<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> input);
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization.Permissions;

namespace Volo.Abp.PermissionManagement.Integration;

[IntegrationService]
public class PermissionIntegrationService : ApplicationService, IPermissionIntegrationService
{
protected IPermissionManager PermissionManager { get; }
protected IPermissionFinder PermissionFinder { get; }

public PermissionIntegrationService(IPermissionManager permissionManager)
public PermissionIntegrationService(IPermissionFinder permissionFinder)
{
PermissionManager = permissionManager;
PermissionFinder = permissionFinder;
}

public virtual async Task<ListResultDto<PermissionGrantOutput>> IsGrantedAsync(List<PermissionGrantInput> input)
public virtual async Task<ListResultDto<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> input)
{
var result = new List<PermissionGrantOutput>();
foreach (var item in input)
{
result.Add(new PermissionGrantOutput
{
UserId = item.UserId,
Permissions = (await PermissionManager.GetAsync(item.PermissionNames, UserPermissionValueProvider.ProviderName, item.UserId.ToString())).Result
.ToDictionary(x => x.Name, x => x.IsGranted)
});
}

return new ListResultDto<PermissionGrantOutput>(result);
return new ListResultDto<IsGrantedResponse>(await PermissionFinder.IsGrantedAsync(input));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Volo.Abp.PermissionManagement;

public interface IPermissionFinder
{
Task<List<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> requests);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

namespace Volo.Abp.PermissionManagement.Integration;
namespace Volo.Abp.PermissionManagement;

public class PermissionGrantInput
public class IsGrantedRequest
{
public Guid UserId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;

namespace Volo.Abp.PermissionManagement.Integration;
namespace Volo.Abp.PermissionManagement;

public class PermissionGrantOutput
public class IsGrantedResponse
{
public Guid UserId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Volo.Abp.PermissionManagement;

public static class PermissionFinderExtensions
{
public async static Task<bool> IsGrantedAsync(this IPermissionFinder permissionFinder, Guid userId, string permissionName)
{
return await permissionFinder.IsGrantedAsync(userId, new[] { permissionName });
}

public async static Task<bool> IsGrantedAsync(this IPermissionFinder permissionFinder, Guid userId, string[] permissionNames)
{
return (await permissionFinder.IsGrantedAsync(new List<IsGrantedRequest>
{
new IsGrantedRequest
{
UserId = userId,
PermissionNames = permissionNames
}
})).Any(x => x.UserId == userId && x.Permissions.All(p => permissionNames.Contains(p.Key) && p.Value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;

namespace Volo.Abp.PermissionManagement;

public class PermissionFinder : IPermissionFinder, ITransientDependency
{
protected IPermissionManager PermissionManager { get; }

public PermissionFinder(IPermissionManager permissionManager)
{
PermissionManager = permissionManager;
}

public virtual async Task<List<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> requests)
{
var result = new List<IsGrantedResponse>();
foreach (var item in requests)
{
result.Add(new IsGrantedResponse
{
UserId = item.UserId,
Permissions = (await PermissionManager.GetAsync(item.PermissionNames, UserPermissionValueProvider.ProviderName, item.UserId.ToString())).Result
.ToDictionary(x => x.Name, x => x.IsGranted)
});
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.ClientProxying;
using Volo.Abp.Http.Modeling;
using Volo.Abp.PermissionManagement;
using Volo.Abp.PermissionManagement.Integration;

// ReSharper disable once CheckNamespace
Expand All @@ -18,11 +19,11 @@ namespace Volo.Abp.PermissionManagement.Integration;
[IntegrationService]
public partial class PermissionIntegrationClientProxy : ClientProxyBase<IPermissionIntegrationService>, IPermissionIntegrationService
{
public virtual async Task<ListResultDto<PermissionGrantOutput>> IsGrantedAsync(List<PermissionGrantInput> input)
public virtual async Task<ListResultDto<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> input)
{
return await RequestAsync<ListResultDto<PermissionGrantOutput>>(nameof(IsGrantedAsync), new ClientProxyRequestTypeValue
return await RequestAsync<ListResultDto<IsGrantedResponse>>(nameof(IsGrantedAsync), new ClientProxyRequestTypeValue
{
{ typeof(List<PermissionGrantInput>), input }
{ typeof(List<IsGrantedRequest>), input }
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,16 @@
"parametersOnMethod": [
{
"name": "input",
"typeAsString": "System.Collections.Generic.List`1[[Volo.Abp.PermissionManagement.Integration.PermissionGrantInput, Volo.Abp.PermissionManagement.Application.Contracts, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib",
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.Integration.PermissionGrantInput>",
"typeSimple": "[Volo.Abp.PermissionManagement.Integration.PermissionGrantInput]",
"typeAsString": "System.Collections.Generic.List`1[[Volo.Abp.PermissionManagement.IsGrantedRequest, Volo.Abp.PermissionManagement.Domain.Shared, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib",
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.IsGrantedRequest>",
"typeSimple": "[Volo.Abp.PermissionManagement.IsGrantedRequest]",
"isOptional": false,
"defaultValue": null
}
],
"returnValue": {
"type": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.Integration.PermissionGrantOutput>",
"typeSimple": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.Integration.PermissionGrantOutput>"
"type": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.IsGrantedResponse>",
"typeSimple": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.IsGrantedResponse>"
}
}
]
Expand All @@ -256,9 +256,9 @@
"parametersOnMethod": [
{
"name": "input",
"typeAsString": "System.Collections.Generic.List`1[[Volo.Abp.PermissionManagement.Integration.PermissionGrantInput, Volo.Abp.PermissionManagement.Application.Contracts, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib",
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.Integration.PermissionGrantInput>",
"typeSimple": "[Volo.Abp.PermissionManagement.Integration.PermissionGrantInput]",
"typeAsString": "System.Collections.Generic.List`1[[Volo.Abp.PermissionManagement.IsGrantedRequest, Volo.Abp.PermissionManagement.Domain.Shared, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib",
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.IsGrantedRequest>",
"typeSimple": "[Volo.Abp.PermissionManagement.IsGrantedRequest]",
"isOptional": false,
"defaultValue": null
}
Expand All @@ -268,8 +268,8 @@
"nameOnMethod": "input",
"name": "input",
"jsonName": null,
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.Integration.PermissionGrantInput>",
"typeSimple": "[Volo.Abp.PermissionManagement.Integration.PermissionGrantInput]",
"type": "System.Collections.Generic.List<Volo.Abp.PermissionManagement.IsGrantedRequest>",
"typeSimple": "[Volo.Abp.PermissionManagement.IsGrantedRequest]",
"isOptional": false,
"defaultValue": null,
"constraintTypes": null,
Expand All @@ -278,8 +278,8 @@
}
],
"returnValue": {
"type": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.Integration.PermissionGrantOutput>",
"typeSimple": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.Integration.PermissionGrantOutput>"
"type": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.IsGrantedResponse>",
"typeSimple": "Volo.Abp.Application.Dtos.ListResultDto<Volo.Abp.PermissionManagement.IsGrantedResponse>"
},
"allowAnonymous": null,
"implementFrom": "Volo.Abp.PermissionManagement.Integration.IPermissionIntegrationService"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.PermissionManagement.Integration;

namespace Volo.Abp.PermissionManagement;

[Dependency(TryRegister = true)]
public class HttpClientPermissionFinder : IPermissionFinder, ITransientDependency
{
protected IPermissionIntegrationService PermissionIntegrationService { get; }

public HttpClientPermissionFinder(IPermissionIntegrationService permissionIntegrationService)
{
PermissionIntegrationService = permissionIntegrationService;
}

public virtual async Task<List<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> requests)
{
return (await PermissionIntegrationService.IsGrantedAsync(requests)).Items.ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PermissionIntegrationController(IPermissionIntegrationService permissionI

[HttpGet]
[Route("is-granted")]
public virtual Task<ListResultDto<PermissionGrantOutput>> IsGrantedAsync(List<PermissionGrantInput> input)
public virtual Task<ListResultDto<IsGrantedResponse>> IsGrantedAsync(List<IsGrantedRequest> input)
{
return PermissionIntegrationService.IsGrantedAsync(input);
}
Expand Down

0 comments on commit 400c1fd

Please sign in to comment.