⼀个简单的WinForm权限设计
最近帮助朋友写了个⼩软件,涉及到不同⽤户的不同权限设计问题.因为看到很多朋友问如何在WinForm下做权限设置,所以将代码贴出来,供⼤家参考完善.这些代码的特点是设置与运⽤权限不必事先将菜单项⼿⼯存⼊数据库,系统会跟据主窗⼝菜单的变⾃化动保存菜单项到数据库中.
(1)取主窗体菜单项,依据不同⽤户以树形显⽰,并存⼊数据库,代码如下:
public partial class UserRightSet : Form
{
//临时表,⽤于加载选择的⽤户权限
User.UserRightDataTable userRightTempTable = new User.UserRightDataTable();
//当前选择的⽤户编号
Guid _CurrentUserId;
public UserRightSet()
{
InitializeComponent();
}
private void UserRightSet_Load(object sender, EventArgs e)
{
//加载⽤户表
this.userTableAdapter.Fill(this.user._User);
}
//填允⽤户权限树形列表
private void GetMenuStripItemName(ToolStripItemCollection items, TreeNodeCollection treeNodeCollection)
{
foreach (ToolStripItem item in items)
{
TreeNode tempTreeNode = new TreeNode(item.Text);
tempTreeNode.Checked = IsHaveRight(item.Text);
treeNodeCollection.Add(tempTreeNode);
if (item is System.Windows.Forms.ToolStripMenuItem)
{
this.GetMenuStripItemName(((ToolStripMenuItem)item).DropDownItems, tempTreeNode.Nodes);
}
}
}
/
/得到⽤户权限
private bool IsHaveRight(string MenuText)
{
foreach (User.UserRightRow row in user.UserRight)
{
if (row.UserRightMenu == MenuText)
{
if (row.UserHaveRight == true)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
private void buttonSave_Click(object sender, EventArgs e)
{
userRightTempTable.Clear();
GetTreeNode(treeView1.Nodes);
//删除⽤户权限表中的数据
foreach (DataRow dr in user.UserRight)
{
dr.Delete();
}
//合并⽤户权限临时表到⽤户权限表中,并更新数据保存
user.UserRight.Merge(userRightTempTable);
int i = userRightTableAdapter.Update(user.UserRight);
if (i >= 1)
{
MessageBox.Show("设置⽤户权限成功", "操作提⽰");
}
}
private void GetTreeNode(TreeNodeCollection treeNodes)
{
//对⽤户权限树进⾏循环,并加⼊到临时表中
foreach (TreeNode treenode in treeNodes)
{
if (treenode.Nodes.Count >= 1)
{
GetTreeNode(treenode.Nodes);
}
User.UserRightRow userRightRow = (User.UserRightRow) userRightTempTable.NewRow(); userRightRow.UserRightId = Guid.NewGuid();
userRightRow.UserId = _CurrentUserId;
userRightRow.UserRightMenu = treenode.Text;
userRightRow.UserHaveRight = treenode.Checked;
userRightTempTable.Rows.Add(userRightRow);
}
}
private void userDataGridView_SelectionChanged(object sender, EventArgs e)
{
//CurrentUserId =
//显⽰当前⽤户
if (userDataGridView.SelectedRows.Count >= 1)
{
_CurrentUserId =(Guid) userDataGridView.SelectedRows[0].Cells["UserId"].Value;
this.userRightTableAdapter.Fill(user.UserRight, _CurrentUserId);
treeView1.Nodes.Clear();
MenuStrip menuStrip = this.Owner.MainMenuStrip;
GetMenuStripItemName(menuStrip.Items, treeView1.Nodes);
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
(2)主窗体中依据⽤户不同,Enable或者disable菜单项
menustrip和toolstripprivate void Main_Load(object sender, EventArgs e)
{
UserManage.UserLogin userLogin = new Loadometer.UserManage.UserLogin();
userLogin.ShowDialog();
UserManage.SetUserMenu.uStrip1, UserManage.LoginedUserInfo.UserId);
}
/// <summary>
/// 依据⽤户权限设置⽤户菜单
/// </summary>
static class SetUserMenu
{
private static UserTableAdapters.UserRightTableAdapter _UserRightTableAdapter = new
Loadometer.UserManage.UserTableAdapters.UserRightTableAdapter();
private static User.UserRightDataTable _UserRightDataTable = new User.UserRightDataTable();
public static void SetUserMenuRight(MenuStrip menuStrip,Guid UserId)
{
_UserRightTableAdapter.Fill(_UserRightDataTable, UserId);
GetMenuStripItemName(menuStrip.Items);
}
//递归每个菜单项
static private void GetMenuStripItemName(ToolStripItemCollection items)
{
foreach (ToolStripItem item in items)
{
if (item is System.Windows.Forms.ToolStripMenuItem)
{
GetMenuStripItemName(((ToolStripMenuItem)item).DropDownItems);
}
item.Enabled = IsHaveRight(item.Text);
}
}
//得到⽤户权限
static private bool IsHaveRight(string MenuText)
{
foreach (User.UserRightRow row in _UserRightDataTable)
{
if (row.UserRightMenu == MenuText)
{
if (row.UserHaveRight == true)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
}
代码写得很垃圾,但是在⼩的软件中,可以很⽅便的进⾏权限管理,具有⼀定的通⽤性
另外有个问题,如果主窗体中有toolstript,那么这种设置对toolstript是不起作⽤的,如何将toolstript集成到⾥⾯呢?如果您有好的想法,赐教.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论