diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs index 1c4b162b21..b314ca6b84 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs @@ -165,5 +165,41 @@ namespace Volo.Abp.Reflection property = currentType.GetProperty(properties.Last()); property.SetValue(obj, value); } + + + /// + /// Get all the constant values in the specified type (including the base type). + /// + /// + /// + public static string[] GetPublicConstantsRecursively(Type type) + { + const int maxRecursiveParameterValidationDepth = 8; + + var publicConstants = new List(); + + void Recursively(List constants, Type targetType, int currentDepth) + { + if (currentDepth > maxRecursiveParameterValidationDepth) + { + return; + } + + constants.AddRange(targetType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) + .Where(x => x.IsLiteral && !x.IsInitOnly) + .Select(x => x.GetValue(null).ToString())); + + var nestedTypes = targetType.GetNestedTypes(BindingFlags.Public); + + foreach (var nestedType in nestedTypes) + { + Recursively(constants, nestedType, currentDepth + 1); + } + } + + Recursively(publicConstants, type, 1); + + return publicConstants.ToArray(); + } } } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs index 842940cfaf..333ea2e84a 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs @@ -1,7 +1,102 @@ -namespace Volo.Abp.Reflection +using System.Linq; +using Shouldly; +using Xunit; + +namespace Volo.Abp.Reflection { public class ReflectionHelper_Tests { //TODO: ... + + + [Fact] + public void GetPublicConstantsRecursively_Test() + { + var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(BaseRole)); + + constants.ShouldNotBeEmpty(); + constants.Length.ShouldBe(1); + constants.ShouldContain(x => x == "DefaultBaseRoleName"); + } + + [Fact] + public void GetPublicConstantsRecursively_Inherit_Test() + { + var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(Roles)); + + constants.ShouldNotBeEmpty(); + constants.Length.ShouldBe(2); + constants.ShouldContain(x => x == "DefaultBaseRoleName"); + constants.ShouldContain(x => x == "DefaultRoleName"); + } + + + [Fact] + public void GetPublicConstantsRecursively_NestedTypes_Test() + { + var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(IdentityPermissions)); + + constants.ShouldNotBeEmpty(); + constants.Except(IdentityPermissions.GetAll()).Count().ShouldBe(0); + } } -} + + + public class BaseRole + { + public const string BaseRoleName = "DefaultBaseRoleName"; + } + + public class Roles : BaseRole + { + public const string RoleName = "DefaultRoleName"; + } + + public static class IdentityPermissions + { + public const string GroupName = "AbpIdentity"; + + public static class Roles + { + public const string Default = GroupName + ".Roles"; + public const string Create = Default + ".Create"; + public const string Update = Default + ".Update"; + public const string Delete = Default + ".Delete"; + public const string ManagePermissions = Default + ".ManagePermissions"; + } + + public static class Users + { + public const string Default = GroupName + ".Users"; + public const string Create = Default + ".Create"; + public const string Update = Default + ".Update"; + public const string Delete = Default + ".Delete"; + public const string ManagePermissions = Default + ".ManagePermissions"; + } + + public static class UserLookup + { + public const string Default = GroupName + ".UserLookup"; + } + + public static string[] GetAll() + { + return new[] + { + GroupName, + Roles.Default, + Roles.Create, + Roles.Update, + Roles.Delete, + Roles.ManagePermissions, + Users.Default, + Users.Create, + Users.Update, + Users.Delete, + Users.ManagePermissions, + UserLookup.Default + }; + } + } + +} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/BloggingPermissions.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/BloggingPermissions.cs index e73dc334da..fae85acf88 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/BloggingPermissions.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/BloggingPermissions.cs @@ -1,4 +1,6 @@ -namespace Volo.Blogging +using Volo.Abp.Reflection; + +namespace Volo.Blogging { public class BloggingPermissions { @@ -39,27 +41,7 @@ public static string[] GetAll() { - return new[] - { - GroupName, - Blogs.Default, - Blogs.Management, - Blogs.Delete, - Blogs.Update, - Blogs.Create, - Posts.Default, - Posts.Delete, - Posts.Update, - Posts.Create, - Tags.Default, - Tags.Delete, - Tags.Update, - Tags.Create, - Comments.Default, - Comments.Delete, - Comments.Update, - Comments.Create - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(BloggingPermissions)); } } } diff --git a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/DocsAdminPermissions.cs b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/DocsAdminPermissions.cs index 565b70c7f5..341f4c5819 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/DocsAdminPermissions.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/DocsAdminPermissions.cs @@ -1,4 +1,6 @@ -namespace Volo.Docs.Admin +using Volo.Abp.Reflection; + +namespace Volo.Docs.Admin { public class DocsAdminPermissions { @@ -14,14 +16,7 @@ public static string[] GetAll() { - return new[] - { - GroupName, - Projects.Default, - Projects.Delete, - Projects.Update, - Projects.Create, - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(DocsAdminPermissions)); } } } diff --git a/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityPermissions.cs b/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityPermissions.cs index 339122c2a8..e22869295f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityPermissions.cs +++ b/modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityPermissions.cs @@ -1,4 +1,6 @@ -namespace Volo.Abp.Identity +using Volo.Abp.Reflection; + +namespace Volo.Abp.Identity { public static class IdentityPermissions { @@ -29,21 +31,7 @@ public static string[] GetAll() { - return new[] - { - GroupName, - Roles.Default, - Roles.Create, - Roles.Update, - Roles.Delete, - Roles.ManagePermissions, - Users.Default, - Users.Create, - Users.Update, - Users.Delete, - Users.ManagePermissions, - UserLookup.Default - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(IdentityPermissions)); } } } \ No newline at end of file diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/TenantManagementPermissions.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/TenantManagementPermissions.cs index 6bc90271bc..49afb47c25 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/TenantManagementPermissions.cs +++ b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/TenantManagementPermissions.cs @@ -1,4 +1,6 @@ -namespace Volo.Abp.TenantManagement +using Volo.Abp.Reflection; + +namespace Volo.Abp.TenantManagement { public static class TenantManagementPermissions { @@ -16,16 +18,7 @@ public static string[] GetAll() { - return new[] - { - GroupName, - Tenants.Default, - Tenants.Create, - Tenants.Update, - Tenants.Delete, - Tenants.ManageFeatures, - Tenants.ManageConnectionStrings - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(TenantManagementPermissions)); } } } \ No newline at end of file diff --git a/samples/BookStore/src/Acme.BookStore.Application/Permissions/BookStorePermissions.cs b/samples/BookStore/src/Acme.BookStore.Application/Permissions/BookStorePermissions.cs index b1ec575904..ea8a26d6ef 100644 --- a/samples/BookStore/src/Acme.BookStore.Application/Permissions/BookStorePermissions.cs +++ b/samples/BookStore/src/Acme.BookStore.Application/Permissions/BookStorePermissions.cs @@ -1,4 +1,5 @@ using System; +using Volo.Abp.Reflection; namespace Acme.BookStore.Permissions { @@ -12,7 +13,7 @@ namespace Acme.BookStore.Permissions public static string[] GetAll() { //Return an array of all permissions - return Array.Empty(); + return ReflectionHelper.GetPublicConstantsRecursively(typeof(BookStorePermissions)); } } } \ No newline at end of file diff --git a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Application.Contracts/ProductManagement/ProductManagementPermissions.cs b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Application.Contracts/ProductManagement/ProductManagementPermissions.cs index b4dd949a73..eebc8cb29c 100644 --- a/samples/MicroserviceDemo/modules/product/src/ProductManagement.Application.Contracts/ProductManagement/ProductManagementPermissions.cs +++ b/samples/MicroserviceDemo/modules/product/src/ProductManagement.Application.Contracts/ProductManagement/ProductManagementPermissions.cs @@ -1,4 +1,6 @@ -namespace ProductManagement +using Volo.Abp.Reflection; + +namespace ProductManagement { public class ProductManagementPermissions { @@ -14,14 +16,7 @@ } public static string[] GetAll() { - return new[] - { - GroupName, - Products.Default, - Products.Delete, - Products.Update, - Products.Create - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(ProductManagementPermissions)); } } } \ No newline at end of file diff --git a/templates/module/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs b/templates/module/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs index 88e7a3c05b..00d1124498 100644 --- a/templates/module/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs +++ b/templates/module/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs @@ -1,4 +1,6 @@ -namespace MyCompanyName.MyProjectName +using Volo.Abp.Reflection; + +namespace MyCompanyName.MyProjectName { public class MyProjectNamePermissions { @@ -6,10 +8,7 @@ public static string[] GetAll() { - return new[] - { - GroupName - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(MyProjectNamePermissions)); } } } \ No newline at end of file diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Application/Permissions/MyProjectNamePermissions.cs b/templates/mvc/src/MyCompanyName.MyProjectName.Application/Permissions/MyProjectNamePermissions.cs index 4e499ae2ed..0646692500 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Application/Permissions/MyProjectNamePermissions.cs +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Application/Permissions/MyProjectNamePermissions.cs @@ -1,4 +1,5 @@ using System; +using Volo.Abp.Reflection; namespace MyCompanyName.MyProjectName.Permissions { @@ -12,7 +13,7 @@ namespace MyCompanyName.MyProjectName.Permissions public static string[] GetAll() { //Return an array of all permissions - return Array.Empty(); + return ReflectionHelper.GetPublicConstantsRecursively(typeof(MyProjectNamePermissions)); } } } \ No newline at end of file diff --git a/templates/service/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs b/templates/service/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs index 88e7a3c05b..00d1124498 100644 --- a/templates/service/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs +++ b/templates/service/src/MyCompanyName.MyProjectName.Application.Contracts/MyCompanyName/MyProjectName/MyProjectNamePermissions.cs @@ -1,4 +1,6 @@ -namespace MyCompanyName.MyProjectName +using Volo.Abp.Reflection; + +namespace MyCompanyName.MyProjectName { public class MyProjectNamePermissions { @@ -6,10 +8,7 @@ public static string[] GetAll() { - return new[] - { - GroupName - }; + return ReflectionHelper.GetPublicConstantsRecursively(typeof(MyProjectNamePermissions)); } } } \ No newline at end of file