From 2f56610c15db5ba50be5b2f1da0fb5cebae835af Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 13 Nov 2025 15:31:47 +0800 Subject: [PATCH] Add resource permission checker and store extension methods for entity permissions --- .../ResourcePermissionCheckerExtensions.cs | 59 +++++++++++++++++++ .../ResourcePermissionStoreExtensions.cs | 56 +----------------- ...tityResourcePermissionCheckerExtensions.cs | 57 ++++++++++++++++++ ...EntityResourcePermissionStoreExtensions.cs | 52 +--------------- 4 files changed, 120 insertions(+), 104 deletions(-) create mode 100644 framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionCheckerExtensions.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionCheckerExtensions.cs diff --git a/framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionCheckerExtensions.cs b/framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionCheckerExtensions.cs new file mode 100644 index 0000000000..3a5fad9d7d --- /dev/null +++ b/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 +{ + /// + /// Checks if a specific permission is granted for a resource with a given key. + /// + /// The type of the resource. + /// The resource permission checker instance. + /// The name of the permission to check. + /// The resource instance to check permission for. + /// The unique key identifying the resource instance. + /// A task that represents the asynchronous operation. The task result contains a boolean value indicating whether the permission is granted. + public static Task IsGrantedAsync( + 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()! + ); + } + + /// + /// Retrieves a dictionary of permissions and their granted statuses for a specific resource. + /// + /// The type of the resource. + /// The resource permission checker instance. + /// The resource instance for which permissions are being checked. + /// The unique key identifying the resource instance. + /// 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. + public static Task> GetPermissionsAsync( + 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()! + ); + } +} diff --git a/framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionStoreExtensions.cs b/framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionStoreExtensions.cs index ad2c033755..6ed817c232 100644 --- a/framework/src/Volo.Abp.Authorization.Abstractions/Volo/Abp/Authorization/Permissions/Resources/ResourcePermissionStoreExtensions.cs +++ b/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 { - /// - /// Checks if a specific permission is granted for a resource with a given key. - /// - /// The type of the resource. - /// The resource permission checker instance. - /// The name of the permission to check. - /// The resource instance to check permission for. - /// The unique key identifying the resource instance. - /// A task that represents the asynchronous operation. The task result contains a boolean value indicating whether the permission is granted. - public static Task IsGrantedAsync( - 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()! - ); - } - - /// - /// Retrieves a dictionary of permissions and their granted statuses for a specific resource. - /// - /// The type of the resource. - /// The resource permission checker instance. - /// The resource instance for which permissions are being checked. - /// The unique key identifying the resource instance. - /// 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. - public static Task> GetPermissionsAsync( - 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()! - ); - } - /// /// Retrieves the list of granted permissions for a specific resource with a given key. /// /// The type of the resource. - /// The resource permission checker instance. + /// The resource permission store instance. /// The resource instance to retrieve permissions for. /// The unique key identifying the resource instance. /// A task that represents the asynchronous operation. The task result contains an array of strings representing the granted permissions. @@ -85,7 +33,7 @@ public static class ResourcePermissionStoreExtensions /// Retrieves the keys of the resources granted a specific permission. /// /// The type of the resource. - /// The resource permission checker instance. + /// The resource permission store instance. /// The resource instance to check granted permissions for. /// The name of the permission to check. /// A task that represents the asynchronous operation. The task result contains an array of strings representing the granted resource keys. diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionCheckerExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionCheckerExtensions.cs new file mode 100644 index 0000000000..28f72f2abe --- /dev/null +++ b/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 +{ + /// + /// Checks if the specified permission is granted for the given entity. + /// + /// The type of the entity. + /// The resource permission checker instance. + /// The name of the permission to check. + /// The entity for which the permission is being checked. + /// A task that represents the asynchronous operation. The task result is a boolean indicating whether the permission is granted. + public static Task IsGrantedAsync( + 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(",") + ); + } + + /// + /// Retrieves a dictionary of permissions and their granted status for the specified entity. + /// + /// The type of the entity. + /// The resource permission checker instance. + /// The entity for which the permissions are being retrieved. + /// A dictionary where the keys are permission names and the values are booleans indicating whether the permission is granted. + public static Task> GetPermissionsAsync( + 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(",") + ); + } + +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionStoreExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionStoreExtensions.cs index b326df7cb4..90a27a246b 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Authorization/Permissions/Resources/EntityResourcePermissionStoreExtensions.cs +++ b/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 { - /// - /// Checks if the specified permission is granted for the given entity. - /// - /// The type of the entity. - /// The resource permission checker instance. - /// The name of the permission to check. - /// The entity for which the permission is being checked. - /// A task that represents the asynchronous operation. The task result is a boolean indicating whether the permission is granted. - public static Task IsGrantedAsync( - 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(",") - ); - } - - /// - /// Retrieves a dictionary of permissions and their granted status for the specified entity. - /// - /// The type of the entity. - /// The resource permission checker instance. - /// The entity for which the permissions are being retrieved. - /// A dictionary where the keys are permission names and the values are booleans indicating whether the permission is granted. - public static Task> GetPermissionsAsync( - 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(",") - ); - } - /// /// Retrieves an array of granted permissions for a specific entity. /// /// The type of the entity. - /// The resource permission checker instance. + /// The resource permission store instance. /// The entity for which the permissions are being checked. /// An array of granted permission names as strings. public static Task GetGrantedPermissionsAsync( @@ -83,7 +35,7 @@ public static class EntityResourcePermissionStoreExtensions /// /// The type of the entity. /// The type of the entity's primary key. - /// The resource permission checker instance. + /// The resource permission store instance. /// The name of the permission to check. /// An array of entity IDs (of type ) for which the permission is granted. public static async Task GetGrantedEntityIdsAsync(