From f78ccc2b4cc0992a1d4e18a9c6e3c235146c16b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Sun, 12 Nov 2023 10:08:42 +0800
Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Infrastructure/JwtUtil.cs | 1 +
.../WebExtensions/EntityExtension.cs | 1 +
.../WebExtensions/HttpContextExtension.cs | 11 +++++
.../Controllers/System/SysLoginController.cs | 10 +----
.../Controllers/System/SysNoticeController.cs | 2 +-
ZR.Admin.WebApi/appsettings.json | 2 +-
ZR.ServiceCore/SqlSugar/DataPermi.cs | 31 +------------
ZR.ServiceCore/SqlSugar/DataPermiSevice.cs | 43 +++++++++++++++++++
ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs | 2 +-
9 files changed, 61 insertions(+), 42 deletions(-)
create mode 100644 ZR.ServiceCore/SqlSugar/DataPermiSevice.cs
diff --git a/Infrastructure/JwtUtil.cs b/Infrastructure/JwtUtil.cs
index bf6a71d..4edc598 100644
--- a/Infrastructure/JwtUtil.cs
+++ b/Infrastructure/JwtUtil.cs
@@ -157,6 +157,7 @@ namespace Infrastructure
{
new Claim(ClaimTypes.PrimarySid, user.UserId.ToString()),
new Claim(ClaimTypes.Name, user.UserName),
+ new Claim(ClaimTypes.GroupSid, user.DeptId.ToString()),
new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(user))
};
diff --git a/Infrastructure/WebExtensions/EntityExtension.cs b/Infrastructure/WebExtensions/EntityExtension.cs
index 3022abf..6bec717 100644
--- a/Infrastructure/WebExtensions/EntityExtension.cs
+++ b/Infrastructure/WebExtensions/EntityExtension.cs
@@ -19,6 +19,7 @@ namespace Infrastructure
types.GetProperty("CreateBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("Create_by", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("UserId", flag)?.SetValue(source, context.GetUId(), null);
+ types.GetProperty("DeptId", flag)?.SetValue(source, context.GetDeptId(), null);
return source;
}
diff --git a/Infrastructure/WebExtensions/HttpContextExtension.cs b/Infrastructure/WebExtensions/HttpContextExtension.cs
index 23efa03..db05d99 100644
--- a/Infrastructure/WebExtensions/HttpContextExtension.cs
+++ b/Infrastructure/WebExtensions/HttpContextExtension.cs
@@ -80,6 +80,17 @@ namespace Infrastructure.Extensions
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
}
+ ///
+ /// 获取部门id
+ ///
+ ///
+ ///
+ public static long GetDeptId(this HttpContext context)
+ {
+ var deptId = context.User.FindFirstValue(ClaimTypes.GroupSid);
+ return !string.IsNullOrEmpty(deptId) ? long.Parse(deptId) : 0;
+ }
+
///
/// 获取登录用户名
///
diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs
index 4122662..217881c 100644
--- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs
@@ -1,6 +1,5 @@
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Options;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
using ZR.Model.System.Dto;
@@ -15,8 +14,6 @@ namespace ZR.Admin.WebApi.Controllers.System
[ApiExplorerSettings(GroupName = "sys")]
public class SysLoginController : BaseController
{
- //static readonly NLog.Logger logger = NLog.LogManager.GetLogger("LoginController");
- private readonly IHttpContextAccessor httpContextAccessor;
private readonly ISysUserService sysUserService;
private readonly ISysMenuService sysMenuService;
private readonly ISysLoginService sysLoginService;
@@ -24,20 +21,16 @@ namespace ZR.Admin.WebApi.Controllers.System
private readonly ICaptcha SecurityCodeHelper;
private readonly ISysConfigService sysConfigService;
private readonly ISysRoleService roleService;
- private readonly OptionsSetting optionSettings;
public SysLoginController(
- IHttpContextAccessor contextAccessor,
ISysMenuService sysMenuService,
ISysUserService sysUserService,
ISysLoginService sysLoginService,
ISysPermissionService permissionService,
ISysConfigService configService,
ISysRoleService sysRoleService,
- ICaptcha captcha,
- IOptions optionSettings)
+ ICaptcha captcha)
{
- httpContextAccessor = contextAccessor;
SecurityCodeHelper = captcha;
this.sysMenuService = sysMenuService;
this.sysUserService = sysUserService;
@@ -45,7 +38,6 @@ namespace ZR.Admin.WebApi.Controllers.System
this.permissionService = permissionService;
this.sysConfigService = configService;
roleService = sysRoleService;
- this.optionSettings = optionSettings.Value;
}
diff --git a/ZR.Admin.WebApi/Controllers/System/SysNoticeController.cs b/ZR.Admin.WebApi/Controllers/System/SysNoticeController.cs
index 18e304e..14057b8 100644
--- a/ZR.Admin.WebApi/Controllers/System/SysNoticeController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/SysNoticeController.cs
@@ -38,7 +38,7 @@ namespace ZR.Admin.WebApi.Controllers.System
public IActionResult QueryNotice([FromQuery] SysNoticeQueryDto parm)
{
var predicate = Expressionable.Create();
-
+
predicate = predicate.And(m => m.Status == 0);
var response = _SysNoticeService.GetPages(predicate.ToExpression(), parm);
return SUCCESS(response);
diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json
index 39c049c..d9f6253 100644
--- a/ZR.Admin.WebApi/appsettings.json
+++ b/ZR.Admin.WebApi/appsettings.json
@@ -37,7 +37,7 @@
"ShowDbLog": true, //是否打印db日志
"InitDb": false, //是否初始化db
"DemoMode": false, //是否演示模式
- "SingleLogin": false,//是否单点登录
+ "SingleLogin": false,//是否允许多设备/浏览器登录
"Upload": {
"uploadUrl": "http://localhost:8888", //本地存储资源访问路径
"localSavePath": "", //本地上传默认文件存储目录 wwwroot
diff --git a/ZR.ServiceCore/SqlSugar/DataPermi.cs b/ZR.ServiceCore/SqlSugar/DataPermi.cs
index e9cc866..508eae0 100644
--- a/ZR.ServiceCore/SqlSugar/DataPermi.cs
+++ b/ZR.ServiceCore/SqlSugar/DataPermi.cs
@@ -1,5 +1,6 @@
using Infrastructure;
using SqlSugar.IOC;
+using ZR.Model.Business;
using ZR.Model.System;
namespace ZR.ServiceCore.SqlSugar
@@ -85,35 +86,5 @@ namespace ZR.ServiceCore.SqlSugar
db.QueryFilter.AddTableFilter(expRole.ToExpression());
db.QueryFilter.AddTableFilter(expLoginlog.ToExpression());
}
-
- public static void FilterData1(int configId)
- {
- //获取当前用户的信息
- var user = JwtUtil.GetLoginUser(App.HttpContext);
- if (user == null) return;
- var db = DbScoped.SugarScope.GetConnectionScope(configId);
-
- foreach (var role in user.Roles.OrderBy(f => f.DataScope))
- {
- var dataScope = (DataPermiEnum)role.DataScope;
- if (DataPermiEnum.All.Equals(dataScope))//所有权限
- {
- break;
- }
- else if (DataPermiEnum.CUSTOM.Equals(dataScope))//自定数据权限
- {
- }
- else if (DataPermiEnum.DEPT.Equals(dataScope))//本部门数据
- {
- }
- else if (DataPermiEnum.DEPT_CHILD.Equals(dataScope))//本部门及以下数据
- {
-
- }
- else if (DataPermiEnum.SELF.Equals(dataScope))//仅本人数据
- {
- }
- }
- }
}
}
diff --git a/ZR.ServiceCore/SqlSugar/DataPermiSevice.cs b/ZR.ServiceCore/SqlSugar/DataPermiSevice.cs
new file mode 100644
index 0000000..f5d6706
--- /dev/null
+++ b/ZR.ServiceCore/SqlSugar/DataPermiSevice.cs
@@ -0,0 +1,43 @@
+using Infrastructure;
+using SqlSugar.IOC;
+using ZR.ServiceCore.SqlSugar;
+
+namespace ZR.ServiceCore
+{
+ public class DataPermiSevice
+ {
+ ///
+ /// 你的业务库数据权限过滤方法
+ ///
+ ///
+ public static void FilterData(int configId)
+ {
+ //获取当前用户的信息
+ var user = JwtUtil.GetLoginUser(App.HttpContext);
+ if (user == null) return;
+ var db = DbScoped.SugarScope.GetConnectionScope(configId);
+
+ foreach (var role in user.Roles.OrderBy(f => f.DataScope))
+ {
+ var dataScope = (DataPermiEnum)role.DataScope;
+ if (DataPermiEnum.All.Equals(dataScope))//所有权限
+ {
+ break;
+ }
+ else if (DataPermiEnum.CUSTOM.Equals(dataScope))//自定数据权限
+ {
+ }
+ else if (DataPermiEnum.DEPT.Equals(dataScope))//本部门数据
+ {
+ }
+ else if (DataPermiEnum.DEPT_CHILD.Equals(dataScope))//本部门及以下数据
+ {
+
+ }
+ else if (DataPermiEnum.SELF.Equals(dataScope))//仅本人数据
+ {
+ }
+ }
+ }
+ }
+}
diff --git a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs
index 6582814..cc45692 100644
--- a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs
+++ b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs
@@ -42,7 +42,7 @@ namespace ZR.ServiceCore.SqlSugar
{
DataPermi.FilterData(0);
//ConfigId = 1的数据权限过滤
- //DataPermi.FilterData1(1);
+ //DataPermiSevice.FilterData(1);
}
iocList.ForEach(iocConfig =>