feat:furion rbac搭建

This commit is contained in:
橙子
2023-04-13 21:12:06 +08:00
parent 18696ec542
commit b9dad93c9d
194 changed files with 9557 additions and 75 deletions

View File

@@ -0,0 +1,93 @@
using IPTools.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.AspNetCore;
using Yi.Framework.Infrastructure.CurrentUsers;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Module.OperLogManager
{
public class GlobalOperLogAttribute : ActionFilterAttribute
{
private ILogger<GlobalOperLogAttribute> _logger;
private IRepository<OperationLogEntity> _repository;
private ICurrentUser _currentUser;
//注入一个日志服务
public GlobalOperLogAttribute(ILogger<GlobalOperLogAttribute> logger, IRepository<OperationLogEntity> repository, ICurrentUser currentUser)
{
_logger = logger;
_repository = repository;
_currentUser = currentUser;
}
public override async 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.Id = SnowflakeHelper.NextId;
logEntity.OperIp = ip;
//logEntity.OperLocation = location;
logEntity.OperType = operLogAttribute.OperType;
logEntity.Title = operLogAttribute.Title;
logEntity.RequestMethod = context.HttpContext.Request.Method;
logEntity.Method = context.HttpContext.Request.Path.Value;
logEntity.OperLocation = location;
logEntity.OperUser = _currentUser.UserName;
if (operLogAttribute.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 (operLogAttribute.IsSaveRequestData)
{
//logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod);
}
await _repository.InsertAsync(logEntity);
}
}
}

View File

@@ -0,0 +1,12 @@
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Module.OperLogManager
{
/// <summary>
/// OperationLog服务抽象
/// </summary>
public interface IOperationLogService : ICrudAppService<OperationLogGetListOutputDto, long, OperationLogGetListInputVo>
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Module.OperLogManager
{
public enum OperEnum
{
Insert = 1,
Update = 2,
Delete = 3,
Auth = 4,
Export = 5,
Import = 6,
ForcedOut = 7,
GenerateCode = 8,
ClearData = 9
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Module.OperLogManager
{
[AttributeUsage(AttributeTargets.Method)]
public class OperLogAttribute : Attribute
{
/// <summary>
/// 操作类型
/// </summary>
public OperEnum OperType { get; set; }
/// <summary>
/// 日志标题(模块)
/// </summary>
public string Title { get; set; }
/// <summary>
/// 是否保存请求数据
/// </summary>
public bool IsSaveRequestData { get; set; } = true;
/// <summary>
/// 是否保存返回数据
/// </summary>
public bool IsSaveResponseData { get; set; } = true;
public OperLogAttribute(string title, OperEnum operationType)
{
Title = title;
OperType = operationType;
}
}
}

View File

@@ -0,0 +1,65 @@
using SqlSugar;
using Yi.Framework.Infrastructure.Data.Auditing;
using Yi.Framework.Infrastructure.Ddd.Entities;
namespace Yi.Framework.Module.OperLogManager
{
/// <summary>
/// 操作日志表
///</summary>
[SugarTable("OperationLog")]
public class OperationLogEntity : IEntity<long>, ICreationAuditedObject
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public long Id { get; set; }
/// <summary>
/// 操作模块
///</summary>
[SugarColumn(ColumnName = "Title")]
public string? Title { get; set; }
/// <summary>
/// 操作类型
///</summary>
[SugarColumn(ColumnName = "OperType")]
public OperEnum OperType { get; set; }
/// <summary>
/// 请求方法
///</summary>
[SugarColumn(ColumnName = "RequestMethod")]
public string? RequestMethod { get; set; }
/// <summary>
/// 操作人员
///</summary>
[SugarColumn(ColumnName = "OperUser")]
public string? OperUser { get; set; }
/// <summary>
/// 操作Ip
///</summary>
[SugarColumn(ColumnName = "OperIp")]
public string? OperIp { get; set; }
/// <summary>
/// 操作地点
///</summary>
[SugarColumn(ColumnName = "OperLocation")]
public string? OperLocation { get; set; }
/// <summary>
/// 操作方法
///</summary>
[SugarColumn(ColumnName = "Method")]
public string? Method { get; set; }
/// <summary>
/// 请求参数
///</summary>
[SugarColumn(ColumnName = "RequestParam")]
public string? RequestParam { get; set; }
/// <summary>
/// 请求结果
///</summary>
[SugarColumn(ColumnName = "RequestResult", Length = 9999)]
public string? RequestResult { get; set; }
public DateTime CreationTime { get; set; }
public long? CreatorId { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
using Yi.Framework.Infrastructure.Ddd.Dtos;
namespace Yi.Framework.Module.OperLogManager
{
public class OperationLogGetListInputVo : PagedAllResultRequestDto
{
public OperEnum? OperType { get; set; }
public string? OperUser { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Module.OperLogManager
{
public class OperationLogGetListOutputDto : IEntityDto<long>
{
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; }
}
}

View File

@@ -0,0 +1,36 @@
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Dtos;
using Yi.Framework.Infrastructure.Ddd.Services;
namespace Yi.Framework.Module.OperLogManager
{
/// <summary>
/// OperationLog服务实现
/// </summary>
//[AppService]
public class OperationLogService : CrudAppService<OperationLogEntity, OperationLogGetListOutputDto, long, OperationLogGetListInputVo>,
IOperationLogService, IDynamicApiController, ITransient
{
public override async Task<PagedResultDto<OperationLogGetListOutputDto>> GetListAsync(OperationLogGetListInputVo input)
{
var entity = await MapToEntityAsync(input);
RefAsync<int> total = 0;
var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.OperUser), x => x.OperUser.Contains(input.OperUser!))
.WhereIF(input.OperType is not null, x => x.OperType == input.OperType)
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
.ToPageListAsync(input.PageNum, input.PageSize, total);
return new PagedResultDto<OperationLogGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
[NonAction]
public override Task<OperationLogGetListOutputDto> UpdateAsync(long id, OperationLogGetListOutputDto input)
{
return base.UpdateAsync(id, input);
}
}
}