71 lines
2.6 KiB
C#
71 lines
2.6 KiB
C#
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using Suspectus.Gandalf.Palantir.Data.Entities.Security.Permission.Data;
|
|
using Suspectus.Gandalf.Palantir.Data.MasterData.Attributes;
|
|
|
|
namespace Suspectus.Gandalf.Palantir.Data.MasterData;
|
|
|
|
public static class Groups
|
|
{
|
|
[Group(AuthorityType.Tenant, "Housemaster", oldKey: "")]
|
|
[Description("Housemaster group, has full control over the tenant.")]
|
|
[HasRoles(Roles.TenantHousemaster)]
|
|
public const string TenantHousemaster = "Housemaster";
|
|
|
|
[Group(AuthorityType.App, "Housemaster")]
|
|
[Description("Housemaster group, has full control over the app.")]
|
|
[HasRoles(Roles.AppHousemaster)]
|
|
public const string AppHousemaster = "Housemaster";
|
|
|
|
public static List<MasterGroupData> GetAllGroups()
|
|
{
|
|
var groups = new List<FieldInfo>();
|
|
CollectFields(typeof(Groups));
|
|
|
|
return groups
|
|
.Select(x => new MasterGroupData
|
|
{
|
|
Name = (string)x.GetValue(null)!,
|
|
Type = x.GetCustomAttribute<GroupAttribute>()!.Type,
|
|
OldName = x.GetCustomAttribute<GroupAttribute>()!.OldKey,
|
|
Path = x.GetCustomAttribute<GroupAttribute>()!.Path,
|
|
Description = x.GetCustomAttribute<DescriptionAttribute>()?.Description ?? null,
|
|
Roles = GetRoles(x)
|
|
}).ToList();
|
|
|
|
IReadOnlyList<string> GetRoles(FieldInfo field)
|
|
{
|
|
var hasMasterRolesAttribute = field.GetCustomAttribute<HasRolesAttribute>();
|
|
|
|
if (hasMasterRolesAttribute is null)
|
|
return [];
|
|
|
|
if (hasMasterRolesAttribute.AllRoles)
|
|
return Roles.GetAllRoles().Select(x => x.Name).ToList();
|
|
|
|
return hasMasterRolesAttribute.Roles;
|
|
}
|
|
|
|
void CollectFields(Type type)
|
|
{
|
|
var fields = type
|
|
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
|
|
.Where(f => f.FieldType == typeof(string) && Attribute.IsDefined(f, typeof(GroupAttribute)));
|
|
|
|
groups.AddRange(fields);
|
|
|
|
foreach (var nested in type.GetNestedTypes())
|
|
CollectFields(nested);
|
|
}
|
|
}
|
|
|
|
public class MasterGroupData
|
|
{
|
|
public required string Name { get; set; } = null!;
|
|
public required AuthorityType Type { get; set; }
|
|
public string? OldName { get; set; }
|
|
public string? Description { get; set; }
|
|
public string? Path { get; set; }
|
|
public IReadOnlyList<string> Roles { get; set; } = [];
|
|
}
|
|
} |