using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
namespace LB_SmartVisionCommon
{
///
/// 动态权限对象包装器
///
public class DynamicPermissionObject : ICustomTypeDescriptor
{
private readonly object _target;
private readonly object _currentRole;
private readonly Type _roleType;
private readonly object[] _roles;
///
/// 动态权限对象,可以接受多个角色
///
/// 目标
/// 角色
/// 异常信息
public DynamicPermissionObject(object target, params object[] roles)
{
_target = target;
_roles = roles;
// 验证所有角色都是枚举类型
foreach (var role in _roles)
{
if (role != null && !role.GetType().IsEnum)
throw new ArgumentException("所有角色必须是枚举值", nameof(roles));
}
}
///
/// 在属性过滤时检查所有角色
///
/// 权限属性
/// 高是true,低是false
private bool CheckPermissions(PermissionAttribute attr)
{
foreach (var role in _roles)
{
if (role != null && role.GetType() == attr.RoleType)
{
int roleValue = (int)Convert.ChangeType(role, typeof(int));
int requiredValue = (int)Convert.ChangeType(attr.MinimumRole, typeof(int));
if (roleValue >= requiredValue)
return true;
}
}
return false;
}
///
/// 动态权限对象,单个角色
///
/// 目标
/// 当前角色
/// 异常信息
public DynamicPermissionObject(object target, object currentRole)
{
_target = target;
_currentRole = currentRole;
if (currentRole != null)
_roleType = currentRole.GetType();
if (!_roleType.IsEnum)
throw new ArgumentException("currentRole 必须是枚举值", nameof(currentRole));
}
///
/// 获取属性描述符集合
///
/// 返回属性描述符集合
public PropertyDescriptorCollection GetProperties()
{
return GetProperties(null);
}
///
/// 获取属性描述符集合
///
/// 自定义属性集合
/// 返回属性描述符集合
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var originalProperties = TypeDescriptor.GetProperties(_target, attributes, true);
var filteredProperties = new List();
foreach (PropertyDescriptor pd in originalProperties)
{
var attrs = pd.Attributes.OfType().ToArray();
bool hasPermission = true;
bool isReadOnly = false;
// 检查所有权限特性
foreach (var attr in attrs)
{
// 只处理与当前角色类型匹配的特性
if (attr.RoleType == _roleType)
{
if (!HasPermission(attr.MinimumRole))
{
hasPermission = false;
break;
}
else
{
// 有权限但可能只读
isReadOnly = !HasPermission(attr.MinimumRole);
}
}
}
if (hasPermission)
{
filteredProperties.Add(new PermissionPropertyDescriptor(pd, isReadOnly));
}
}
return new PropertyDescriptorCollection(filteredProperties.ToArray());
}
///
/// 权限检测
///
/// 检测角色
/// 是否有权限
private bool HasPermission(object requiredRole)
{
if (_currentRole == null)
{
return false;
}
// 将枚举值转换为整数进行比较
int currentRoleValue = (int)Convert.ChangeType(_currentRole, typeof(int));
int requiredRoleValue = (int)Convert.ChangeType(requiredRole, typeof(int));
return currentRoleValue >= requiredRoleValue;
}
// ICustomTypeDescriptor 的其他方法实现...
///
/// 获取属性
///
/// 返回属性集合
public AttributeCollection GetAttributes() => TypeDescriptor.GetAttributes(_target);
///
/// 获取类名称
///
/// 返回类名称
public string GetClassName() => TypeDescriptor.GetClassName(_target);
///
/// 获取组件名称
///
/// 返回获取组件名称
public string GetComponentName() => TypeDescriptor.GetComponentName(_target);
///
/// 获取转换
///
/// 返回对应类型的转换
public TypeConverter GetConverter() => TypeDescriptor.GetConverter(_target);
///
/// 获取默认事件
///
/// 返回默认事件
public EventDescriptor GetDefaultEvent() => TypeDescriptor.GetDefaultEvent(_target);
///
/// 获取默认属性
///
/// 返回默认属性
public PropertyDescriptor GetDefaultProperty() => TypeDescriptor.GetDefaultProperty(_target);
///
/// 获取指定组件的具有指定基类型的编辑器
///
/// 表示要查找的编辑器的基类型的 Type。
/// 可转换为指定编辑器类型的编辑器的一个实例,如果找不到请求类型的编辑器,则为 null。
public object GetEditor(Type editorBaseType) => TypeDescriptor.GetEditor(_target, editorBaseType);
///
/// 返回组件或类型的事件的集合。
///
/// 具有此组件的事件的 EventDescriptorCollection。
public EventDescriptorCollection GetEvents() => TypeDescriptor.GetEvents(_target);
///
/// 返回组件或类型的事件的集合。
///
/// 可以用作筛选器的类型 Attribute 的数组。
/// 具有匹配此组件指定属性的事件的 EventDescriptorCollection。
public EventDescriptorCollection GetEvents(Attribute[] attributes) => TypeDescriptor.GetEvents(_target, attributes);
///
/// 返回一个对象,该对象包含指定的属性描述符所描述的属性。
///
/// 要检索其所属对象的属性描述符。
/// 一个 Object,拥有该类型说明符指定的给定属性。 默认值为 null。
public object GetPropertyOwner(PropertyDescriptor pd) => _target;
}
}