.NET之后台用户权限管理实现

 更新时间:2013年02月22日 16:46:24   作者:   我要评论

在功能性比较强大的后台管理网站处于各种角度考虑多有应用权限管理功能。以公司内部管理系统为例,管理员根据不同员工所在不同部门赋予其不同权限,或者根据上下级隶属关系实现“金字塔”管理。本文内容有不尽不实之处恳请指正。

序:在功能性比较强大的后台管理网站处于各种角度考虑多有应用权限管理功能。以公司内部管理系统为例,管理员根据不同员工所在不同部门赋予其不同权限,或者根据上下级隶属关系实现“金字塔”管理。本文内容有不尽不实之处恳请指正。

正文:
如下效果图是否令某用户具备相应权限用checkbox状态区别存储。

(一)储存
这里的权限功能(类别)名称和权限模块(具体)名称均是手动输入金沙国际官网,不可增删改查。本例将某权限模块作为最基本元素,是否具备该权限用1或0表示,进而将当前用户所有权限链接组成二进制字符串储存在金沙国际官网中。如示例代码1.1

(二)读取 如示例代码1.2


(三)前台代码 如下

复制代码 代码如下:

//利用js添加逗号和竖线 ,通过hidden传值

<script language="javascript" type="text/javascript">

function getcheck() {
var inputs = document.getElementById("cbpanel");
var chks = inputs.getElementsByTagName("input");
var str = "";
for (var i = 0; i < chks.length; i++) {
if (chks[i].type == 'checkbox') {
str += chks[i].value + "," + chks[i].checked + "|";
}
}
document.getElementById("Hidden1").value = str;
}

<script>

<body>

<div id="cbpanel"> <asp:Literal ID="LiteralRole" runat="server"></asp:Literal>

<input id="Hidden1" runat="server" type="hidden" /></div>

</body>


复制代码 代码如下:

示例代码1.2
 /// <summary>
         /// 绑定所有模块分类
         /// </summary>
         private void BindAllModule()
         {
             StringBuilder sb = new StringBuilder();
             sb.Append("<table class=\"AddRole\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
             Bll_User_Module mbll = new Bll_User_Module();
             IList<User_Model.Model_User_Module> list = mbll.GetAllList();//得到泛型集合
              string moduleidstr = "";
              for (int i = 0; i < list.Count; i++)
              {
                  sb.Append("<tr><th style=\"width: 100px; height: 36px;\">");
                  sb.Append(list[i].ModuleName);
                  sb.Append("</th>");
                  sb.Append("<td>");
                  sb.Append("{" + list[i].ID + "}");
                  sb.Append("</td></tr>");
                  moduleidstr += list[i].ID.ToString() + ",";
              }
              sb.Append("</table>");

              if (Request.QueryString["nid"] != null)
              {
                  //修改
                  BLL_User_Role mrbll = new BLL_User_Role();

                  User_Model.Model_User_Role mrmodel = mrbll.GetModel(Convert.ToInt32(Request.QueryString["nid"]));
                  string RoleActions = mrmodel.RoleAction;
                  BindAllFunction(moduleidstr, sb, RoleActions);
              }
              else
              {
                  //新增
                  BindAllFunction(moduleidstr, sb, "");
              }
         }
         /// <summary>
         /// 绑定每个模块中的各个功能选项
         /// </summary>
         /// <param name="moduleidstr">模块数</param>
         /// <param name="sb">html表格</param>
         /// <param name="RoleActions">权限二进制字符串</param>
         private void BindAllFunction(string moduleidstr, StringBuilder sb, string RoleActions)
         {
             Bll_User_Function bll = new Bll_User_Function();
             IList<User_Model.Model_User_Function> list = bll.GetAllList();//功能表泛型集合
             moduleidstr = moduleidstr.TrimEnd(',');//用逗号间隔功能模块数
             string[] str = moduleidstr.Split(',');//移除逗号,以数组形式保存
             for (int i = 0; i < str.Length; i++)//循环数组
             {
                 StringBuilder funsb = new StringBuilder();
                 for (int j = 0; j < list.Count; j++)
                 {
                     if (list[j].ModuleNo == Convert.ToInt32(str[i]))
                     {
                         if (RoleActions == "")
                         {
                             //新增
                             funsb.Append("<input id=\"" + list[j].FuncNo.ToString() + "\" type=\"checkbox\" checked=\"checked\" value=\"" + list[j].FuncNo.ToString() + "\" />");
                             funsb.Append(list[j].FuncName + "  ");
                         }
                         else
                         {
                             //修改
                             if (RoleActions.Length >= list[j].FuncNo)
                             {
                                 //FuncNO代表当前权限位于二进制字符索引值
                                 if (RoleActions.Substring(list[j].FuncNo - 1, 1) == "1")//截取权限二进制字符串并判断其状态
                                 {
                                     //选中状态
                                     funsb.Append("<input id=\"" + list[j].FuncNo.ToString() + "\" type=\"checkbox\" checked=\"checked\" value=\"" + list[j].FuncNo.ToString() + "\" />");
                                     funsb.Append(list[j].FuncName + "  ");
                                 }
                                 else
                                 {
                                     //非选中状态
                                     funsb.Append("<input id=\"" + list[j].FuncNo.ToString() + "\" type=\"checkbox\" value=\"" + list[j].FuncNo.ToString() + "\" />");
                                     funsb.Append(list[j].FuncName + "  ");
                                 }
                             }
                             else
                             {
                                 funsb.Append("<input id=\"" + list[j].FuncNo.ToString() + "\" type=\"checkbox\" value=\"" + list[j].FuncNo.ToString() + "\" />");
                                 funsb.Append(list[j].FuncName + "  ");
                             }
                         }
                     }
                 }
                 //替换功能项
                 sb.Replace("{" + str[i] + "}", funsb.ToString());
             }
             LiteralRole.Text = sb.ToString();     
         }

 // 保存(更新)事件
 protected void Submit_Click(object sender, EventArgs e)
 {
 string cbstr = Hidden1.Value;//获取前台hidden传值,如 “1,true|2,true|3,false|4,true”形式

 ........

 }

复制代码 代码如下:

示例代码1.1
 /// <summary>
         /// 生成角色二进制字符串
         /// </summary>
         /// <param name="str"></param>
         /// <returns>二进制序列</returns>
         private string GenerateRoleAction(string str)
         {
            //虚拟表构建两列存储权限序列号及其状态
             DataTable dt = new DataTable();
             DataColumn col1 = new DataColumn("funno", typeof(int));
             dt.Columns.Add(col1);
             DataColumn col2 = new DataColumn("flag");
             dt.Columns.Add(col2);

             string[] strarray = str.TrimEnd('|').Split('|');//截取任意两个二进制元素之间竖线
             for (int i = 0; i < strarray.Length; i++)
             {
                 DataRow dr = dt.NewRow();
                 dr[0] = strarray[i].Split(',')[0];//权限序列号
                 dr[1] = strarray[i].Split(',')[1];//权限状态,用true和false区别记录
                 dt.Rows.Add(dr);
             }

             dt.DefaultView.Sort = "funno asc";
             DataTable dttemp = dt.DefaultView.ToTable();
             //将状态列转换为二进制字符
             string s = "";
             for (int j = 0; j < dttemp.Rows.Count; j++)
             {
                 if (dttemp.Rows[j]["flag"].ToString().ToLower() == "true")
                 {
                     s += "1";//选中状态(true,即具备该权限)用1表示
                 }
                 else
                 {
                     s += "0";//非选中状态(false,即不具备该权限)用0表示
                 }
             }
             return s;//返回象征权限的二进制字符串
         }

相关文章

最新评论