CS程序中基于菜单和工具栏的权限设计

权限管理几乎是任何系统都有的,有的权限较简单,而有的权限比较复杂,下面的这种方法是控制模块权限的一种常用方法,

用户管理:右边的功能列表是所有子菜单的Text属性,使用的是(CheckedListBox)。

基本方法是这样的:权限表中存储权限(子菜单的Text属性的值),用户表中存储登录系统的用户,
权限表存储用户对应的所有权限(用0和1来表示是否有访问的权限)。

设置权限之后登录的效果图如下所示,没有打勾的选项都变成了灰色不可用状态。

CS程序中基于菜单和工具栏的权限设计_第1张图片


CS程序中基于菜单和工具栏的权限设计_第2张图片  


  主窗体一加载的时候,取出所有子菜单对象:

List < ToolStripMenuItem >  list  =   new  List < ToolStripMenuItem > ();

            
// 取出所有子菜单,添加到集合
             for  ( int  i  =   0 ; i  <   this .menu.Items.Count; i ++ )
            
{
                ToolStripDropDownItem menuItems 
= (ToolStripDropDownItem)menu.Items[i];
                
if (menuItems.HasDropDownItems)
                
{
                    
for (int k = 0; k < menuItems.DropDownItems.Count; k++)
                    
{
                        list.Add((ToolStripMenuItem)menuItems.DropDownItems[k]);
                    }

                }

            }

        }

取出用户对应的权限,和刚才取出的菜单对比,没有权限的就禁用掉。

  // 读取相应的权限
                DataSet ds  =  UR.LaodUsersRight(Share.UserID);

                
if  ( ! DataHelper.IsNullDataSet(ds))
                
{
                    DataTable dt 
= ds.Tables[0];

                    
if (!DataHelper.IsNullDataTable(dt))
                    
{
                        
for (int i = 0; i < dt.Rows.Count; i++)
                        
{
                            
for (int j = 0; j < list.Count; j++)
                            
{
                                
if (dt.Rows[i]["IsOk"].ToString() == "True")
                                
{
                                    
if (dt.Rows[i]["RightName"].ToString() == list[j].Text)
                                    
{
                                        list[j].Enabled 
= true;
                                        
break;
                                    }

                                }

                                
else
                                
{
                                    
if (dt.Rows[i]["RightName"].ToString() == list[j].Text)
                                    
{
                                        list[j].Enabled 
= false;
                                        
break;
                                    }

                                }

                            }

                        }


                        
//遍历所有的工具栏
                        for (int k = 0; k < this.toolStrip1.Items.Count; k++)
                        
{
                            
for (int z = 0; z < dt.Rows.Count; z++)
                            
{
                                
if (dt.Rows[z]["IsOk"].ToString() == "True")
                                
{
                                    
if (toolStrip1.Items[k].Text.Trim() == dt.Rows[z]["RightName"].ToString())
                                    
{
                                        toolStrip1.Items[k].Enabled 
= true;
                                        
break;
                                    }

                                }

                                
else
                                
{
                                    
if (toolStrip1.Items[k].Text.Trim() == dt.Rows[z]["RightName"].ToString())
                                    
{
                                        toolStrip1.Items[k].Enabled 
= false;
                                        
break;
                                    }

                                }

                            }

                        }

                    }

                    
else
                    
{
                        
this.menu.Enabled = false;
                        
this.toolStrip1.Enabled = false;
                    }


                }

                
else
                
{
                    
this.menu.Enabled = false;
                    
this.toolStrip1.Enabled = false;
                }

下面是关于CheckedListBox的部分代码:

    private   void  cklb_Function_ItemCheck( object  sender, ItemCheckEventArgs e)
        
{
            
try
            
{

                
string uid = dgv_Users.SelectedRows[0].Cells["操作员"].Value.ToString();
                
string rid = cklb_Function.SelectedValue.ToString();
                
if (Convert.ToBoolean(e.CurrentValue))
                
{
                    ur.UpdateUserRight(uid, rid, 
false);
                }

                
else
                
{
                    ur.UpdateUserRight(uid, rid, 
true);
                }


            }

            
catch (Exception ex)
            
{
                MsgBox.Error(ex.Message);
            }

        }


全选和全删
  private   void  btn_AllSelect_Click( object  sender, EventArgs e)
        
{
            
for (int i = 0; i < cklb_Function.Items.Count; i++)
            
{
                cklb_Function.SetItemChecked(i, 
true);
            }

        }


        
private   void  btn_AllDel_Click( object  sender, EventArgs e)
        
{
            
for (int i = 0; i < cklb_Function.Items.Count; i++)
            
{
                cklb_Function.SetItemChecked(i, 
false);
            }

        }

你可能感兴趣的:(权限)