欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-31 18:46:04
序:在功能性比较强大的后台管理网站处于各种角度考虑多有应用权限管理功能。以公司内部管理系统为例,管理员根据不同员工所在不同部门赋予其不同权限,或者根据上下级隶属关系实现“金...

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

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

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

(一)储存
这里的权限功能(类别)名称和权限模块(具体)名称均是手动输入数据库,不可增删改查。本例将某权限模块作为最基本元素,是否具备该权限用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;//返回象征权限的二进制字符串
         }