using System.Windows; using System.Windows.Controls; using Microsoft.Extensions.DependencyInjection; using OrpaonVision.ConfigApp.Infrastructure.Services; namespace OrpaonVision.ConfigApp.Infrastructure.Controls; /// /// 权限控制附加属性,用于控制UI元素的可见性和启用状态。 /// public static class PermissionControl { /// /// 权限编码附加属性。 /// public static readonly DependencyProperty PermissionCodeProperty = DependencyProperty.RegisterAttached( "PermissionCode", typeof(string), typeof(PermissionControl), new PropertyMetadata(null, OnPermissionCodeChanged)); /// /// 控制类型附加属性(Visible/Enabled)。 /// public static readonly DependencyProperty ControlTypeProperty = DependencyProperty.RegisterAttached( "ControlType", typeof(PermissionControlType), typeof(PermissionControl), new PropertyMetadata(PermissionControlType.Visible, OnControlTypeChanged)); /// /// 当前用户ID附加属性。 /// public static readonly DependencyProperty CurrentUserIdProperty = DependencyProperty.RegisterAttached( "CurrentUserId", typeof(Guid), typeof(PermissionControl), new PropertyMetadata(Guid.Empty, OnCurrentUserIdChanged)); /// /// 获取权限编码。 /// public static string GetPermissionCode(DependencyObject obj) { return (string)obj.GetValue(PermissionCodeProperty); } /// /// 设置权限编码。 /// public static void SetPermissionCode(DependencyObject obj, string value) { obj.SetValue(PermissionCodeProperty, value); } /// /// 获取控制类型。 /// public static PermissionControlType GetControlType(DependencyObject obj) { return (PermissionControlType)obj.GetValue(ControlTypeProperty); } /// /// 设置控制类型。 /// public static void SetControlType(DependencyObject obj, PermissionControlType value) { obj.SetValue(ControlTypeProperty, value); } /// /// 获取当前用户ID。 /// public static Guid GetCurrentUserId(DependencyObject obj) { return (Guid)obj.GetValue(CurrentUserIdProperty); } /// /// 设置当前用户ID。 /// public static void SetCurrentUserId(DependencyObject obj, Guid value) { obj.SetValue(CurrentUserIdProperty, value); } private static void OnPermissionCodeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UpdateControlState(d); } private static void OnControlTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UpdateControlState(d); } private static void OnCurrentUserIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UpdateControlState(d); } private static void UpdateControlState(DependencyObject d) { if (d is not FrameworkElement element) return; var permissionCode = GetPermissionCode(element); var controlType = GetControlType(element); var currentUserId = GetCurrentUserId(element); // 如果没有设置权限编码或用户ID,默认拒绝访问(安全优先原则) if (string.IsNullOrEmpty(permissionCode) || currentUserId == Guid.Empty) { SetElementState(element, controlType, false); return; } // 检查权限 var hasPermission = CheckUserPermission(currentUserId, permissionCode); SetElementState(element, controlType, hasPermission); } private static void SetElementState(FrameworkElement element, PermissionControlType controlType, bool isAllowed) { switch (controlType) { case PermissionControlType.Visible: element.Visibility = isAllowed ? Visibility.Visible : Visibility.Collapsed; break; case PermissionControlType.Enabled: if (element is Control control) { control.IsEnabled = isAllowed; } break; } } private static bool CheckUserPermission(Guid userId, string permissionCode) { try { // 从应用程序服务容器获取权限服务 var serviceProvider = ((App)Application.Current).ServiceProvider; var authorizationService = serviceProvider.GetService(); if (authorizationService == null) { // 如果权限服务不可用,默认拒绝访问(安全优先原则) return false; } var result = authorizationService.CheckUserHasPermission(userId, permissionCode); return result.Succeeded && result.Data; } catch { // 异常情况下默认拒绝访问(安全优先原则) return false; } } } /// /// 权限控制类型。 /// public enum PermissionControlType { /// /// 控制可见性。 /// Visible, /// /// 控制启用状态。 /// Enabled }