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 GetAllGroups() { var groups = new List(); CollectFields(typeof(Groups)); return groups .Select(x => new MasterGroupData { Name = (string)x.GetValue(null)!, Type = x.GetCustomAttribute()!.Type, OldName = x.GetCustomAttribute()!.OldKey, Path = x.GetCustomAttribute()!.Path, Description = x.GetCustomAttribute()?.Description ?? null, Roles = GetRoles(x) }).ToList(); IReadOnlyList GetRoles(FieldInfo field) { var hasMasterRolesAttribute = field.GetCustomAttribute(); 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 Roles { get; set; } = []; } }