using System; using System.Linq; namespace EnVisage.Code { /// /// Represents a permission display properties /// public class PermissionItemAttribute : DisplayValueAttribute { public Areas ParentItem; public short Level = 1; public bool DisplayBold = false; public PermissionItemAttribute(string displayValue): base(displayValue) { } } public static class PermissionItemExtensions { private static PermissionItemAttribute getAttributeItem(Areas value) { PermissionItemAttribute result = null; if (value != null) { var members = value.GetType().GetMember(value.ToString()); var attrItem = members.Length > 0 ? members.First().GetCustomAttributes(typeof(PermissionItemAttribute), true).Cast().FirstOrDefault() : null; result = attrItem; } return result; } /// /// Returns TRUE, if item must be displayed with bold font /// public static bool DisplayAsBold(this Areas value) { PermissionItemAttribute attrItem = getAttributeItem(value); bool result = (attrItem != null) && attrItem.DisplayBold; return result; } /// /// Returns parent permission or NULL /// public static Areas? GetParentItem(this Areas value) { PermissionItemAttribute attrItem = getAttributeItem(value); Areas? result = ((attrItem != null) && (attrItem.Level > 1)) ? attrItem.ParentItem : (Areas?)null; return result; } /// /// Returns TRUE, if item has parent permission /// public static bool HasParentItem(this Areas value) { PermissionItemAttribute attrItem = getAttributeItem(value); bool result = (attrItem != null) && (attrItem.Level > 1); return result; } /// /// Returns item indent level in the permission Tree. Root items has level 1 /// public static int GetLevelInTree(this Areas value) { PermissionItemAttribute attrItem = getAttributeItem(value); int result = ((attrItem != null) && attrItem.Level >= 1) ? attrItem.Level : 1; return result; } } }