From 6615229003fd321915810514197cc23aa8dca44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Tue, 21 Feb 2023 19:37:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=90=AD=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/Yi.Framework.Template/Program.cs | 2 +- .../ApplicationContractsSwaggerDoc.xml | 5 + .../OperationLogGetListInputVo.cs | 24 +++++ .../OperationLogGetListOutputDto.cs | 25 +++++ .../Logs/IOperationLogService.cs | 18 ++++ .../ApplicationSwaggerDoc.xml | 5 + .../Logs/LoginLogService.cs | 1 + .../Logs/OperationLogService.cs | 23 +++++ .../Yi.RBAC.Domain.Shared/Logs/OperEnum.cs | 21 ++++ .../Logs/OperLogAttribute.cs | 38 ++++++++ .../rbac/Yi.RBAC.Domain/DomainSwaggerDoc.xml | 50 ++++++++++ .../Logs/Entities/OperationLogEntity.cs | 70 ++++++++++++++ ...nEventHandler .cs => LoginEventHandler.cs} | 0 .../Logs/GlobalOperLogAttribute.cs | 91 ++++++++++++++++++ .../rbac/Yi.RBAC.Domain/YiRBACDomainModule.cs | 5 + .../rbac/Yi.RBAC.Web/yi-sqlsugar-dev.db | Bin 126976 -> 135168 bytes 16 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListInputVo.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListOutputDto.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/IOperationLogService.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/OperationLogService.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperEnum.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperLogAttribute.cs create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Entities/OperationLogEntity.cs rename Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Event/{LoginEventHandler .cs => LoginEventHandler.cs} (100%) create mode 100644 Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs diff --git a/Yi.Framework.Net6/src/module/Yi.Framework.Template/Program.cs b/Yi.Framework.Net6/src/module/Yi.Framework.Template/Program.cs index 57f4b117..1d6c5a27 100644 --- a/Yi.Framework.Net6/src/module/Yi.Framework.Template/Program.cs +++ b/Yi.Framework.Net6/src/module/Yi.Framework.Template/Program.cs @@ -20,7 +20,7 @@ TemplateFactory templateFactory = new(); //string modelName = "Dictionary"; //string nameSpaces = "Yi.RBAC"; //List entityNames = new() { "_", "_" }; -string modelName = "Setting"; +string modelName = "Logs"; string nameSpaces = "Yi.RBAC"; List entityNames = new() { "_" }; diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/ApplicationContractsSwaggerDoc.xml b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/ApplicationContractsSwaggerDoc.xml index a211a924..e4d97274 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/ApplicationContractsSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/ApplicationContractsSwaggerDoc.xml @@ -94,6 +94,11 @@ 登录信息 + + + OperationLog服务抽象 + + Config输入创建对象 diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListInputVo.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListInputVo.cs new file mode 100644 index 00000000..dce0661b --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListInputVo.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Ddd.Dtos; +using Yi.RBAC.Domain.Shared.Logs; + +namespace Yi.RBAC.Application.Contracts.Logs.Dtos +{ + public class OperationLogGetListInputVo : PagedAllResultRequestDto + { + public string? Title { get; set; } + public OperEnum OperType { get; set; } + public string? RequestMethod { get; set; } + public string? OperUser { get; set; } + public string? OperIp { get; set; } + public string? OperLocation { get; set; } + public string? Method { get; set; } + public string? RequestParam { get; set; } + public string? RequestResult { get; set; } + public DateTime CreationTime { get; set; } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListOutputDto.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListOutputDto.cs new file mode 100644 index 00000000..0f727957 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/Dtos/OperationLog/OperationLogGetListOutputDto.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Ddd.Dtos; +using Yi.RBAC.Domain.Shared.Logs; + +namespace Yi.RBAC.Application.Contracts.Logs.Dtos +{ + public class OperationLogGetListOutputDto : IEntityDto + { + public long Id { get; set; } + public string? Title { get; set; } + public OperEnum OperType { get; set; } + public string? RequestMethod { get; set; } + public string? OperUser { get; set; } + public string? OperIp { get; set; } + public string? OperLocation { get; set; } + public string? Method { get; set; } + public string? RequestParam { get; set; } + public string? RequestResult { get; set; } + public DateTime CreationTime { get; set; } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/IOperationLogService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/IOperationLogService.cs new file mode 100644 index 00000000..b676a4b0 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Logs/IOperationLogService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.RBAC.Application.Contracts.Logs.Dtos; +using Yi.Framework.Ddd.Services.Abstract; + +namespace Yi.RBAC.Application.Contracts.Logs +{ + /// + /// OperationLog服务抽象 + /// + public interface IOperationLogService : ICrudAppService + { + + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/ApplicationSwaggerDoc.xml b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/ApplicationSwaggerDoc.xml index f3dd3fec..75e1f5ac 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/ApplicationSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/ApplicationSwaggerDoc.xml @@ -179,6 +179,11 @@ + + + OperationLog服务实现 + + Config服务实现 diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/LoginLogService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/LoginLogService.cs index 183c206e..3ab6d27f 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/LoginLogService.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/LoginLogService.cs @@ -28,5 +28,6 @@ namespace Yi.RBAC.Application.Logs .ToPageListAsync(input.PageNum, input.PageSize, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } + } } diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/OperationLogService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/OperationLogService.cs new file mode 100644 index 00000000..b267768a --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Logs/OperationLogService.cs @@ -0,0 +1,23 @@ +using Yi.RBAC.Application.Contracts.Logs; +using NET.AutoWebApi.Setting; +using Yi.RBAC.Application.Contracts.Logs.Dtos; +using Yi.RBAC.Domain.Logs.Entities; +using Yi.Framework.Ddd.Services; +using Yi.Framework.Model.RABC.Entitys; +using Yi.Framework.Ddd.Dtos; + +namespace Yi.RBAC.Application.Logs +{ + /// + /// OperationLog服务实现 + /// + [AppService] + public class OperationLogService : CrudAppService, + IOperationLogService, IAutoApiService + { + public override Task> GetListAsync(OperationLogGetListInputVo input) + { + return base.GetListAsync(input); + } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperEnum.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperEnum.cs new file mode 100644 index 00000000..188ad1e2 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperEnum.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.RBAC.Domain.Shared.Logs +{ + public enum OperEnum + { + Insert = 1, + Update = 2, + Delete = 3, + Auth = 4, + Export = 5, + Import = 6, + ForcedOut = 7, + GenerateCode = 8, + ClearData = 9 + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperLogAttribute.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperLogAttribute.cs new file mode 100644 index 00000000..49625d7a --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain.Shared/Logs/OperLogAttribute.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.RBAC.Domain.Shared.Logs +{ + [AttributeUsage(AttributeTargets.Method)] + public class OperLogAttribute : System.Attribute + { + /// + /// 操作类型 + /// + public OperEnum OperType { get; set; } + + /// + /// 日志标题(模块) + /// + public string Title { get; set; } + + /// + /// 是否保存请求数据 + /// + public bool IsSaveRequestData { get; set; } = true; + + /// + /// 是否保存返回数据 + /// + public bool IsSaveResponseData { get; set; } = true; + + public OperLogAttribute(string title, OperEnum operationType) + { + this.Title = title; + this.OperType = operationType; + } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DomainSwaggerDoc.xml b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DomainSwaggerDoc.xml index d7fd7578..e60d99a5 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DomainSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DomainSwaggerDoc.xml @@ -790,5 +790,55 @@ 描述 + + + 操作日志表 + + + + + 操作模块 + + + + + 操作类型 + + + + + 请求方法 + + + + + 操作人员 + + + + + 操作Ip + + + + + 操作地点 + + + + + 操作方法 + + + + + 请求参数 + + + + + 请求结果 + + diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Entities/OperationLogEntity.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Entities/OperationLogEntity.cs new file mode 100644 index 00000000..ba513734 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Entities/OperationLogEntity.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; +using SqlSugar; +using Yi.Framework.Data.Auditing; +using Yi.Framework.Ddd.Entities; +using Yi.RBAC.Domain.Shared.Logs; + +namespace Yi.Framework.Model.RABC.Entitys +{ + /// + /// 操作日志表 + /// + [SugarTable("OperationLog")] + public class OperationLogEntity : IEntity, ICreationAuditedObject + { + [SugarColumn(ColumnName = "Id", IsPrimaryKey = true)] + public long Id { get; set; } + /// + /// 操作模块 + /// + [SugarColumn(ColumnName = "Title")] + public string? Title { get; set; } + /// + /// 操作类型 + /// + [SugarColumn(ColumnName = "OperType")] + public OperEnum OperType { get; set; } + /// + /// 请求方法 + /// + [SugarColumn(ColumnName = "RequestMethod")] + public string? RequestMethod { get; set; } + /// + /// 操作人员 + /// + [SugarColumn(ColumnName = "OperUser")] + public string? OperUser { get; set; } + /// + /// 操作Ip + /// + [SugarColumn(ColumnName = "OperIp")] + public string? OperIp { get; set; } + /// + /// 操作地点 + /// + [SugarColumn(ColumnName = "OperLocation")] + public string? OperLocation { get; set; } + /// + /// 操作方法 + /// + [SugarColumn(ColumnName = "Method")] + public string? Method { get; set; } + /// + /// 请求参数 + /// + [SugarColumn(ColumnName = "RequestParam")] + public string? RequestParam { get; set; } + /// + /// 请求结果 + /// + [SugarColumn(ColumnName = "RequestResult")] + public string? RequestResult { get; set; } + + public DateTime CreationTime { get; set; } + + public long? CreatorId { get; set; } + } +} diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Event/LoginEventHandler .cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Event/LoginEventHandler.cs similarity index 100% rename from Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Event/LoginEventHandler .cs rename to Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/Event/LoginEventHandler.cs diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs new file mode 100644 index 00000000..25b5c6c1 --- /dev/null +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs @@ -0,0 +1,91 @@ +using IPTools.Core; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.AspNetCore.Extensions; +using Yi.RBAC.Domain.Shared.Logs; + +namespace Yi.RBAC.Domain.Logs +{ + public class GlobalOperLogAttribute : ActionFilterAttribute + { + private ILogger _logger; + //注入一个日志服务 + public GlobalOperLogAttribute(ILogger logger) + { + _logger = logger; + } + + public override void OnResultExecuted(ResultExecutedContext context) + { + //判断标签是在方法上 + if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return; + + //查找标签,获取标签对象 + OperLogAttribute? operLogAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) + .FirstOrDefault(a => a.GetType().Equals(typeof(OperLogAttribute))) as OperLogAttribute; + //空对象直接返回 + if (operLogAttribute is null) return; + + ////获取控制器名 + //string controller = context.RouteData.Values["Controller"].ToString(); + + ////获取方法名 + //string action = context.RouteData.Values["Action"].ToString(); + + //获取Ip + string ip = context.HttpContext.GetClientIp(); + + //根据ip获取地址 + + var ipTool = IpTool.Search(ip); + string location = ipTool.Province + " " + ipTool.City; + + //日志服务插入一条操作记录即可 + + //var logEntity = new OperationLogEntity(); + + //logEntity.OperIp = ip; + ////logEntity.OperLocation = location; + //logEntity.OperType = logAttribute.OperType.GetHashCode(); + //logEntity.Title = logAttribute.Title; + //logEntity.RequestMethod = context.HttpContext.Request.Method; + //logEntity.Method = context.HttpContext.Request.Path.Value; + //logEntity.IsDeleted = false; + //logEntity.OperUser= context.HttpContext.GetUserNameInfo(); + //logEntity.OperLocation = location; + //if (logAttribute.IsSaveResponseData) + //{ + // if (context.Result is ContentResult result && result.ContentType == "application/json") + // { + // logEntity.RequestResult = result.Content?.Replace("\r\n", "").Trim(); + // } + // if (context.Result is JsonResult result2) + // { + // logEntity.RequestResult = result2.Value?.ToString(); + // } + + // if (context.Result is ObjectResult result3) + // { + // logEntity.RequestResult = JsonHelper.ObjToStr(result3.Value); + // } + + //} + + //if (logAttribute.IsSaveRequestData) + //{ + // logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod); + //} + + //_operationLogService._repository.InsertReturnSnowflakeId(logEntity); + + + } + } +} 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 d88ef2d5..5a2e9966 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 @@ -11,6 +11,7 @@ using Yi.Framework.Core.Attributes; using Yi.Framework.Data; using Yi.Framework.EventBus; using Yi.Framework.ThumbnailSharp; +using Yi.RBAC.Domain.Logs; using Yi.RBAC.Domain.Shared; namespace Yi.RBAC.Domain @@ -30,6 +31,10 @@ namespace Yi.RBAC.Domain public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) { services.AddHeiCaptcha(); + services.AddControllers(options => { + options.Filters.Add(); + }); + services.AddSingleton(); } } } diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/yi-sqlsugar-dev.db b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/yi-sqlsugar-dev.db index 54b2b1599c7a9b68b4f927acc2457a8a7028033e..2ebb7ed8ccf616efd81a43a166437207064ad417 100644 GIT binary patch delta 402 zcmZp8z}~QcV}i6`Is*fPA`ruX@I)PB#`MO7tqF_^>kWaTq71JY_^=jL z;K}E{&b^GAkLxbS4vuEFt86n^4zf&PHfCmGc+GTja-hOayXI(Fc5ziz#et3rsQlaH%{5=?4(eIuhTYe;5EPU>`rCPpb%p!Sf; z$#U#cyg{jjrK!awzNsY{`6*yY7NBJ4blWCIS!N(>asab53&@=5KN=Yo*&ynsH8D!^ zKy?Nr7A59_1Ykx5r52awlz>DgOS9N=Dkx~0%dv@jt4`j?BOwcO5y;{3hR8fceVOTn zJ&ZEbf44IVZ})9vOk`x!mSPjPmjr3#ch1jC%LHnH(6ZatH8EaiobKJhXuDm%lW`s6 Z^a&k|rrTRv7*(0J-|b>lPF=J?0RT(~e)<3a delta 234 zcmZozz|ru4eS)-L8Uq7^JP^Zx&_o?$#vh>yF$gpIGVovLcjp)5P2{!U zS-_LeeVuz5Hy_tsmR61locbKzOzSwr7=79AZVptCXWVYw%$Ufy{a_2@b;j*GIv7=% zxH$M2jm0%J8C%P@-*02oWMtBpnO@k#C^KE5nNgUJ!