一.建立两张表

//存放要控制的窗体控件

CREATE TABLE [dbo].[AuthControl] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[NiceName] VARCHAR (200) NULL,
[FormFullName] VARCHAR (200) NOT NULL,
[ControlName] VARCHAR (200) NOT NULL
);

//存放用户的控件控制状态

CREATE TABLE [dbo].[AuthUser] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[AuthControlID] INT NOT NULL,
[UserName] VARCHAR (50) NOT NULL,
[IsEnable] BIT NOT NULL,
[IsVisible] BIT NOT NULL
);

二.写个方法.并把方法放到From_Load中,

代码里传入的参数是要控制的窗体和一个自定义的类,是提供用户名和数据访问的,可替换为其它类

功能:

从表中取出窗体和用户数据.

在窗体的控件和菜单上循环匹配取出的数据有无要控制的控件,并按用户设置进行设定

        public static void AuthUserControl(System.Windows.Forms.Form form, HRBase.UserRight login)
{
HRBase.UserRight.DataBaseOperate DB = (HRBase.UserRight.DataBaseOperate)login.NewDataBase("wsprint", "WERP", HRBase.UserRight.DataBaseOperate.Debug.Formal);
string cmd = $@"select *
from AuthControl a
left join AuthUser b on a.id = b.AuthControlID
where a.FormFullName = '{(form.GetType().FullName)}'";
DataTable dt = DB.SelectToTable(cmd);
//有行时才控制
foreach (DataRow row in dt.Rows)
{
//Control控制
try
{
if (form.Controls.Find(row["ControlName"].ToString(), true).Length > )
{
//默认可视不可用
form.Controls.Find(row["ControlName"].ToString(), true)[].Enabled = false;
form.Controls.Find(row["ControlName"].ToString(), true)[].Visible = true;
//设定用户设置
DataRow[] dtrow = dt.Select($@"FormFullName = '{(form.GetType().FullName)}' and ControlName = '{(row["ControlName"].ToString())}' and UserName = '{(login.UserId)}'");
if(dtrow.Count() > )
{
form.Controls.Find(dtrow[]["ControlName"].ToString(), true)[].Enabled = (bool)(dtrow[]["IsEnable"] ?? false);
form.Controls.Find(dtrow[]["ControlName"].ToString(), true)[].Visible = (bool)(dtrow[]["IsVisible"] ?? true); }
}
}
catch { }
//菜单控制
try
{
if (form.MainMenuStrip.Items.Find(row["ControlName"].ToString(), true).Length > )
{
//默认可视不可用
form.MainMenuStrip.Items.Find(row["ControlName"].ToString(), true)[].Enabled = false;
form.MainMenuStrip.Items.Find(row["ControlName"].ToString(), true)[].Visible = true;
//设定用户设置
DataRow[] dtrow = dt.Select($@"FormFullName = '{(form.GetType().FullName)}' and ControlName = '{(row["ControlName"].ToString())}' and UserName = '{(login.UserId)}'");
if (dtrow.Count() > )
{
form.MainMenuStrip.Items.Find(dtrow[]["ControlName"].ToString(), true)[].Enabled = (bool)(dtrow[]["IsEnable"] ?? false);
form.MainMenuStrip.Items.Find(dtrow[]["ControlName"].ToString(), true)[].Visible = (bool)(dtrow[]["IsVisible"] ?? true); } }
}
catch { }
}
}

三.写个窗体用来保存权限数据

以下可选做

四,可以在通过反射namespace,获取窗体名和窗体的控件名

    public partial class SelectAuthControlForm : Form
{
DataTable dt1=new DataTable(), dt2 = new DataTable();
/// <summary>
/// 选择的窗体FullName
/// </summary>
/// <value>The full name of the select form.</value>
public string SelectFormFullName { get;private set; }
/// <summary>
/// 选择的窗体上的控件Name
/// </summary>
/// <value>The name of the select control.</value>
public string SelectControlName { get;private set; } public SelectAuthControlForm()
{
InitializeComponent();
dt1.Columns.Add("Text");
dt1.Columns.Add("FullName");
dt2.Columns.Add("Text");
dt2.Columns.Add("Name");
} private void SelectAuthControlForm_Load(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
this.Show();
dataGridView1.AddColumn("Text", "窗体标题");
dataGridView1.AddColumn("FullName", "窗体类名");
dataGridView2.AddColumn("Text", "控件文本");
dataGridView2.AddColumn("Name", "控件类名");
Application.DoEvents();
var classes = Assembly.Load("erp").GetTypes();
foreach (var item in classes)
{
if ((item.BaseType != null ? item.BaseType.Name : "") == "Form")
{
try
{
var obj = Assembly.Load("erp").CreateInstance(item.FullName);
PropertyInfo propertyText = obj.GetType().GetProperty("Text"); //获取指定名称的属性
string valueText = propertyText.GetValue(obj, null).ToString(); //获取属性值
DataRow row = dt1.Rows.Add(valueText, item.FullName);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
}
dataGridView1.SetDataSource(dt1); this.Cursor = Cursors.Arrow;
Application.DoEvents();
}
private void AddTabControl(TabPage page)
{
foreach (Control con in page.Controls)
{
Type type = con.GetType();
string str1 = con.GetType().GetProperty("Text").GetValue(con, null).ToString();
string strname1 = con.GetType().GetProperty("Name").GetValue(con, null).ToString();
if (((Control)con).Controls.Count > )
{
AddControlInList(type.FullName);
}
else
{
PropertyInfo propertyText = con.GetType().GetProperty("Text"); //获取指定名称的属性
string valueText = propertyText.GetValue(con, null).ToString(); //获取属性值
PropertyInfo propertyName = con.GetType().GetProperty("Name"); //获取指定名称的属性
string valueName = propertyName.GetValue(con, null).ToString(); //获取属性值
dt2.Rows.Add(con.Text, con.Name);
} }
}
private void AddControlInList(string fullName)
{
var obj = Assembly.Load("erp").CreateInstance(fullName);
if (obj == null) return;
foreach (Control con in ((Control)obj).Controls)
{
Type type = con.GetType();
if(type.FullName == "System.Windows.Forms.TabControl")
{
foreach(TabPage page in ((TabControl)con).TabPages)
{
AddTabControl(page);
}
}
switch (type.BaseType.Name)
{
case "ToolStrip":
string str = con.GetType().GetProperty("Text").GetValue(con, null).ToString();
string strname = con.GetType().GetProperty("Name").GetValue(con, null).ToString();
if (((ToolStrip)con).Items.Count > )
AddMenuInList((ToolStrip)con);
else
{
dt2.Rows.Add(con.Text, con.Name);
}
break;
default:
string str1 = con.GetType().GetProperty("Text").GetValue(con, null).ToString();
string strname1 = con.GetType().GetProperty("Name").GetValue(con, null).ToString();
if (((Control)con).Controls.Count > )
{
AddControlInList(type.FullName);
}
else
{
PropertyInfo propertyText = con.GetType().GetProperty("Text"); //获取指定名称的属性
string valueText = propertyText.GetValue(con, null).ToString(); //获取属性值
PropertyInfo propertyName = con.GetType().GetProperty("Name"); //获取指定名称的属性
string valueName = propertyName.GetValue(con, null).ToString(); //获取属性值
dt2.Rows.Add(con.Text, con.Name);
} break;
}
} } private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dt2.Rows.Clear();
if (e.RowIndex > -)
{
string fillName = dataGridView1["FullName", e.RowIndex].Value.ToString();
this.Cursor = Cursors.WaitCursor;
AddControlInList(fillName);
dataGridView2.SetDataSource(dt2);
this.Cursor = Cursors.Arrow;
}
} private void toolStripButton1_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null && dataGridView2.CurrentRow != null)
{
this.DialogResult = DialogResult.OK;
SelectFormFullName = dataGridView1["FullName", dataGridView1.CurrentRow.Index].Value.ToString();
SelectControlName = dataGridView2["Name", dataGridView2.CurrentRow.Index].Value.ToString();
}
else
{
MessageBox.Show("没有选择有效的控件!!!");
}
} private void AddMenuInList(object obj)
{
ToolStrip ts = obj as ToolStrip;
if (ts != null)
{
foreach (object con in ts.Items)
{
ToolStripDropDown tdd = con as ToolStripDropDown;
if (tdd != null)
{
dt2.Rows.Add(tdd.Text , tdd.Name);
if (tdd.Items.Count > )
AddMenuInList(tdd);
}
ToolStripMenuItem tsm = con as ToolStripMenuItem;
if (tsm != null)
{
dt2.Rows.Add(tsm.Text , tsm.Name); if (tsm.DropDownItems.Count > )
AddMenuInList(tsm);
}
}
}
ToolStripMenuItem ts1 = obj as ToolStripMenuItem;
if (ts1 != null)
{
foreach (object con in ts1.DropDownItems)
{
ToolStripDropDown tdd = con as ToolStripDropDown;
if (tdd != null)
{
dt2.Rows.Add(tdd.Text, tdd.Name); if (tdd.Items.Count > )
AddMenuInList(tdd);
}
ToolStripMenuItem tsm = con as ToolStripMenuItem;
if (tsm != null)
{
dt2.Rows.Add(tsm.Text, tsm.Name); if (tsm.DropDownItems.Count > )
AddMenuInList(tsm);
}
}
}
} }

五.写个窗体选择用户

六,可扩展用户角色,更灵活.

WinForm窗体权限控制的简单实现的更多相关文章

  1. shiro权限控制的简单实现

    权限控制常用的有shiro.spring security,两者相比较,各有优缺点,此篇文章以shiro为例,实现系统的权限控制. 一.数据库的设计 简单的五张表,用户.角色.权限及关联表: CREA ...

  2. ASP.NET MVC中权限控制的简单实现

    1.重写AuthorizeAttribute类,用自己的权限控制逻辑重写AuthorizeCore方法 public class MyAuthorizeAttribute : AuthorizeAtt ...

  3. Kibana访问权限控制

    ELK平台搭建完成后,由于Kibana的服务也是暴露在外网,且默认是没有访问限制的(外部所有人都可以访问到),这明显不是我们想要的,所以我们需要利用Nginx接管所有Kibana请求,通过Nginx配 ...

  4. SpringCloud微服务实战——搭建企业级开发框架(二十八):扩展MybatisPlus插件DataPermissionInterceptor实现数据权限控制

    一套完整的系统权限需要支持功能权限和数据权限,前面介绍了系统通过RBAC的权限模型来实现功能的权限控制,这里我们来介绍,通过扩展Mybatis-Plus的插件DataPermissionInterce ...

  5. Winform开发框架之字段权限控制

    在我的很多Winform开发项目中(包括混合框架的项目),统一采用了权限管理模块来进行各种权限的控制,包括常规的功能权限(按钮.菜单权限).数据权限(记录的权限),另外还可以进行字段级别的字段权限控制 ...

  6. WinForm下窗体权限设计

    权限设计   笔者不才看了园子里面很多园友写关于权限设计这块内容,那么笔者也在添一笔.这个是笔者在上完软件工程课程后,上交的一篇笔者论文,这里分享给大家交流,当然笔者经验尚浅,若内容有误,请大家指点出 ...

  7. C#winform菜单权限分配,与菜单同步的treeView树状菜单权限控制使用心得

    在网上查了很多,发现没有讲述关于--C#winform菜单权限分配,与菜单同步的treeView树状菜单权限控制使用--的资料 自己研究了一个使用方法.下面来看看. 我有两个窗体:LOGINFRM,M ...

  8. 基于SqlSugar的开发框架循序渐进介绍(9)-- 结合Winform控件实现字段的权限控制

    字段的权限控制,一般就是控制对应角色人员对某个业务对象的一些敏感字段的可访问性:包括可见.可编辑性等处理.本篇随笔结合基于SqlSugar的开发框架进行的字段控制管理介绍. 在设计字段权限的时候,我们 ...

  9. WinForm窗体淡入效果界面的简单实现方法

    WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...

随机推荐

  1. PYQT窗口可视化编程

    1.用PYQT的Qt设计师设计完程序UI后,将其转换为UI.py脚本. 转换步骤见帖:http://www.cnblogs.com/doudongchun/p/3694765.html 2.在同目录下 ...

  2. Linux 内核参数 arp_ignore & arp_announce 详解

    arp_ignore定义了对目标地址为本机IP的ARP询问的不同应答模式. arp_announce对网络接口(网卡)上发出的ARP请求包中的源IP地址作出相应的限制:主机会根据这个参数值的不同选择使 ...

  3. puppeteer 填充基础表单

    main.js const pptr = require("puppeteer"); const gotoUrl = "http://127.0.0.1:5500/ind ...

  4. 软件体系架构之ssh框架阅读笔记

    首先我们要了解一下什么是ssh框架? SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. ssh框架系统从职责上分为四层:web层 业 ...

  5. 在U8菜单中增加自定义项

    --.NET实现的脚本 If Exists (Select 1 From UFSystem..UA_Menu where cMenu_id = 'SAM06') delete from UFSyste ...

  6. XAMPP 安装时 MySQL 无法启动,且提示端口占用。

    今天安装XAMPP时遇到了几个坑,忙活了一上午才搞定,写下来分享给同样遇坑的盆友们. MySQL 点击start 提示端口3306被占用,我改了端口号,又改了注册表,将注册表地址改为xampp中mys ...

  7. 壁虎书6 Decision Trees

    Decision Trees are versatile Machine Learning algorithms that can perform both classification and re ...

  8. Spring Cloud 学习记录

    Spring Cloud中文网 拜托!面试不要再问我Spring Cloud底层原理 SpringCloud简介与5大常用组件 Spring Cloud在国内中小型公司能用起来吗?

  9. vue 实现子向父传值

    父组件 <template> <div id="app"> <child @onChange='onChildValue'></child ...

  10. Head First Python-python面向对象

    与大多数其他的编程语言一样,Python容许创建并定义面向对象的类,类可以将代码与代码处理的数据相关联. 对于更加复杂的数据,一般的列表已经不能满足需求了. 我们可以使用字典dict将数据值与键相关联 ...