程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .NET之後台用戶權限管理實現

.NET之後台用戶權限管理實現

編輯:ASP.NET基礎

序:在功能性比較強大的後台管理網站處於各種角度考慮多有應用權限管理功能。以公司內部管理系統為例,管理員根據不同員工所在不同部門賦予其不同權限,或者根據上下級隸屬關系實現“金字塔”管理。本文內容有不盡不實之處懇請指正。

正文:
如下效果圖是否令某用戶具備相應權限用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;//返回象征權限的二進制字符串
         }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved