Browse Source

Add resource permission checker and store extension methods for entity permissions

pull/24374/head
maliming 9 months ago
parent
commit
2f56610c15
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 59
      framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionCheckerExtensions.cs
  2. 56
      framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionStoreExtensions.cs
  3. 57
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionCheckerExtensions.cs
  4. 52
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionStoreExtensions.cs

59
framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionCheckerExtensions.cs

@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Volo.Abp.Authorization.Permissions.Resources;
public static class ResourcePermissionCheckerExtensions
{
/// <summary>
/// Checks if a specific permission is granted for a resource with a given key.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <param name="resource">The resource instance to check permission for.</param>
/// <param name="resourceKey">The unique key identifying the resource instance.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a boolean value indicating whether the permission is granted.</returns>
public static Task<bool> IsGrantedAsync<TResource>(
this IResourcePermissionChecker resourcePermissionChecker,
string permissionName,
TResource resource,
object resourceKey
)
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNullOrWhiteSpace(permissionName, nameof(permissionName));
Check.NotNull(resource, nameof(resource));
Check.NotNull(resourceKey, nameof(resourceKey));
return resourcePermissionChecker.IsGrantedAsync(
permissionName,
typeof(TResource).FullName!,
resourceKey.ToString()!
);
}
/// <summary>
/// Retrieves a dictionary of permissions and their granted statuses for a specific resource.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="resource">The resource instance for which permissions are being checked.</param>
/// <param name="resourceKey">The unique key identifying the resource instance.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a dictionary where keys are permission names and values indicate whether the corresponding permission is granted.</returns>
public static Task<IDictionary<string, bool>> GetPermissionsAsync<TResource>(
this IResourcePermissionChecker resourcePermissionChecker,
TResource resource,
object resourceKey
)
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNull(resource, nameof(resource));
Check.NotNull(resourceKey, nameof(resourceKey));
return resourcePermissionChecker.GetPermissionsAsync(
typeof(TResource).FullName!,
resourceKey.ToString()!
);
}
}

56
framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionStoreExtensions.cs

@ -5,63 +5,11 @@ namespace Volo.Abp.Authorization.Permissions.Resources;
public static class ResourcePermissionStoreExtensions
{
/// <summary>
/// Checks if a specific permission is granted for a resource with a given key.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <param name="resource">The resource instance to check permission for.</param>
/// <param name="resourceKey">The unique key identifying the resource instance.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a boolean value indicating whether the permission is granted.</returns>
public static Task<bool> IsGrantedAsync<TResource>(
this IResourcePermissionChecker resourcePermissionChecker,
string permissionName,
TResource resource,
object resourceKey
)
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNullOrWhiteSpace(permissionName, nameof(permissionName));
Check.NotNull(resource, nameof(resource));
Check.NotNull(resourceKey, nameof(resourceKey));
return resourcePermissionChecker.IsGrantedAsync(
permissionName,
typeof(TResource).FullName!,
resourceKey.ToString()!
);
}
/// <summary>
/// Retrieves a dictionary of permissions and their granted statuses for a specific resource.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="resource">The resource instance for which permissions are being checked.</param>
/// <param name="resourceKey">The unique key identifying the resource instance.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a dictionary where keys are permission names and values indicate whether the corresponding permission is granted.</returns>
public static Task<IDictionary<string, bool>> GetPermissionsAsync<TResource>(
this IResourcePermissionChecker resourcePermissionChecker,
TResource resource,
object resourceKey
)
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNull(resource, nameof(resource));
Check.NotNull(resourceKey, nameof(resourceKey));
return resourcePermissionChecker.GetPermissionsAsync(
typeof(TResource).FullName!,
resourceKey.ToString()!
);
}
/// <summary>
/// Retrieves the list of granted permissions for a specific resource with a given key.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionStore">The resource permission checker instance.</param>
/// <param name="resourcePermissionStore">The resource permission store instance.</param>
/// <param name="resource">The resource instance to retrieve permissions for.</param>
/// <param name="resourceKey">The unique key identifying the resource instance.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains an array of strings representing the granted permissions.</returns>
@ -85,7 +33,7 @@ public static class ResourcePermissionStoreExtensions
/// Retrieves the keys of the resources granted a specific permission.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="resourcePermissionStore">The resource permission checker instance.</param>
/// <param name="resourcePermissionStore">The resource permission store instance.</param>
/// <param name="resource">The resource instance to check granted permissions for.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains an array of strings representing the granted resource keys.</returns>

57
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionCheckerExtensions.cs

@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Authorization.Permissions.Resources;
public static class EntityResourcePermissionCheckerExtensions
{
/// <summary>
/// Checks if the specified permission is granted for the given entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <param name="entity">The entity for which the permission is being checked.</param>
/// <returns>A task that represents the asynchronous operation. The task result is a boolean indicating whether the permission is granted.</returns>
public static Task<bool> IsGrantedAsync<TEntity>(
this IResourcePermissionChecker resourcePermissionChecker,
string permissionName,
TEntity entity
)
where TEntity : class, IEntity
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNullOrWhiteSpace(permissionName, nameof(permissionName));
Check.NotNull(entity, nameof(entity));
return resourcePermissionChecker.IsGrantedAsync(
permissionName,
typeof(TEntity).FullName!,
entity.GetKeys().JoinAsString(",")
);
}
/// <summary>
/// Retrieves a dictionary of permissions and their granted status for the specified entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="entity">The entity for which the permissions are being retrieved.</param>
/// <returns>A dictionary where the keys are permission names and the values are booleans indicating whether the permission is granted.</returns>
public static Task<IDictionary<string, bool>> GetPermissionsAsync<TEntity>(
this IResourcePermissionChecker resourcePermissionChecker,
TEntity entity
)
where TEntity : class, IEntity
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNull(entity, nameof(entity));
return resourcePermissionChecker.GetPermissionsAsync(
typeof(TEntity).FullName!,
entity.GetKeys().JoinAsString(",")
);
}
}

52
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionStoreExtensions.cs

@ -8,59 +8,11 @@ namespace Volo.Abp.Authorization.Permissions.Resources;
public static class EntityResourcePermissionStoreExtensions
{
/// <summary>
/// Checks if the specified permission is granted for the given entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <param name="entity">The entity for which the permission is being checked.</param>
/// <returns>A task that represents the asynchronous operation. The task result is a boolean indicating whether the permission is granted.</returns>
public static Task<bool> IsGrantedAsync<TEntity>(
this IResourcePermissionChecker resourcePermissionChecker,
string permissionName,
TEntity entity
)
where TEntity : class, IEntity
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNullOrWhiteSpace(permissionName, nameof(permissionName));
Check.NotNull(entity, nameof(entity));
return resourcePermissionChecker.IsGrantedAsync(
permissionName,
typeof(TEntity).FullName!,
entity.GetKeys().JoinAsString(",")
);
}
/// <summary>
/// Retrieves a dictionary of permissions and their granted status for the specified entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="resourcePermissionChecker">The resource permission checker instance.</param>
/// <param name="entity">The entity for which the permissions are being retrieved.</param>
/// <returns>A dictionary where the keys are permission names and the values are booleans indicating whether the permission is granted.</returns>
public static Task<IDictionary<string, bool>> GetPermissionsAsync<TEntity>(
this IResourcePermissionChecker resourcePermissionChecker,
TEntity entity
)
where TEntity : class, IEntity
{
Check.NotNull(resourcePermissionChecker, nameof(resourcePermissionChecker));
Check.NotNull(entity, nameof(entity));
return resourcePermissionChecker.GetPermissionsAsync(
typeof(TEntity).FullName!,
entity.GetKeys().JoinAsString(",")
);
}
/// <summary>
/// Retrieves an array of granted permissions for a specific entity.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="resourcePermissionStore">The resource permission checker instance.</param>
/// <param name="resourcePermissionStore">The resource permission store instance.</param>
/// <param name="entity">The entity for which the permissions are being checked.</param>
/// <returns>An array of granted permission names as strings.</returns>
public static Task<string[]> GetGrantedPermissionsAsync<TEntity>(
@ -83,7 +35,7 @@ public static class EntityResourcePermissionStoreExtensions
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TKey">The type of the entity's primary key.</typeparam>
/// <param name="resourcePermissionStore">The resource permission checker instance.</param>
/// <param name="resourcePermissionStore">The resource permission store instance.</param>
/// <param name="permissionName">The name of the permission to check.</param>
/// <returns>An array of entity IDs (of type <typeparamref name="TKey"/>) for which the permission is granted.</returns>
public static async Task<TKey[]> GetGrantedEntityIdsAsync<TEntity, TKey>(

Loading…
Cancel
Save