add - 添加肉夹馍使用AOP切面实现权限过滤

dev
wenjy 3 weeks ago
parent 524c93f430
commit fcfa1f16ca

@ -46,6 +46,8 @@ namespace SlnMesnac.WPF
var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/";
Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}");
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
}
/// <summary>
@ -72,6 +74,14 @@ namespace SlnMesnac.WPF
// ...
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// 处理异常
var info = e.Exception;
MessageBox.Show(e.Exception.Message);
// 防止默认的崩溃行为
e.Handled = true;
}
}
}

@ -0,0 +1,67 @@
using Rougamo;
using Rougamo.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* SlnMesnac.WPF.Attribute
* 34a4538b-bbac-4b48-9e87-1662b859a705
*
* WenJY
*
* 2024-12-30 9:59:19
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.Attribute
{
/// <summary>
/// 权限过滤
/// </summary>
public class RequirePermissionAttribute : MoAttribute
{
private string _permissionName;
public RequirePermissionAttribute(string permissionName)
{
_permissionName = permissionName;
}
public override void OnEntry(MethodContext context)
{
Console.WriteLine($"{_permissionName}方法执行前");
bool hasPermission = CheckPermission(_permissionName);
if (!hasPermission)
{
// 如果用户没有权限,抛出异常或采取其他措施
throw new UnauthorizedAccessException("User does not have the required permission.");
}
base.OnEntry(context);
}
private bool CheckPermission(string permissionName)
{
// 这里实现你的权限检查逻辑
// 例如,从用户的权限集合中检查是否包含所需的权限
// 这只是一个示例,你可以根据实际需要实现
return false; // 假设用户总是有权限
}
}
}

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Rougamo />
</Weavers>

@ -40,6 +40,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Lierda.WPFHelper" Version="1.0.3" />
<PackageReference Include="NVelocity" Version="1.2.0" />
<PackageReference Include="Rougamo.Fody" Version="5.0.0" />
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" />
</ItemGroup>

@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Serilog;
using SlnMesnac.WPF.Attribute;
using SlnMesnac.WPF.Page.Generate;
using System;
using System.Windows;
@ -76,6 +77,7 @@ namespace SlnMesnac.WPF.ViewModel
/// </summary>
/// <param name="obj"></param>
[RelayCommand]
[RequirePermission("FormControl")]
private void FormControl(object obj)
{
try

Loading…
Cancel
Save