From 035d2e3858dd55580c294031573c3be9e1999449 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 3 Oct 2023 02:20:13 +1100 Subject: AclDialog --- Acl.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'Acl.cs') diff --git a/Acl.cs b/Acl.cs index 7bffc50..a16d08a 100644 --- a/Acl.cs +++ b/Acl.cs @@ -12,6 +12,24 @@ public class Acl { public int AclId { get; set; } public HBObject Subject { get; set; } public List Rules { get; set; } + + public static Type GetAclType(HBObject obj) { + var attrib = Attribute.GetCustomAttribute(obj.GetType(), typeof(AclTypeAttribute), true); + + return (attrib as AclTypeAttribute)?.Type ?? typeof(ObjectPermissions); + } + + public static IEnumerable> GetPermissionDescriptions(HBObject obj) { + var aclType = GetAclType(obj); + + foreach(var val in Enum.GetValues(aclType)) { + var attrib = (AclPermissionAttribute?) Attribute.GetCustomAttribute( + aclType.GetMember(val.ToString()!).First(), + typeof(AclPermissionAttribute)); + + yield return new(attrib?.Name ?? val.ToString()!, (ulong) val); + } + } } public class Acl : Acl where T : Enum { @@ -40,3 +58,22 @@ public class AclRule : AclRule where T : Enum { set => base.Permissions = (ulong) (object) value; } } + +public class AclTypeAttribute : Attribute { + public Type Type { get; private init; } + + public AclTypeAttribute(Type aclType) { + if(!aclType.IsEnum) + throw new ArgumentException(); + Type = aclType; + } +} + +public class AclPermissionAttribute : Attribute { + public string? Name { get; set; } + + public AclPermissionAttribute() {} + + public AclPermissionAttribute(string name) => + Name = name; +} -- cgit v1.3