From 87e82eefea48a2b3313ab88b1ba42fa059f2a865 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Tue, 26 Nov 2024 15:41:43 +0800 Subject: [PATCH] =?UTF-8?q?add=20-=20=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SysUserInfoService.cs | 10 ++++ .../service/SysUserRoleService.cs | 14 ++++- HighWayIot.Winform/Business/RoleBusiness.cs | 55 ++++++++++++++++++- HighWayIot.Winform/Business/XmlUtil.cs | 13 ++++- HighWayIot.Winform/MainForm/BaseForm.cs | 33 +++++++++++ HighWayIot.Winform/MainForm/LoginForm.cs | 2 +- .../SysConfigPages/RoleAddForm.cs | 8 +-- .../SysConfigPages/RoleConfigPage.cs | 8 +-- .../SysConfigPages/RoleUpdateForm.cs | 7 +-- .../SysConfigPages/UserAddForm.cs | 5 +- .../SysConfigPages/UserConfigPage.cs | 8 +-- .../SysConfigPages/UserUpDateForm.cs | 5 +- 12 files changed, 140 insertions(+), 28 deletions(-) diff --git a/HighWayIot.Repository/service/SysUserInfoService.cs b/HighWayIot.Repository/service/SysUserInfoService.cs index 1680d3a..bb49e51 100644 --- a/HighWayIot.Repository/service/SysUserInfoService.cs +++ b/HighWayIot.Repository/service/SysUserInfoService.cs @@ -11,6 +11,16 @@ namespace HighWayIot.Repository.service { public class SysUserInfoService { + private static readonly Lazy lazy = new Lazy(() => new SysUserInfoService()); + + public static SysUserInfoService Instance + { + get + { + return lazy.Value; + } + } + private LogHelper log = LogHelper.Instance; Repository _repository => new Repository("sqlserver"); diff --git a/HighWayIot.Repository/service/SysUserRoleService.cs b/HighWayIot.Repository/service/SysUserRoleService.cs index 0dcb70a..f77de97 100644 --- a/HighWayIot.Repository/service/SysUserRoleService.cs +++ b/HighWayIot.Repository/service/SysUserRoleService.cs @@ -11,6 +11,16 @@ namespace HighWayIot.Repository.service { public class SysUserRoleService { + private static readonly Lazy lazy = new Lazy(() => new SysUserRoleService()); + + public static SysUserRoleService Instance + { + get + { + return lazy.Value; + } + } + private LogHelper log = LogHelper.Instance; Repository _repository => new Repository("sqlserver"); @@ -24,7 +34,7 @@ namespace HighWayIot.Repository.service { List deviceInfo = _repository.GetList(); - + return deviceInfo; } catch (Exception ex) @@ -82,7 +92,7 @@ namespace HighWayIot.Repository.service { return _repository.Update(sysRoleEntity); } - catch(Exception ex) + catch (Exception ex) { log.Error("用户信息修改异常", ex); return false; diff --git a/HighWayIot.Winform/Business/RoleBusiness.cs b/HighWayIot.Winform/Business/RoleBusiness.cs index 748530a..f4ac206 100644 --- a/HighWayIot.Winform/Business/RoleBusiness.cs +++ b/HighWayIot.Winform/Business/RoleBusiness.cs @@ -1,4 +1,5 @@ -using System; +using HighWayIot.Repository.service; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,5 +13,57 @@ namespace HighWayIot.Winform.Business /// 当前登录人的姓名 /// public static string LoginUserName = string.Empty; + + /// + /// 用户数据业务实例 + /// + SysUserInfoService _sysUserInfoService = SysUserInfoService.Instance; + + /// + /// 角色数据业务实例 + /// + SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance; + + /// + /// XML读取类 + /// + XmlUtil xmlUtil = XmlUtil.Instance; + + /// + /// 页面权限偏移量对应表 + /// + List RoleConfigs; + + public RoleBusiness() + { + RoleConfigs = xmlUtil.ConfigReader(); + } + + /// + /// 判断现在登录的用户是否有指定页面的访问权限 + /// + /// 页面名称 + /// 有权限返回true,没权限返回false + public bool PageIsAccess(string pageName) + { + RoleConfig roleConfig = RoleConfigs.Where(x => x.PageName == pageName).FirstOrDefault(); + + //先判断页面是否配置过,没配置过的都默认有权限 + if (roleConfig == null) + { + return true; + } + + //获取当前用户的权限字符串 + string roleString = _sysUserRoleService.GetRoleInfos( + _sysUserInfoService.GetUserInfoByUserName(LoginUserName).First().UserRole + ).First().RoleSet; + + //转换为Char数组 + char[] chars = roleString.ToCharArray(); + + return chars[roleConfig.RoleIndex] == '1'; + + } } } diff --git a/HighWayIot.Winform/Business/XmlUtil.cs b/HighWayIot.Winform/Business/XmlUtil.cs index 311ed98..949f9d9 100644 --- a/HighWayIot.Winform/Business/XmlUtil.cs +++ b/HighWayIot.Winform/Business/XmlUtil.cs @@ -1,4 +1,5 @@ -using System; +using HighWayIot.Repository.service; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,6 +13,16 @@ namespace HighWayIot.Winform.Business /// public class XmlUtil { + private static readonly Lazy lazy = new Lazy(() => new XmlUtil()); + + public static XmlUtil Instance + { + get + { + return lazy.Value; + } + } + /// /// XML读写实例 /// diff --git a/HighWayIot.Winform/MainForm/BaseForm.cs b/HighWayIot.Winform/MainForm/BaseForm.cs index 7d160a7..f299960 100644 --- a/HighWayIot.Winform/MainForm/BaseForm.cs +++ b/HighWayIot.Winform/MainForm/BaseForm.cs @@ -19,8 +19,16 @@ namespace HighWayIot.Winform.MainForm { private static LogHelper logger = LogHelper.Instance; + /// + /// 用户控件列表 + /// List UserControls = new List(); + /// + /// 权限配置类 + /// + RoleBusiness roleBusiness = new RoleBusiness(); + public BaseForm() { InitializeComponent(); @@ -36,6 +44,7 @@ namespace HighWayIot.Winform.MainForm TimeDisplayTimer.Start(); NowLoginUserName.Text = RoleBusiness.LoginUserName; UserPanelSwitch(typeof(MonitorMainPage), "监控主页面"); + RoleControl(); } /// @@ -205,5 +214,29 @@ namespace HighWayIot.Winform.MainForm { TimeStripLabel.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } + + /// + /// 权限控制 + /// + private void RoleControl() + { + foreach(ToolStripMenuItem i in MainMenu.Items) + { + TraverseMenuitem(i); + } + } + + /// + /// 递归遍历MenuItem + /// + /// + private void TraverseMenuitem(ToolStripMenuItem item) + { + item.Enabled = roleBusiness.PageIsAccess(item.Text); + foreach (ToolStripMenuItem i in item.DropDownItems) + { + TraverseMenuitem(i); + } + } } } diff --git a/HighWayIot.Winform/MainForm/LoginForm.cs b/HighWayIot.Winform/MainForm/LoginForm.cs index 0a45897..c441eeb 100644 --- a/HighWayIot.Winform/MainForm/LoginForm.cs +++ b/HighWayIot.Winform/MainForm/LoginForm.cs @@ -15,7 +15,7 @@ namespace HighWayIot.Winform.MainForm { public partial class LoginForm : Form { - SysUserInfoService sysUserInfoService = new SysUserInfoService(); + SysUserInfoService sysUserInfoService = SysUserInfoService.Instance; public LoginForm() { diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleAddForm.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleAddForm.cs index 6c33dce..96f5e77 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleAddForm.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleAddForm.cs @@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// /// Sql业务类 /// - SysUserRoleService _sysUserRoleService; + SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance; /// /// XML读取类 /// - XmlUtil xmlUtil = new XmlUtil(); + XmlUtil xmlUtil = XmlUtil.Instance; /// /// 规则字符数组 @@ -40,11 +40,9 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// DataTable dt = new DataTable(); - public RoleAddForm(SysUserRoleService sysUserRoleService) + public RoleAddForm() { InitializeComponent(); - this._sysUserRoleService = sysUserRoleService; - Init(); } diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleConfigPage.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleConfigPage.cs index 4abf33c..0f4af65 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleConfigPage.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleConfigPage.cs @@ -17,7 +17,7 @@ namespace HighWayIot.Winform.UserControlPages { public partial class RoleConfigPage : UserControl { - SysUserRoleService roleService = new SysUserRoleService(); + SysUserRoleService roleService = SysUserRoleService.Instance; private static LogHelper logger = LogHelper.Instance; @@ -29,7 +29,7 @@ namespace HighWayIot.Winform.UserControlPages /// /// XML读取类 /// - XmlUtil xmlUtil = new XmlUtil(); + XmlUtil xmlUtil = XmlUtil.Instance; /// /// 页面规则偏移量配置 @@ -78,7 +78,7 @@ namespace HighWayIot.Winform.UserControlPages int index = RoleDataGridView.CurrentRow.Index; //前端数据顺序绑定的时候就是数据库的顺序,所以可以这么写。 - RoleUpdateForm form = new RoleUpdateForm(roleService, List[index]); + RoleUpdateForm form = new RoleUpdateForm(List[index]); form.ShowDialog(); List = roleService.GetRoleInfos(); @@ -87,7 +87,7 @@ namespace HighWayIot.Winform.UserControlPages private void AddRole_Click(object sender, EventArgs e) { - RoleAddForm form = new RoleAddForm(roleService); + RoleAddForm form = new RoleAddForm(); form.ShowDialog(); List = roleService.GetRoleInfos(); diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleUpdateForm.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleUpdateForm.cs index ee8af64..6fa5ec1 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleUpdateForm.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/RoleUpdateForm.cs @@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// /// Sql业务类 /// - SysUserRoleService _sysUserRoleService; + SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance; /// /// XML读取类 /// - XmlUtil xmlUtil = new XmlUtil(); + XmlUtil xmlUtil = XmlUtil.Instance; /// /// 规则字符数组 @@ -45,10 +45,9 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// SysRoleEntity _roleEntity; - public RoleUpdateForm(SysUserRoleService sysUserRoleService, SysRoleEntity sysRoleEntity) + public RoleUpdateForm(SysRoleEntity sysRoleEntity) { InitializeComponent(); - this._sysUserRoleService = sysUserRoleService; this._roleEntity = sysRoleEntity; Init(); } diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserAddForm.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserAddForm.cs index 8331e9d..422d3b3 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserAddForm.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserAddForm.cs @@ -17,12 +17,11 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// /// 角色数据库业务实例 /// - SysUserInfoService SysUserInfoService; + SysUserInfoService SysUserInfoService = SysUserInfoService.Instance; - public UserAddForm(SysUserInfoService sysUserInfoService, string[] roleList) + public UserAddForm(string[] roleList) { InitializeComponent(); - this.SysUserInfoService = sysUserInfoService; UserRoleComboBox.DataSource = roleList; } diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserConfigPage.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserConfigPage.cs index 56a0056..8b31370 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserConfigPage.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserConfigPage.cs @@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages /// /// 用户数据库业务实例 /// - private SysUserInfoService sysUserInfoService = new SysUserInfoService(); + private SysUserInfoService sysUserInfoService = SysUserInfoService.Instance; /// /// 角色数据库业务实例 /// - private SysUserRoleService sysUserRoleService = new SysUserRoleService(); + private SysUserRoleService sysUserRoleService = SysUserRoleService.Instance; /// /// 用户信息列表 @@ -67,7 +67,7 @@ namespace HighWayIot.Winform.UserControlPages entity.UserName = UserInfoDataGridView.Rows[a].Cells["UserName"].Value.ToString(); entity.UserRole = UserInfoDataGridView.Rows[a].Cells["UserRole"].Value.ToString(); entity.Password = UserInfoDataGridView.Rows[a].Cells["Password"].Value.ToString(); - UserUpDateForm form = new UserUpDateForm(sysUserInfoService, entity, RoleNames); + UserUpDateForm form = new UserUpDateForm(entity, RoleNames); form.ShowDialog(); List = sysUserInfoService.GetUserInfos(); UserInfoDataGridView.DataSource = null; @@ -76,7 +76,7 @@ namespace HighWayIot.Winform.UserControlPages private void AddUser_Click(object sender, EventArgs e) { - UserAddForm form = new UserAddForm(sysUserInfoService, RoleNames); + UserAddForm form = new UserAddForm(RoleNames); form.ShowDialog(); List = sysUserInfoService.GetUserInfos(); UserInfoDataGridView.DataSource = null; diff --git a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserUpDateForm.cs b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserUpDateForm.cs index ec0dc22..514a5fc 100644 --- a/HighWayIot.Winform/UserControlPages/SysConfigPages/UserUpDateForm.cs +++ b/HighWayIot.Winform/UserControlPages/SysConfigPages/UserUpDateForm.cs @@ -17,17 +17,16 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages /// /// 角色数据库业务实例 /// - SysUserInfoService _userInfoService; + SysUserInfoService _userInfoService = SysUserInfoService.Instance; /// /// 要修改的实体 /// SysUserEntity _userEntity; - public UserUpDateForm(SysUserInfoService sysUserInfoService, SysUserEntity userEntity, string[] roleList) + public UserUpDateForm(SysUserEntity userEntity, string[] roleList) { InitializeComponent(); - this._userInfoService = sysUserInfoService; this._userEntity = userEntity; UserRoleComboBox.DataSource = roleList; PasswordTextBox.Text = _userEntity.Password;