From dffdaa8d6883cfe153f6387a2cd9b8c41ce3302b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 19 Feb 2023 20:57:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=80=BB=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yi.Framework.Net6/Yi.Framework.sln | 7 ++++++ .../Authorization/DefaultPermissionHandler.cs | 12 +++++++--- .../Authorization/IPermissionHandler.cs | 2 +- .../Authorization/PermissionAttribute.cs | 11 ++++----- .../Helper/AssemblyHelper.cs | 9 ++++++++ .../Yi.Framework.EventBus.csproj | 17 ++++++++++++++ .../YiFrameworkEventBusModule.cs | 23 +++++++++++++++++++ .../Identity/AccountService.cs | 18 ++++++++++++++- .../Identity/Etos/LoginEventArgs.cs | 15 ++++++++++++ .../Identity/Event/LoginEventHandler .cs | 19 +++++++++++++++ .../rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj | 1 + .../rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs | 4 +++- 12 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 Yi.Framework.Net6/src/module/Yi.Framework.EventBus/Yi.Framework.EventBus.csproj create mode 100644 Yi.Framework.Net6/src/module/Yi.Framework.EventBus/YiFrameworkEventBusModule.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Identity/Etos/LoginEventArgs.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Identity/Event/LoginEventHandler .cs diff --git a/Yi.Framework.Net6/Yi.Framework.sln b/Yi.Framework.Net6/Yi.Framework.sln index 83bb7f09..6b5befa2 100644 --- a/Yi.Framework.Net6/Yi.Framework.sln +++ b/Yi.Framework.Net6/Yi.Framework.sln @@ -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} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs index c31d6607..4aad26a0 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/DefaultPermissionHandler.cs @@ -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); } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/IPermissionHandler.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/IPermissionHandler.cs index 411248b9..4cc17784 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/IPermissionHandler.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/IPermissionHandler.cs @@ -9,6 +9,6 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization { public interface IPermissionHandler { - bool IsPass(string permission,ICurrentUser currentUser); + bool IsPass(string permission ); } } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs index 88d5b4b4..9f4fe83b 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Auth.JwtBearer/Authorization/PermissionAttribute.cs @@ -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(); - var currentUser = ServiceLocatorModel.Instance.GetRequiredService(); - - - - var result = permissionHandler.IsPass(Permission, currentUser); + var result = permissionHandler.IsPass(Permission); if (!result) { diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs index a1e9b0dc..7f646689 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Helper/AssemblyHelper.cs @@ -10,6 +10,15 @@ namespace Yi.Framework.Core.Helper public static class AssemblyHelper { + /// + /// 此处统一获取程序集,排除微软内部相关 + /// + /// + public static Assembly[] GetAllLoadAssembly() + { + return AppDomain.CurrentDomain.GetAssemblies(); + } + public static List GetReferanceAssemblies(this AppDomain domain) { var list = new List(); diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/Yi.Framework.EventBus.csproj b/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/Yi.Framework.EventBus.csproj new file mode 100644 index 00000000..a6d1a9aa --- /dev/null +++ b/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/Yi.Framework.EventBus.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/YiFrameworkEventBusModule.cs b/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/YiFrameworkEventBusModule.cs new file mode 100644 index 00000000..558716f1 --- /dev/null +++ b/Yi.Framework.Net6/src/module/Yi.Framework.EventBus/YiFrameworkEventBusModule.cs @@ -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()); + }); + } + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs index 913b548e..e0ea1e91 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Identity/AccountService.cs @@ -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; } /// /// 登录 /// @@ -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)); diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Identity/Etos/LoginEventArgs.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Identity/Etos/LoginEventArgs.cs new file mode 100644 index 00000000..925b5eea --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Identity/Etos/LoginEventArgs.cs @@ -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; } + + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Identity/Event/LoginEventHandler .cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Identity/Event/LoginEventHandler .cs new file mode 100644 index 00000000..2cd87ee3 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Identity/Event/LoginEventHandler .cs @@ -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 + { + public Task HandlerAsync(LoginEventArgs eventData) + { + Console.WriteLine(eventData.UserName+"登录系统"); + return Task.CompletedTask; + } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj index b602d472..cfb38688 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Yi.RBAC.Domain.csproj @@ -15,6 +15,7 @@ + diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs index 368ac54f..d88ef2d5 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs @@ -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 {