一个简单的权限分配设计

  效果:

         

数据库表的设计:

                   

  1   //绑定父节点
  2         private void loadTreeList()
  3         {
  4             string strLoginName = LoginID;
  5             Right_treeView.Nodes.Clear();
  6             DataTable dt = foun.GetAllList().Tables[0];
  7             DataTable dtName = auth.GetList(" User_ID='" + strLoginName+"'").Tables[0];
  8             for (int i = 0; i < dt.Rows.Count; i++)
  9             {
 10                 TreeNode node = new TreeNode();
 11                 node.Tag = dt.Rows[i]["Foundation_ID"].ToString();
 12                 node.Text = dt.Rows[i]["Foundation_Name"].ToString();
 13                 Right_treeView.Nodes.Add(node);
 14 
 15                 for (int j = 0; j < dtName.Rows.Count; j++)
 16                 {
 17                     string sTag = node.Tag.ToString();
 18                     if (sTag == dtName.Rows[j]["User_ID"].ToString())  //查看是否有选择
 19                     {
 20                         node.Checked = true;
 21                     }
 22                 }
 23 
 24                 InitTree(Right_treeView.Nodes[i].Nodes, node.Tag.ToString(), strLoginName);
 25             }
 26         }
 27         //绑定子节点
 28         private void InitTree(TreeNodeCollection Nds, String strFid, String strRose)
 29         {
 30             DataTable dt = sysfun.GetList("Foundation_ID=" + strFid).Tables[0];
 31             DataTable dtName = auth.GetList(" User_ID='" + strRose + "'").Tables[0];
 32 
 33             TreeNode tmpNd;
 34             for (int i = 0; i < dt.Rows.Count; i++)
 35             {
 36                 tmpNd = new TreeNode();
 37                 //菜单
 38                 tmpNd.Text = dt.Rows[i]["Sysfunction_Name"].ToString();
 39                 //id
 40                 tmpNd.Tag = dt.Rows[i]["Sysfunction_ID"].ToString();
 41 
 42                 Nds.Add(tmpNd);
 43 
 44                 for (int j = 0; j < dtName.Rows.Count; j++)
 45                 {
 46                     string sTag = tmpNd.Tag.ToString();
 47                     if (sTag == dtName.Rows[j]["Sysfunction_ID"].ToString())
 48                     {
 49                         tmpNd.Checked = true;
 50                     }
 51 
 52                 }
 53             }
 54         }
 55 
 56         private void Right_treeView_AfterCheck(object sender, TreeViewEventArgs e)
 57         {
 58             if (e.Action != TreeViewAction.Unknown)
 59             {
 60                 CheckAllChildNodes(e.Node, e.Node.Checked);
 61                 //选中父节点 
 62                 bool bol = true;
 63                 if (e.Node.Parent != null)
 64                 {
 65                     for (int i = 0; i < e.Node.Parent.Nodes.Count; i++)
 66                     {
 67                         if (!e.Node.Parent.Nodes[i].Checked)
 68                             bol = false;
 69                     }
 70                     e.Node.Parent.Checked = bol;
 71                 }
 72             }
 73         }
 74 
 75         public void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
 76         {
 77             foreach (TreeNode node in treeNode.Nodes)
 78             {
 79                 node.Checked = nodeChecked;
 80                 if (node.Nodes.Count > 0)
 81                 {
 82                     this.CheckAllChildNodes(node, nodeChecked);
 83                 }
 84             }
 85         }
 86 
 87         private void Right_treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
 88         {
 89             Right_treeView.SelectedNode = e.Node;
 90             if (e.Node.Bounds.Contains(e.Location))
 91             {
 92                 if (!e.Node.IsExpanded)
 93                 {
 94                     e.Node.Expand();
 95                 }
 96                 else
 97                 {
 98                     e.Node.Collapse();
 99                 }
100             }
101         }
102 
103         private void CheckChild(TreeNode node)
104         {
105             if (node.Nodes.Count != 0)
106             {
107                 DataTable dt = sysfun.GetList("Foundation_ID=" + node.Tag.ToString()).Tables[0]; ;
108                 DataTable dtName = auth.GetList(" User_ID=" + LoginID).Tables[0];
109          
110                 if (node.Parent == null)
111                 {
112                     foreach (TreeNode item in node.Nodes)
113                     {
114                         //问题???
115                         item.Checked = node.Checked;
116                         //CheckChild(item);
117 
118                     }
119                 }
120             }
121         }
122         private void CheckParent(TreeNode node)
123         {
124             if (node.Parent != null)
125             {
126                 int count = 0;
127                 foreach (TreeNode item in node.Parent.Nodes)
128                 {
129                     if (item.Checked)
130                     {
131                         count++;
132                     }
133                 }
134 
135                 node.Parent.Checked = (count == node.Parent.Nodes.Count);
136                 CheckParent(node.Parent);
137             }
138         }
139 
140         private void Right_treeView_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
141         {
142             Cursor = Cursors.Hand;
143         }
144 
145         private void Right_treeView_MouseMove(object sender, MouseEventArgs e)
146         {
147             TreeNode node = Right_treeView.GetNodeAt(e.X, e.Y);
148             if (node == null)
149             {
150                 Cursor = Cursors.Default;
151             }
152         }
153 
154         //保存
155         private void button1_Click(object sender, EventArgs e)
156         {
157             List<string> list = new List<string>();
158             string str = Right_treeView.SelectedNode.ToString();
159             foreach (TreeNode tnode in this.Right_treeView.Nodes)
160             {
161                 if (tnode.Checked)
162                 {
163                     list.Add(tnode.Tag.ToString());
164                 }
165                 foreach (TreeNode trnode in tnode.Nodes)
166                 {
167                     if (trnode.Checked)
168                     {
169                         list.Add(trnode.Tag.ToString());
170                     }
171                 }
172             }
173             if (list.Count == 0)
174             {
175                 MessageBox.Show("请选择权限!");
176                 return;
177             }
178             auth.DeleteforName(LoginID);
179             Authority authModel = new Authority();
180             for (int i = 0; i < list.Count; i++)
181             {
182                 authModel.Sysfunction_ID = int.Parse(list[i]);
183                 authModel.User_ID = LoginID;
184                 auth.Add(authModel);
185             }
186             MessageBox.Show("保存成功!");
187             msg.AddModel(HelpeMess.userID, "员工表导出成功", "员工表导出");
188             this.Close();
189         }
View Code

 

 1 public  class UserRight
 2     {
 3       static AuthorityBLL authbll = new AuthorityBLL();
 4         public static bool CheckAuthority(string userNo, string authorityNum)
 5         {
 6           DataTable  ds= authbll.GetList(string.Format("User_ID='{0}' AND Sysfunction_ID={1}", userNo, authorityNum)).Tables[0];
 7             if (ds!= null)
 8             {
 9                 if (ds.Rows.Count < 1)
10                 {
11                     MessageBox.Show("对不起,你没有此权限!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
12                     return false;
13                 }
14                 else
15                     return true;
16             }
17             else
18             {
19                 MessageBox.Show("对不起你没有此权限!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
20                 return false;
21             }
22         }
23     }
判断权限

 

 

posted @ 2014-05-06 17:08  vaejp  阅读(1124)  评论(0编辑  收藏  举报