Preface: In the functional more powerful background management website is in various angles to consider many has the application authority management function. Take the company internal management system as an example, the administrator according to different employees in different departments to give them different permissions, or according to subordinate relationship to achieve "pyramid" management. The contents of this article are not in the truth.
Body:
The following effect diagram makes a user have the appropriate permissions to distinguish between storage using a CheckBox state.
(i) Storage
Here the permission function (category) Name and Permissions module (specific) name are manually entered the database, can not be deleted and checked. This example takes a permission module as the most basic element and whether the permission is represented by 1 or 0, and then stores all of the current user's permission links into a binary string in the database. Such as example code 1.1
(ii) reads like example code 1.2
(c) The front desk code is as follows
Copy Code code as follows:
Use JS to add commas and vertical bars, passing values through 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>
Copy Code code as follows:
Example code 1.2
<summary>
Bind all module categories
</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 ()//Get generic collection
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)
{
Modify
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
{
New
Bindallfunction (Moduleidstr, SB, "");
}
}
<summary>
To bind individual feature options in each module
</summary>
<param name= "Moduleidstr" > Module number </param>
<param name= "SB" >html form </param>
<param name= "roleactions" > Permission binary string </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 ()//function table generic Collection
Moduleidstr = Moduleidstr. TrimEnd (', ');//comma interval function module number
string[] str = moduleidstr. Split (', ');//Remove comma, Save as Array
for (int i = 0; i < str. Length; i++)//loop array
{
StringBuilder FUNSB = new StringBuilder ();
for (int j = 0; J < list.) Count; J + +)
{
if (list[j). Moduleno = = Convert.ToInt32 (Str[i])
{
if (roleactions = "")
{
New
Funsb. Append ("<input id=\" "+ list[j]." Funcno.tostring () + "\" type=\ "checkbox\" checked=\ "checked\" "value=\" "+ list[j". Funcno.tostring () + "\/>");
Funsb. Append (List[j]. FuncName + "");
}
Else
{
Modify
if (Roleactions.length >= list[j]. FUNCNO)
{
FUNCNO represents the current permission at the binary character index value
if (roleactions.substring (LIST[J). FuncNo-1, 1) = = "1")//Intercept the permission binary string and judge its status
{
Checked status
Funsb. Append ("<input id=\" "+ list[j]." Funcno.tostring () + "\" type=\ "checkbox\" checked=\ "checked\" "value=\" "+ list[j". Funcno.tostring () + "\/>");
Funsb. Append (List[j]. FuncName + "");
}
Else
{
Non-selected state
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 + "");
}
}
}
}
Replace feature items
Sb. Replace ("{" + Str[i] + "}", Funsb. ToString ());
}
Literalrole.text = sb. ToString ();
}
Save (update) event
protected void Submit_click (object sender, EventArgs e)
{
String cbstr = hidden1.value;//Get foreground hidden value, such as "1,true|2,true|3,false|4,true" form
........
}
Copy Code code as follows:
Example Code 1.1
<summary>
To generate a role binary string
</summary>
<param name= "str" ></param>
<returns> binary Sequence </returns>
private string Generateroleaction (String str)
{
Virtual tables build two-column storage permission sequence number and its status
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 (' | '); /intercept any two binary elements between vertical bars
for (int i = 0; i < Strarray. Length; i++)
{
DataRow dr = dt. NewRow ();
Dr[0] = Strarray[i]. Split (', ') [0];//permission sequence number
DR[1] = Strarray[i]. Split (', ') [1];//permission status, using True and false to distinguish records
Dt. Rows.Add (DR);
}
Dt. Defaultview.sort = "Funno ASC";
DataTable dttemp = dt. Defaultview.totable ();
Converts a status column to a binary character
string s = "";
for (int j = 0; J < Dttemp. Rows.Count; J + +)
{
if (dttemp. rows[j]["Flag"]. ToString (). ToLower () = = "true")
{
s = = "1";/= Selected (True, with this permission) expressed in 1
}
Else
{
s + = "0";//unchecked (false, that does not have the permission) is represented by 0
}
}
Return s;//a binary string that returns a symbolic permission
}