1
0
Fork 0

add - 权限控制实现

nodyang
SoulStar 2 months ago
parent 4a76141831
commit 87e82eefea

@ -11,6 +11,16 @@ namespace HighWayIot.Repository.service
{
public class SysUserInfoService
{
private static readonly Lazy<SysUserInfoService> lazy = new Lazy<SysUserInfoService>(() => new SysUserInfoService());
public static SysUserInfoService Instance
{
get
{
return lazy.Value;
}
}
private LogHelper log = LogHelper.Instance;
Repository<SysUserEntity> _repository => new Repository<SysUserEntity>("sqlserver");

@ -11,6 +11,16 @@ namespace HighWayIot.Repository.service
{
public class SysUserRoleService
{
private static readonly Lazy<SysUserRoleService> lazy = new Lazy<SysUserRoleService>(() => new SysUserRoleService());
public static SysUserRoleService Instance
{
get
{
return lazy.Value;
}
}
private LogHelper log = LogHelper.Instance;
Repository<SysRoleEntity> _repository => new Repository<SysRoleEntity>("sqlserver");
@ -24,7 +34,7 @@ namespace HighWayIot.Repository.service
{
List<SysRoleEntity> 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;

@ -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
/// 当前登录人的姓名
/// </summary>
public static string LoginUserName = string.Empty;
/// <summary>
/// 用户数据业务实例
/// </summary>
SysUserInfoService _sysUserInfoService = SysUserInfoService.Instance;
/// <summary>
/// 角色数据业务实例
/// </summary>
SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance;
/// <summary>
/// XML读取类
/// </summary>
XmlUtil xmlUtil = XmlUtil.Instance;
/// <summary>
/// 页面权限偏移量对应表
/// </summary>
List<RoleConfig> RoleConfigs;
public RoleBusiness()
{
RoleConfigs = xmlUtil.ConfigReader();
}
/// <summary>
/// 判断现在登录的用户是否有指定页面的访问权限
/// </summary>
/// <param name="pageName">页面名称</param>
/// <returns>有权限返回true没权限返回false</returns>
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';
}
}
}

@ -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
/// </summary>
public class XmlUtil
{
private static readonly Lazy<XmlUtil> lazy = new Lazy<XmlUtil>(() => new XmlUtil());
public static XmlUtil Instance
{
get
{
return lazy.Value;
}
}
/// <summary>
/// XML读写实例
/// </summary>

@ -19,8 +19,16 @@ namespace HighWayIot.Winform.MainForm
{
private static LogHelper logger = LogHelper.Instance;
/// <summary>
/// 用户控件列表
/// </summary>
List<UserControl> UserControls = new List<UserControl>();
/// <summary>
/// 权限配置类
/// </summary>
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();
}
/// <summary>
@ -205,5 +214,29 @@ namespace HighWayIot.Winform.MainForm
{
TimeStripLabel.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
/// <summary>
/// 权限控制
/// </summary>
private void RoleControl()
{
foreach(ToolStripMenuItem i in MainMenu.Items)
{
TraverseMenuitem(i);
}
}
/// <summary>
/// 递归遍历MenuItem
/// </summary>
/// <param name="item"></param>
private void TraverseMenuitem(ToolStripMenuItem item)
{
item.Enabled = roleBusiness.PageIsAccess(item.Text);
foreach (ToolStripMenuItem i in item.DropDownItems)
{
TraverseMenuitem(i);
}
}
}
}

@ -15,7 +15,7 @@ namespace HighWayIot.Winform.MainForm
{
public partial class LoginForm : Form
{
SysUserInfoService sysUserInfoService = new SysUserInfoService();
SysUserInfoService sysUserInfoService = SysUserInfoService.Instance;
public LoginForm()
{

@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// <summary>
/// Sql业务类
/// </summary>
SysUserRoleService _sysUserRoleService;
SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance;
/// <summary>
/// XML读取类
/// </summary>
XmlUtil xmlUtil = new XmlUtil();
XmlUtil xmlUtil = XmlUtil.Instance;
/// <summary>
/// 规则字符数组
@ -40,11 +40,9 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// </summary>
DataTable dt = new DataTable();
public RoleAddForm(SysUserRoleService sysUserRoleService)
public RoleAddForm()
{
InitializeComponent();
this._sysUserRoleService = sysUserRoleService;
Init();
}

@ -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
/// <summary>
/// XML读取类
/// </summary>
XmlUtil xmlUtil = new XmlUtil();
XmlUtil xmlUtil = XmlUtil.Instance;
/// <summary>
/// 页面规则偏移量配置
@ -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();

@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// <summary>
/// Sql业务类
/// </summary>
SysUserRoleService _sysUserRoleService;
SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance;
/// <summary>
/// XML读取类
/// </summary>
XmlUtil xmlUtil = new XmlUtil();
XmlUtil xmlUtil = XmlUtil.Instance;
/// <summary>
/// 规则字符数组
@ -45,10 +45,9 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// </summary>
SysRoleEntity _roleEntity;
public RoleUpdateForm(SysUserRoleService sysUserRoleService, SysRoleEntity sysRoleEntity)
public RoleUpdateForm(SysRoleEntity sysRoleEntity)
{
InitializeComponent();
this._sysUserRoleService = sysUserRoleService;
this._roleEntity = sysRoleEntity;
Init();
}

@ -17,12 +17,11 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// <summary>
/// 角色数据库业务实例
/// </summary>
SysUserInfoService SysUserInfoService;
SysUserInfoService SysUserInfoService = SysUserInfoService.Instance;
public UserAddForm(SysUserInfoService sysUserInfoService, string[] roleList)
public UserAddForm(string[] roleList)
{
InitializeComponent();
this.SysUserInfoService = sysUserInfoService;
UserRoleComboBox.DataSource = roleList;
}

@ -18,12 +18,12 @@ namespace HighWayIot.Winform.UserControlPages
/// <summary>
/// 用户数据库业务实例
/// </summary>
private SysUserInfoService sysUserInfoService = new SysUserInfoService();
private SysUserInfoService sysUserInfoService = SysUserInfoService.Instance;
/// <summary>
/// 角色数据库业务实例
/// </summary>
private SysUserRoleService sysUserRoleService = new SysUserRoleService();
private SysUserRoleService sysUserRoleService = SysUserRoleService.Instance;
/// <summary>
/// 用户信息列表
@ -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;

@ -17,17 +17,16 @@ namespace HighWayIot.Winform.UserControlPages.SysConfigPages
/// <summary>
/// 角色数据库业务实例
/// </summary>
SysUserInfoService _userInfoService;
SysUserInfoService _userInfoService = SysUserInfoService.Instance;
/// <summary>
/// 要修改的实体
/// </summary>
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;

Loading…
Cancel
Save