版本260406

This commit is contained in:
2026-04-06 22:04:05 +08:00
parent 7dc5e73af7
commit 0b150470be
216 changed files with 98993 additions and 33 deletions

View File

@@ -0,0 +1,182 @@
using System.Windows;
using System.Windows.Controls;
using Microsoft.Extensions.DependencyInjection;
using OrpaonVision.ConfigApp.Infrastructure.Services;
namespace OrpaonVision.ConfigApp.Infrastructure.Controls;
/// <summary>
/// 权限控制附加属性用于控制UI元素的可见性和启用状态。
/// </summary>
public static class PermissionControl
{
/// <summary>
/// 权限编码附加属性。
/// </summary>
public static readonly DependencyProperty PermissionCodeProperty =
DependencyProperty.RegisterAttached(
"PermissionCode",
typeof(string),
typeof(PermissionControl),
new PropertyMetadata(null, OnPermissionCodeChanged));
/// <summary>
/// 控制类型附加属性Visible/Enabled
/// </summary>
public static readonly DependencyProperty ControlTypeProperty =
DependencyProperty.RegisterAttached(
"ControlType",
typeof(PermissionControlType),
typeof(PermissionControl),
new PropertyMetadata(PermissionControlType.Visible, OnControlTypeChanged));
/// <summary>
/// 当前用户ID附加属性。
/// </summary>
public static readonly DependencyProperty CurrentUserIdProperty =
DependencyProperty.RegisterAttached(
"CurrentUserId",
typeof(Guid),
typeof(PermissionControl),
new PropertyMetadata(Guid.Empty, OnCurrentUserIdChanged));
/// <summary>
/// 获取权限编码。
/// </summary>
public static string GetPermissionCode(DependencyObject obj)
{
return (string)obj.GetValue(PermissionCodeProperty);
}
/// <summary>
/// 设置权限编码。
/// </summary>
public static void SetPermissionCode(DependencyObject obj, string value)
{
obj.SetValue(PermissionCodeProperty, value);
}
/// <summary>
/// 获取控制类型。
/// </summary>
public static PermissionControlType GetControlType(DependencyObject obj)
{
return (PermissionControlType)obj.GetValue(ControlTypeProperty);
}
/// <summary>
/// 设置控制类型。
/// </summary>
public static void SetControlType(DependencyObject obj, PermissionControlType value)
{
obj.SetValue(ControlTypeProperty, value);
}
/// <summary>
/// 获取当前用户ID。
/// </summary>
public static Guid GetCurrentUserId(DependencyObject obj)
{
return (Guid)obj.GetValue(CurrentUserIdProperty);
}
/// <summary>
/// 设置当前用户ID。
/// </summary>
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<IAuthorizationService>();
if (authorizationService == null)
{
// 如果权限服务不可用,默认拒绝访问(安全优先原则)
return false;
}
var result = authorizationService.CheckUserHasPermission(userId, permissionCode);
return result.Succeeded && result.Data;
}
catch
{
// 异常情况下默认拒绝访问(安全优先原则)
return false;
}
}
}
/// <summary>
/// 权限控制类型。
/// </summary>
public enum PermissionControlType
{
/// <summary>
/// 控制可见性。
/// </summary>
Visible,
/// <summary>
/// 控制启用状态。
/// </summary>
Enabled
}