简单的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菜单项
      private void Main_Load(object sender, EventArgs e)
        {   
            UserManage.UserLogin userLogin = new Loadometer.UserManage.UserLogin();
            userLogin.ShowDialog();
            UserManage.SetUserMenu.SetUserMenuRight(this.menuStrip1, 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;
        }
    }
 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Joetao/archive/2009/05/26/4216077.aspx

你可能感兴趣的:(WinForm)