feat: 添加事件总线

This commit is contained in:
橙子
2023-02-19 20:57:31 +08:00
parent 1f33204697
commit dffdaa8d68
12 changed files with 126 additions and 12 deletions

View File

@@ -85,6 +85,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.RBAC.Web", "src\project\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.ThumbnailSharp", "src\module\Yi.Framework.ThumbnailSharp\Yi.Framework.ThumbnailSharp.csproj", "{60E54034-792C-4A90-BCDF-4D5FFB45089E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.EventBus", "src\module\Yi.Framework.EventBus\Yi.Framework.EventBus.csproj", "{FC559052-36EC-4379-B447-298FAD6045F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -231,6 +233,10 @@ Global
{60E54034-792C-4A90-BCDF-4D5FFB45089E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60E54034-792C-4A90-BCDF-4D5FFB45089E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60E54034-792C-4A90-BCDF-4D5FFB45089E}.Release|Any CPU.Build.0 = Release|Any CPU
{FC559052-36EC-4379-B447-298FAD6045F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC559052-36EC-4379-B447-298FAD6045F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC559052-36EC-4379-B447-298FAD6045F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC559052-36EC-4379-B447-298FAD6045F4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -274,6 +280,7 @@ Global
{2074EA00-59A4-49CE-97A6-735F4B77443B} = {07C9E949-DB5E-4315-A497-FF73746667D8}
{0C031C7D-6F80-4559-977C-AC001036EC44} = {07C9E949-DB5E-4315-A497-FF73746667D8}
{60E54034-792C-4A90-BCDF-4D5FFB45089E} = {EEF5F221-0E32-4A3D-B647-B4B5E7305806}
{FC559052-36EC-4379-B447-298FAD6045F4} = {EEF5F221-0E32-4A3D-B647-B4B5E7305806}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6C1A3808-0F4F-43FB-A9FE-5F27A3BB2ECF}

View File

@@ -9,11 +9,17 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
{
public class DefaultPermissionHandler : IPermissionHandler
{
public bool IsPass(string permission, ICurrentUser currentUser)
private ICurrentUser _currentUser { get; set; }
public DefaultPermissionHandler(ICurrentUser currentUser)
{
if (currentUser.Permission is not null)
_currentUser = currentUser;
}
public bool IsPass(string permission)
{
if (_currentUser.Permission is not null)
{
return currentUser.Permission.Contains(permission);
return _currentUser.Permission.Contains(permission);
}

View File

@@ -9,6 +9,6 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
{
public interface IPermissionHandler
{
bool IsPass(string permission,ICurrentUser currentUser);
bool IsPass(string permission );
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
@@ -11,7 +12,9 @@ using Yi.Framework.Core.Model;
namespace Yi.Framework.Auth.JwtBearer.Authorization
{
[AttributeUsage(AttributeTargets.Method)]
public class PermissionAttribute : ActionFilterAttribute
{
private string Permission { get; set; }
@@ -31,11 +34,7 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
var permissionHandler = ServiceLocatorModel.Instance.GetRequiredService<IPermissionHandler>();
var currentUser = ServiceLocatorModel.Instance.GetRequiredService<ICurrentUser>();
var result = permissionHandler.IsPass(Permission, currentUser);
var result = permissionHandler.IsPass(Permission);
if (!result)
{

View File

@@ -10,6 +10,15 @@ namespace Yi.Framework.Core.Helper
public static class AssemblyHelper
{
/// <summary>
/// 此处统一获取程序集,排除微软内部相关
/// </summary>
/// <returns></returns>
public static Assembly[] GetAllLoadAssembly()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
public static List<Assembly> GetReferanceAssemblies(this AppDomain domain)
{
var list = new List<Assembly>();

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cike.EventBus" Version="1.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using StartupModules;
using Yi.Framework.Core.Helper;
namespace Yi.Framework.EventBus
{
public class YiFrameworkEventBusModule : IStartupModule
{
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
{
}
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
{
services.AddCikeEventBus(opt =>
{
opt.AddHandlerForAsemmbly(AssemblyHelper.GetAllLoadAssembly());
});
}
}
}

View File

@@ -1,4 +1,5 @@
using Hei.Captcha;
using Cike.EventBus.DistributedEvent;
using Hei.Captcha;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
@@ -11,6 +12,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Auth.JwtBearer.Authentication;
using Yi.Framework.Auth.JwtBearer.Authorization;
using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Exceptions;
@@ -25,6 +27,7 @@ using Yi.RBAC.Domain.Identity.Entities;
using Yi.RBAC.Domain.Identity.Repositories;
using Yi.RBAC.Domain.Shared.Identity.ConstClasses;
using Yi.RBAC.Domain.Shared.Identity.Dtos;
using Yi.RBAC.Domain.Shared.Identity.Etos;
namespace Yi.RBAC.Application.Identity
{
@@ -45,6 +48,9 @@ namespace Yi.RBAC.Application.Identity
[Autowired]
private SecurityCodeHelper _securityCode { get; set; }
[Autowired]
private IDistributedEventBus _distributedEventBus { get; set; }
/// <summary>
/// 登录
/// </summary>
@@ -63,6 +69,16 @@ namespace Yi.RBAC.Application.Identity
{
throw new UserFriendlyException(UserConst.);
}
//这里抛出一个登录的事件
//不阻碍执行,无需等待
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
_distributedEventBus.PublishAsync(new LoginEventArgs
{
UserId = userInfo.User.Id,
UserName = user.UserName
});
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
//创建token
var token = _jwtTokenManager.CreateToken(_accountManager.UserInfoToClaim(userInfo));

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Domain.Shared.Identity.Etos
{
public class LoginEventArgs
{
public long UserId { get; set; }
public string UserName { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
using Cike.EventBus.EventHandlerAbstracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.RBAC.Domain.Shared.Identity.Etos;
namespace Yi.RBAC.Domain.Identity.Event
{
public class LoginEventHandler : IDistributedEventHandler<LoginEventArgs>
{
public Task HandlerAsync(LoginEventArgs eventData)
{
Console.WriteLine(eventData.UserName+"登录系统");
return Task.CompletedTask;
}
}
}

View File

@@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.EventBus\Yi.Framework.EventBus.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.ThumbnailSharp\Yi.Framework.ThumbnailSharp.csproj" />
<ProjectReference Include="..\Yi.RBAC.Domain.Shared\Yi.RBAC.Domain.Shared.csproj" />
</ItemGroup>

View File

@@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Data;
using Yi.Framework.EventBus;
using Yi.Framework.ThumbnailSharp;
using Yi.RBAC.Domain.Shared;
@@ -17,7 +18,8 @@ namespace Yi.RBAC.Domain
[DependsOn(
typeof(YiRBACDomainSharedModule),
typeof(YiFrameworkDataModule),
typeof(YiFrameworkThumbnailSharpModule)
typeof(YiFrameworkThumbnailSharpModule),
typeof(YiFrameworkEventBusModule)
)]
public class YiRBACDomainModule : IStartupModule
{