Merge branch 'master' into net6.0

This commit is contained in:
不做码农
2022-04-10 10:55:53 +08:00
24 changed files with 87 additions and 144 deletions

View File

@@ -106,7 +106,7 @@ namespace ZR.Admin.WebApi.Controllers
throw new CustomException("请求参数错误");
}
//从 Dto 映射到 实体
var model = parm.Adapt<SysConfig>().ToCreate();
var model = parm.Adapt<SysConfig>().ToCreate(HttpContext);
return SUCCESS(_SysConfigService.Insert(model, it => new
{

View File

@@ -33,28 +33,28 @@ namespace ZR.Admin.WebApi.Extensions
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
new IocConfig() {
ConfigId = "0",
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
}, new IocConfig() {
ConfigId = "1",
ConnectionString = connStrBus,
DbType = (IocDbType)dbType_bus,
IsAutoCloseConnection = true
}
});
new IocConfig() {
ConfigId = "0",
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
}, new IocConfig() {
ConfigId = "1",
ConnectionString = connStrBus,
DbType = (IocDbType)dbType_bus,
IsAutoCloseConnection = true
}
});
SugarIocServices.ConfigurationSugar(db =>
{
FilterData(0);
//FilterData(1);
#region db0
db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
{
var param = db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
var param = db.GetConnection(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
FilterData(db.GetConnection(0));
logger.Info($"【sql语句】{sql}{param}");
logger.Info($"【sql语句】{sql}{param}\n");
};
db.GetConnection(0).Aop.OnError = (e) =>
@@ -72,7 +72,7 @@ namespace ZR.Admin.WebApi.Extensions
//Db1
db.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
{
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
var param = db.GetConnection(1).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
logger.Info($"【sql语句】{sql}, {param}");
};
@@ -88,7 +88,8 @@ namespace ZR.Admin.WebApi.Extensions
/// <summary>
/// 分页获取count 不会追加sql
/// </summary>
private static void FilterData(ISqlSugarClient sqlSugarClient)
/// <param name="configId">多库id</param>
private static void FilterData(int configId)
{
var u = App.User;
if (u == null) return;
@@ -97,8 +98,8 @@ namespace ZR.Admin.WebApi.Extensions
if (user == null) return;
//管理员不过滤
if (user.RoleIds.Any(f => f.Equals("admin"))) return;
foreach (var role in user.Roles)
var db = DbScoped.SugarScope.GetConnection(configId);
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
{
string dataScope = role.DataScope;
if (DATA_SCOPE_ALL.Equals(dataScope))//所有权限
@@ -107,28 +108,30 @@ namespace ZR.Admin.WebApi.Extensions
}
else if (DATA_SCOPE_CUSTOM.Equals(dataScope))//自定数据权限
{
//有问题
//var roleDepts = db0.Queryable<SysRoleDept>()
//.Where(f => f.RoleId == role.RoleId).Select(f => f.DeptId).ToList();
//var filter1 = new TableFilterItem<SysDept>(it => roleDepts.Contains(it.DeptId));
//DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
//" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
var filter1 = new TableFilterItem<SysUser>(it => SqlFunc.Subqueryable<SysRoleDept>().Where(f => f.DeptId == it.DeptId && f.RoleId == role.RoleId).Any());
db.QueryFilter.Add(filter1);
}
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
{
//有问题添加后的SQL 语句 是 AND deptId = @deptId
var exp = Expressionable.Create<SysDept>();
exp.Or(it => it.DeptId == user.DeptId);
var filter1 = new TableFilterItem<SysDept>(exp.ToExpression());
sqlSugarClient.QueryFilter.Add(filter1);
var filter1 = new TableFilterItem<SysUser>(it => it.DeptId == user.DeptId);
db.QueryFilter.Add(filter1);
}
else if (DATA_SCOPE_DEPT_AND_CHILD.Equals(dataScope))//本部门及以下数据
{
//SQl OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )
var allChildDepts = db.Queryable<SysDept>().ToChildList(it => it.ParentId, user.DeptId);
var filter1 = new TableFilterItem<SysUser>(it => allChildDepts.Select(f => f.DeptId).ToList().Contains(it.DeptId));
db.QueryFilter.Add(filter1);
var filter2 = new TableFilterItem<SysDept>(it => allChildDepts.Select(f => f.DeptId).ToList().Contains(it.DeptId));
db.QueryFilter.Add(filter2);
}
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
{
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId, true);
sqlSugarClient.QueryFilter.Add(filter1);
db.QueryFilter.Add(filter1);
}
}
}

View File

@@ -121,7 +121,7 @@ namespace ZR.Admin.WebApi.Middleware
SysOperLogService.InsertOperlog(sysOperLog);
}
public static Endpoint? GetEndpoint(HttpContext context)
public static Endpoint GetEndpoint(HttpContext context)
{
if (context == null)
{

View File

@@ -1,70 +0,0 @@
using Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZR.Admin.WebApi.Middleware
{
public class RequestIPMiddleware
{
private readonly RequestDelegate _next;
static readonly Logger Logger = LogManager.GetCurrentClassLogger();//声明NLog变量
public RequestIPMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var url = context.Request.Path.ToString().ToLower();
string userip = Tools.GetRealIP();
//string agent = HttpContextExtension.GetUserAgent(context);
string[] urls = new string[] { "/css", "/js", "/images", "/lib", "/home/error", "/api" };
var strRegex = "(.jpg|.png|.gif|.php|.cfg|.ico)$"; //用于验证图片扩展名的正则表达式
var re = new Regex(strRegex);
//阻止.php访问往下请求
if (new Regex("(.php)$").IsMatch(url))
{
await context.Response.WriteAsync("hello");
return;
}
//var ip_info = IpTool.Search(userip);
////bool flag = ((IList)urls).Contains(url);
//if (!re.IsMatch(url) )
//{
Logger.Debug($"IP中间件请求访问IP[{userip}]");
//}
//两种方式传递下去 一是invoke 一个直接next
//await _next.Invoke(context);
await _next(context);
}
}
public static class RequestIPMiddlewareExtensions
{
/// <summary>
/// 用于引用请求IP中间件
/// </summary>
/// <param name="builder">扩展类型</param>
/// <returns>IApplicationBuilder</returns>
public static IApplicationBuilder UseRequestIPMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestIPMiddleware>();
}
}
}

View File

@@ -57,7 +57,7 @@
<rules>
<!-- 除非调试需要,把 .NET Core 程序集的 Debug 输出都屏蔽 Trace -> Debug-> Info ->Warn-> Error-> Critical-->
<!--跳过所有级别的Microsoft组件的日志记录-->
<!--<logger name="Microsoft.*" writeTo="blackhole" final="true" />-->
<logger name="Microsoft.*" writeTo="blackhole" final="true" />
<!-- 除非调试需要,把系统的 Debug 输出都屏蔽 -->
<!--<logger name="System.*" writeTo="blackhole" final="true" />-->
<!-- Quartz -->

View File

@@ -123,7 +123,7 @@ namespace ZR.Admin.WebApi
//2.<2E>ٿ<EFBFBD><D9BF><EFBFBD><EFBFBD><EFBFBD>Ȩ
app.UseAuthorization();
//<2F><><EFBFBD><EFBFBD>session
app.UseSession();
//app.UseSession();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
app.UseResponseCaching();
//<2F>ָ<EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>