80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace EnVisage.Code
|
|
{
|
|
/// <summary>
|
|
/// Represents a permission display properties
|
|
/// </summary>
|
|
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<PermissionItemAttribute>().FirstOrDefault() :
|
|
null;
|
|
|
|
result = attrItem;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns TRUE, if item must be displayed with bold font
|
|
/// </summary>
|
|
public static bool DisplayAsBold(this Areas value)
|
|
{
|
|
PermissionItemAttribute attrItem = getAttributeItem(value);
|
|
bool result = (attrItem != null) && attrItem.DisplayBold;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns parent permission or NULL
|
|
/// </summary>
|
|
public static Areas? GetParentItem(this Areas value)
|
|
{
|
|
PermissionItemAttribute attrItem = getAttributeItem(value);
|
|
Areas? result = ((attrItem != null) && (attrItem.Level > 1)) ? attrItem.ParentItem : (Areas?)null;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns TRUE, if item has parent permission
|
|
/// </summary>
|
|
public static bool HasParentItem(this Areas value)
|
|
{
|
|
PermissionItemAttribute attrItem = getAttributeItem(value);
|
|
bool result = (attrItem != null) && (attrItem.Level > 1);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns item indent level in the permission Tree. Root items has level 1
|
|
/// </summary>
|
|
public static int GetLevelInTree(this Areas value)
|
|
{
|
|
PermissionItemAttribute attrItem = getAttributeItem(value);
|
|
int result = ((attrItem != null) && attrItem.Level >= 1) ? attrItem.Level : 1;
|
|
return result;
|
|
}
|
|
}
|
|
} |