Merge pull request #1059 from abpframework/maliming/constants

ReflectionHelper add GetAllConstants method.
pull/1068/head
Halil İbrahim Kalkan 7 years ago committed by GitHub
commit 0fcc1aee3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -165,5 +165,41 @@ namespace Volo.Abp.Reflection
property = currentType.GetProperty(properties.Last());
property.SetValue(obj, value);
}
/// <summary>
/// Get all the constant values in the specified type (including the base type).
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string[] GetPublicConstantsRecursively(Type type)
{
const int maxRecursiveParameterValidationDepth = 8;
var publicConstants = new List<string>();
void Recursively(List<string> 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();
}
}
}

@ -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
};
}
}
}

@ -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));
}
}
}

@ -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));
}
}
}

@ -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));
}
}
}

@ -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));
}
}
}

@ -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<string>();
return ReflectionHelper.GetPublicConstantsRecursively(typeof(BookStorePermissions));
}
}
}

@ -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));
}
}
}

@ -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));
}
}
}

@ -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<string>();
return ReflectionHelper.GetPublicConstantsRecursively(typeof(MyProjectNamePermissions));
}
}
}

@ -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));
}
}
}
Loading…
Cancel
Save