优化代码
This commit is contained in:
parent
72b6fba559
commit
b27ea85fed
@ -32,7 +32,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
private readonly ISysPermissionService permissionService;
|
||||
private readonly SecurityCodeHelper SecurityCodeHelper;
|
||||
private readonly ISysConfigService sysConfigService;
|
||||
|
||||
private readonly ISysRoleService roleService;
|
||||
public SysLoginController(
|
||||
IHttpContextAccessor contextAccessor,
|
||||
ISysMenuService sysMenuService,
|
||||
@ -40,6 +40,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
ISysLoginService sysLoginService,
|
||||
ISysPermissionService permissionService,
|
||||
ISysConfigService configService,
|
||||
ISysRoleService sysRoleService,
|
||||
SecurityCodeHelper captcha)
|
||||
{
|
||||
httpContextAccessor = contextAccessor;
|
||||
@ -49,6 +50,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
this.sysLoginService = sysLoginService;
|
||||
this.permissionService = permissionService;
|
||||
this.sysConfigService = configService;
|
||||
roleService = sysRoleService;
|
||||
}
|
||||
|
||||
|
||||
@ -67,17 +69,18 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
||||
if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower()))
|
||||
{
|
||||
throw new CustomException(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
||||
return CustomError(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
||||
}
|
||||
|
||||
var user = sysLoginService.Login(loginBody, AsyncFactory.RecordLogInfo(httpContextAccessor.HttpContext, "0", "login"));
|
||||
#region 存入cookie Action校验权限使用
|
||||
//角色集合 eg: admin,yunying,common
|
||||
List<string> roles = permissionService.GetRolePermission(user);
|
||||
//List<string> roles = permissionService.GetRolePermission(user);
|
||||
List<SysRole> roles = roleService.SelectRolePermissionByUserId(user.UserId);
|
||||
//权限集合 eg *:*:*,system:user:list
|
||||
List<string> permissions = permissionService.GetMenuPermission(user);
|
||||
#endregion
|
||||
LoginUser loginUser = new LoginUser(user.UserId, user.UserName, roles, permissions);
|
||||
LoginUser loginUser = new LoginUser(user, roles, permissions);
|
||||
|
||||
return SUCCESS(JwtUtil.GenerateJwtToken(HttpContext.AddClaims(loginUser)));
|
||||
}
|
||||
|
||||
@ -130,12 +130,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
sysRoleDto.Create_by = HttpContext.GetName();
|
||||
sysRoleService.CheckRoleAllowed(sysRoleDto);
|
||||
|
||||
bool result = sysRoleService.UseTran2(() =>
|
||||
{
|
||||
//删除角色菜单
|
||||
sysRoleService.DeleteRoleMenuByRoleId(sysRoleDto.RoleId);
|
||||
sysRoleService.InsertRoleMenu(sysRoleDto);
|
||||
});
|
||||
bool result = sysRoleService.AuthDataScope(sysRoleDto);
|
||||
|
||||
return SUCCESS(result);
|
||||
}
|
||||
|
||||
118
ZR.Admin.WebApi/Extensions/DbExtension.cs
Normal file
118
ZR.Admin.WebApi/Extensions/DbExtension.cs
Normal file
@ -0,0 +1,118 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SqlSugar;
|
||||
using SqlSugar.IOC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ZR.Admin.WebApi.Framework;
|
||||
using ZR.Model.System;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
public static class DbExtension
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
public static void AddDb(IConfiguration Configuration)
|
||||
{
|
||||
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
||||
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
||||
string dbKey = Configuration[OptionsSetting.DbKey];
|
||||
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
||||
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//自动释放
|
||||
}
|
||||
});
|
||||
//每次Sql执行前事件
|
||||
var db0 = DbScoped.SugarScope.GetConnection(0);
|
||||
db0.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
|
||||
FilterData(db0);
|
||||
|
||||
logger.Info($"Sql语句:{sql},{param}");
|
||||
};
|
||||
//出错打印日志
|
||||
db0.Aop.OnError = (e) =>
|
||||
{
|
||||
logger.Error(e, $"执行SQL出错:{e.Message}");
|
||||
};
|
||||
//SQL执行完
|
||||
db0.Aop.OnLogExecuted = (sql, pars) =>
|
||||
{
|
||||
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
|
||||
};
|
||||
//Db1
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//Console.WriteLine("【SQL语句Bus】" + sql.ToLower() + "\r\n" + param);
|
||||
logger.Info($"Sql语句:{sql}, {param}");
|
||||
};
|
||||
//Db1错误日志
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
|
||||
{
|
||||
logger.Error($"执行Sql语句失败:{e.Sql},原因:{e.Message}");
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
private static void FilterData(SqlSugarProvider db0)
|
||||
{
|
||||
var u = App.User;
|
||||
if (u != null && u.Identity.IsAuthenticated)
|
||||
{
|
||||
//获取当前用户的信息
|
||||
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
||||
if (user != null)
|
||||
{
|
||||
//非管理员过滤数据权限
|
||||
if (!user.RoleIds.Any(f => f.Equals("admin")))
|
||||
{
|
||||
//TODO 实现范围过滤
|
||||
foreach (var role in user.Roles)
|
||||
{
|
||||
string dataScope = role.DataScope;
|
||||
if ("1".Equals(dataScope))
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if ("2".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));
|
||||
}
|
||||
else if ("3".Equals(dataScope))
|
||||
{
|
||||
var filter1 = new TableFilterItem<SysDept>(it => it.DeptId == user.DeptId);
|
||||
}
|
||||
else if ("4".Equals(dataScope))
|
||||
{
|
||||
|
||||
}
|
||||
else if ("5".Equals(dataScope))
|
||||
{
|
||||
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO 在此实现数据过滤
|
||||
//DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(new TableFilterItem<SysUser>(it => it.DeptId == 333)); //为Order表置全局条件
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -30,6 +30,5 @@ namespace ZR.Admin.WebApi.Framework
|
||||
|
||||
return sysLogininfor;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@ namespace ZR.Admin.WebApi.Framework
|
||||
/// </summary>
|
||||
/// <param name="jwtToken"></param>
|
||||
/// <returns></returns>
|
||||
private static LoginUser ValidateJwtToken(IEnumerable<Claim> jwtToken)
|
||||
public static LoginUser ValidateJwtToken(IEnumerable<Claim> jwtToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@ -9,12 +9,8 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using SqlSugar.IOC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Admin.WebApi.Framework;
|
||||
@ -24,13 +20,11 @@ namespace ZR.Admin.WebApi
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
CurrentEnvironment = hostEnvironment;
|
||||
}
|
||||
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private IWebHostEnvironment CurrentEnvironment { get; }
|
||||
public IConfiguration Configuration { get; }
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
@ -72,7 +66,7 @@ namespace ZR.Admin.WebApi
|
||||
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
||||
});
|
||||
|
||||
InjectServices(services);
|
||||
InjectServices(services, Configuration);
|
||||
|
||||
services.AddMvc(options =>
|
||||
{
|
||||
@ -94,7 +88,6 @@ namespace ZR.Admin.WebApi
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
//使可以多次多去body内容
|
||||
app.Use((context, next) =>
|
||||
@ -135,59 +128,15 @@ namespace ZR.Admin.WebApi
|
||||
/// 注册Services服务
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
private void InjectServices(IServiceCollection services)
|
||||
/// <param name="configuration"></param>
|
||||
private void InjectServices(IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddAppService();
|
||||
|
||||
//开启计划任务
|
||||
services.AddTaskSchedulers();
|
||||
|
||||
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
||||
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
||||
string dbKey = Configuration[OptionsSetting.DbKey];
|
||||
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
||||
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//自动释放
|
||||
}
|
||||
});
|
||||
|
||||
//调式代码 用来打印SQL
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//Console.WriteLine("【SQL语句】" + sql.ToLower() + "\r\n" + param);
|
||||
logger.Info($"Sql语句:{sql},{param}");
|
||||
};
|
||||
//出错打印日志
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnError = (e) =>
|
||||
{
|
||||
Console.WriteLine($"[执行Sql出错]{e.Message},SQL={e.Sql}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
//调式代码 用来打印SQL
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//Console.WriteLine("【SQL语句Bus】" + sql.ToLower() + "\r\n" + param);
|
||||
logger.Info($"Sql语句:{sql}, {param}");
|
||||
};
|
||||
//Bus Db错误日志
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
|
||||
{
|
||||
logger.Error($"执行Sql语句失败:{e.Sql},原因:{e.Message}");
|
||||
};
|
||||
//³õʼ»¯db
|
||||
DbExtension.AddDb(configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace ZR.Model.System
|
||||
@ -10,6 +11,7 @@ namespace ZR.Model.System
|
||||
public class LoginUser
|
||||
{
|
||||
public long UserId { get; set; }
|
||||
public long DeptId { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string NickName { get; set; }
|
||||
/// <summary>
|
||||
@ -17,6 +19,10 @@ namespace ZR.Model.System
|
||||
/// </summary>
|
||||
public List<string> RoleIds { get; set; }
|
||||
/// <summary>
|
||||
/// 角色集合
|
||||
/// </summary>
|
||||
public List<SysRole> Roles { get; set; }
|
||||
/// <summary>
|
||||
/// 权限集合
|
||||
/// </summary>
|
||||
public List<string> Permissions { get; set; }
|
||||
@ -24,11 +30,6 @@ namespace ZR.Model.System
|
||||
{
|
||||
}
|
||||
|
||||
public LoginUser(long userId, string userName)
|
||||
{
|
||||
UserId = userId;
|
||||
UserName = userName;
|
||||
}
|
||||
public LoginUser(long userId, string userName, List<string> roleIds, List<string> permissions)
|
||||
{
|
||||
UserId = userId;
|
||||
@ -36,5 +37,14 @@ namespace ZR.Model.System
|
||||
RoleIds = roleIds;
|
||||
Permissions = permissions;
|
||||
}
|
||||
public LoginUser(SysUser user, List<SysRole> roles, List<string> permissions)
|
||||
{
|
||||
UserId = user.UserId;
|
||||
UserName = user.UserName;
|
||||
DeptId = user.DeptId;
|
||||
Roles = roles;
|
||||
RoleIds = roles.Select(f => f.RoleKey).ToList();
|
||||
Permissions = permissions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,6 +76,12 @@ namespace ZR.Service.System.IService
|
||||
/// <returns></returns>
|
||||
public int DeleteRoleMenuByRoleId(long roleId);
|
||||
|
||||
/// <summary>
|
||||
/// 授权数据范围
|
||||
/// </summary>
|
||||
/// <param name="role"></param>
|
||||
/// <returns></returns>
|
||||
bool AuthDataScope(SysRole role);
|
||||
#region Service
|
||||
|
||||
|
||||
|
||||
@ -148,11 +148,33 @@ namespace ZR.Service
|
||||
return SysRoleRepository.DeleteRoleMenuByRoleId(roleId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改数据权限信息
|
||||
/// </summary>
|
||||
/// <param name="sysRoleDto"></param>
|
||||
/// <returns></returns>
|
||||
public bool AuthDataScope(SysRole sysRoleDto)
|
||||
{
|
||||
return UseTran2(() =>
|
||||
{
|
||||
int result = Update(sysRoleDto, it => new
|
||||
{
|
||||
it.DataScope
|
||||
}, f => f.RoleId == sysRoleDto.RoleId);
|
||||
|
||||
//if (result > 0 && sysRoleDto.DataScope == "2")
|
||||
//{
|
||||
//删除角色菜单
|
||||
DeleteRoleMenuByRoleId(sysRoleDto.RoleId);
|
||||
InsertRoleMenu(sysRoleDto);
|
||||
//}
|
||||
});
|
||||
}
|
||||
#region Service
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 新增角色菜单信息
|
||||
/// 批量新增角色菜单信息
|
||||
/// </summary>
|
||||
/// <param name="sysRoleDto"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user