Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GroupAccessC.aspx.cs" Inherits="Admin_GroupAccessC" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<link href="../css/sheet.css" type="text/css" rel="stylesheet" />
<style type="text/css">
body{margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; font-size:12px; color:black; font-family:宋体;}
a:link{color:black;}
a:active{color:blue;}
a:hover{color:red;}
a:visited{color:green;}
.btnSave
{
border:1px solid white;
background-color:white;
padding-top:2px;
height:20px;
width:50px;
font-size:12px;
cursor:default;
}
.btnOver
{
border:1px solid #517AB8;
background-color:white;
padding-top:2px;
height:20px;
width:50px;
font-size:12px;
cursor:hand;
}
</style>
<script src="../script/process.js" type="text/javascript"></script>
<script type="text/javascript">
function process()
{
var frm = document.getElementById("form1");
frm.style.display = "none";
var pb=new ProcessBar();
pb.name="请稍后,模块正在加载"; //要显示的文字
pb.mode=6; //进度条样式 (1-11)共11种
var dsv = document.createElement("div");
dsv.id = "divPcs";
// document.body.style.marginTop = "50px";
dsv.innerHTML=pb.GetProcessHtml();
document.body.appendChild(dsv);
window.setTimeout(LoadOK,2000);
}
function LoadOK()
{
var frm = document.getElementById("form1");
frm.style.display = "";
document.getElementById("divPcs").innerHTML='';
// document.body.style.marginTop = "0px";
}
</script>
<script type="text/javascript">
<!--
function SelectModule()
{
var Module = document.getElementById('HiddenField1');
var postid = document.getElementById('NowTitleID');
Module.value = window.showModalDialog('blank.aspx?url=MDSelect.aspx?id='+postid.value, window, 'dialogHeight:400px;dialogWidth:220px;;status:no;scroll:no');
__doPostBack("LinkButton1","");
}
function btnDelete()
{
var flag;
if(form1.HiddenField2.value=="true")//如果有子节点
{
flag=confirm('您选中节点具有子节点\n是否删除该节点及子节点?','删除');
form1.HiddenField3.value=flag;
}
else
{
flag = true;
}
if(!flag)
{
return false;
}
else
{
__doPostBack("LinkButton2","");
}
}
//-->
</script>
</head>
<body onload="process()">
<form id="form1" runat="server">
<div>
<div style="margin-top:0px;">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="btnAdd_Click" OnClientClick="SelectModule()"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="return btnDelete()" OnClick="btneDel_Click"></asp:LinkButton>
<table cellpadding="0" cellspacing="0" style="width:100%; display:none;">
<tr>
<td>
<asp:LinkButton ID="btnSave" onmouseover="this.className='btnOver'" onmouseout="this.className='btnSave'" runat="server" OnClick="btnSave_Click">保存</asp:LinkButton>
</td>
<td align="right">
<asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2">
<ProgressTemplate>
<asp:Label ID="Label1" runat="server" Height="100%" Text="正在更新数据请稍后" Width="100%" Font-Bold="True" Font-Size="15px" ForeColor="Red"></asp:Label>
</ProgressTemplate>
</asp:UpdateProgress>
</td>
</tr>
</table>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<table cellpadding="0" cellspacing="0" style="width:100%;">
<tr class="addressTH">
<th>
成员列表
</th>
<th>
系统模块
</th>
<th>
权限列表
</th>
</tr>
<tr>
<td valign="top" style="width:200px;">
<div id="leftscroll" style="overflow:auto; position:absolute; width:185px; height:445px;">
<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" BorderStyle="None" ExpandDepth="1" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" style="position: relative" ImageSet="Arrows" >
<SelectedNodeStyle BackColor="#99CCFF" Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
</div>
</td>
<td valign="top" style="width:200px;" class="dvTree" align="center">
<table cellpadding="0" cellspacing="0" style="width:100%;">
<tr>
<td style="height:45px;">
<input type="button" value="添加模块" onclick="SelectModule()" />
<input type="button" value="删除模块" onclick="btnDelete()" />
</td>
</tr>
<tr>
<td align="left" style="border-top:1px solid #FFFFFF; padding-top:2px;">
<div class="dvTree" id="Div1" style="overflow:auto; left:200px; position:absolute; width:200px; height:400px;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TreeView ID="TreeView2" runat="server" OnSelectedNodeChanged="TreeView2_SelectedNodeChanged" style="position: relative" ImageSet="Arrows">
<SelectedNodeStyle BackColor="#99CCFF" Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
</td>
<td valign="top" style="padding-left:30px;">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:CheckBoxList style="POSITION: relative" id="CheckBoxList1" runat="server" Height="17px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" AutoPostBack="True" EnableTheming="True"></asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="right" valign="bottom" colspan="3">
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="12px" ForeColor="Red"
Style="position: relative"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
<%--<table style="width:100%; height: 460px; position: relative;" align="center" border="0" cellpadding="1" cellspacing="1" >
<tr>
<td style="width: 250px; height: 25px;" align="center"><strong>成员列表</strong>
</td>
<td style="width: 250px; height: 25px;" align="center">
功能<strong>模块</strong>
</td>
<td style="width: 250px; height: 25px;" align="center"><strong>成员权限</strong>
</td>
</tr>
<tr>
<td valign="top" >
<asp:Panel ID="Panel1" runat="server" Height="422px" Width="100%" BorderStyle="None" BorderWidth="2px" ScrollBars="Both" style="overflow:scroll; position: relative; left: 0px; top: 0px;">
<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" BorderStyle="None" ImageSet="XPFileExplorer"
ShowLines="True" ExpandDepth="15" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" OnTreeNodePopulate="TreeView1_TreeNodePopulate" style="position: relative" >
<Nodes>
<asp:TreeNode Expanded="False" PopulateOnDemand="True" SelectAction="Expand"></asp:TreeNode>
</Nodes>
<SelectedNodeStyle BackColor="#99CCFF" />
</asp:TreeView>
</asp:Panel>
</td>
<td valign="top" >
<asp:TreeView ID="TreeView2" runat="server" OnSelectedNodeChanged="TreeView2_SelectedNodeChanged">
</asp:TreeView>
</td>
<td valign="top" >
<asp:CheckBoxList style="POSITION: relative" id="CheckBoxList1" runat="server" Height="17px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" AutoPostBack="True" EnableTheming="True">
</asp:CheckBoxList></td>
</tr>
</table>--%>
</div>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="NowTitleID" runat="server" />
<asp:HiddenField ID="HiddenField3" runat="server" />
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GroupAccessC.aspx.cs" Inherits="Admin_GroupAccessC" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<link href="../css/sheet.css" type="text/css" rel="stylesheet" />
<style type="text/css">
body{margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; font-size:12px; color:black; font-family:宋体;}
a:link{color:black;}
a:active{color:blue;}
a:hover{color:red;}
a:visited{color:green;}
.btnSave
{
border:1px solid white;
background-color:white;
padding-top:2px;
height:20px;
width:50px;
font-size:12px;
cursor:default;
}
.btnOver
{
border:1px solid #517AB8;
background-color:white;
padding-top:2px;
height:20px;
width:50px;
font-size:12px;
cursor:hand;
}
</style>
<script src="../script/process.js" type="text/javascript"></script>
<script type="text/javascript">
function process()
{
var frm = document.getElementById("form1");
frm.style.display = "none";
var pb=new ProcessBar();
pb.name="请稍后,模块正在加载"; //要显示的文字
pb.mode=6; //进度条样式 (1-11)共11种
var dsv = document.createElement("div");
dsv.id = "divPcs";
// document.body.style.marginTop = "50px";
dsv.innerHTML=pb.GetProcessHtml();
document.body.appendChild(dsv);
window.setTimeout(LoadOK,2000);
}
function LoadOK()
{
var frm = document.getElementById("form1");
frm.style.display = "";
document.getElementById("divPcs").innerHTML='';
// document.body.style.marginTop = "0px";
}
</script>
<script type="text/javascript">
<!--
function SelectModule()
{
var Module = document.getElementById('HiddenField1');
var postid = document.getElementById('NowTitleID');
Module.value = window.showModalDialog('blank.aspx?url=MDSelect.aspx?id='+postid.value, window, 'dialogHeight:400px;dialogWidth:220px;;status:no;scroll:no');
__doPostBack("LinkButton1","");
}
function btnDelete()
{
var flag;
if(form1.HiddenField2.value=="true")//如果有子节点
{
flag=confirm('您选中节点具有子节点\n是否删除该节点及子节点?','删除');
form1.HiddenField3.value=flag;
}
else
{
flag = true;
}
if(!flag)
{
return false;
}
else
{
__doPostBack("LinkButton2","");
}
}
//-->
</script>
</head>
<body onload="process()">
<form id="form1" runat="server">
<div>
<div style="margin-top:0px;">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="btnAdd_Click" OnClientClick="SelectModule()"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="return btnDelete()" OnClick="btneDel_Click"></asp:LinkButton>
<table cellpadding="0" cellspacing="0" style="width:100%; display:none;">
<tr>
<td>
<asp:LinkButton ID="btnSave" onmouseover="this.className='btnOver'" onmouseout="this.className='btnSave'" runat="server" OnClick="btnSave_Click">保存</asp:LinkButton>
</td>
<td align="right">
<asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2">
<ProgressTemplate>
<asp:Label ID="Label1" runat="server" Height="100%" Text="正在更新数据请稍后" Width="100%" Font-Bold="True" Font-Size="15px" ForeColor="Red"></asp:Label>
</ProgressTemplate>
</asp:UpdateProgress>
</td>
</tr>
</table>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<table cellpadding="0" cellspacing="0" style="width:100%;">
<tr class="addressTH">
<th>
成员列表
</th>
<th>
系统模块
</th>
<th>
权限列表
</th>
</tr>
<tr>
<td valign="top" style="width:200px;">
<div id="leftscroll" style="overflow:auto; position:absolute; width:185px; height:445px;">
<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" BorderStyle="None" ExpandDepth="1" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" style="position: relative" ImageSet="Arrows" >
<SelectedNodeStyle BackColor="#99CCFF" Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
</div>
</td>
<td valign="top" style="width:200px;" class="dvTree" align="center">
<table cellpadding="0" cellspacing="0" style="width:100%;">
<tr>
<td style="height:45px;">
<input type="button" value="添加模块" onclick="SelectModule()" />
<input type="button" value="删除模块" onclick="btnDelete()" />
</td>
</tr>
<tr>
<td align="left" style="border-top:1px solid #FFFFFF; padding-top:2px;">
<div class="dvTree" id="Div1" style="overflow:auto; left:200px; position:absolute; width:200px; height:400px;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TreeView ID="TreeView2" runat="server" OnSelectedNodeChanged="TreeView2_SelectedNodeChanged" style="position: relative" ImageSet="Arrows">
<SelectedNodeStyle BackColor="#99CCFF" Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
</td>
<td valign="top" style="padding-left:30px;">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:CheckBoxList style="POSITION: relative" id="CheckBoxList1" runat="server" Height="17px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" AutoPostBack="True" EnableTheming="True"></asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="right" valign="bottom" colspan="3">
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="12px" ForeColor="Red"
Style="position: relative"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
<%--<table style="width:100%; height: 460px; position: relative;" align="center" border="0" cellpadding="1" cellspacing="1" >
<tr>
<td style="width: 250px; height: 25px;" align="center"><strong>成员列表</strong>
</td>
<td style="width: 250px; height: 25px;" align="center">
功能<strong>模块</strong>
</td>
<td style="width: 250px; height: 25px;" align="center"><strong>成员权限</strong>
</td>
</tr>
<tr>
<td valign="top" >
<asp:Panel ID="Panel1" runat="server" Height="422px" Width="100%" BorderStyle="None" BorderWidth="2px" ScrollBars="Both" style="overflow:scroll; position: relative; left: 0px; top: 0px;">
<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" BorderStyle="None" ImageSet="XPFileExplorer"
ShowLines="True" ExpandDepth="15" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" OnTreeNodePopulate="TreeView1_TreeNodePopulate" style="position: relative" >
<Nodes>
<asp:TreeNode Expanded="False" PopulateOnDemand="True" SelectAction="Expand"></asp:TreeNode>
</Nodes>
<SelectedNodeStyle BackColor="#99CCFF" />
</asp:TreeView>
</asp:Panel>
</td>
<td valign="top" >
<asp:TreeView ID="TreeView2" runat="server" OnSelectedNodeChanged="TreeView2_SelectedNodeChanged">
</asp:TreeView>
</td>
<td valign="top" >
<asp:CheckBoxList style="POSITION: relative" id="CheckBoxList1" runat="server" Height="17px" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged" AutoPostBack="True" EnableTheming="True">
</asp:CheckBoxList></td>
</tr>
</table>--%>
</div>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="NowTitleID" runat="server" />
<asp:HiddenField ID="HiddenField3" runat="server" />
</form>
</body>
</html>
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Text;
using System.Collections.Generic;
public partial class Admin_ModuleAccessC : System.Web.UI.Page, ICallbackEventHandler
{
private Hashtable hTable = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
#region 控件初始化
if (!IsPostBack)
{
string url = System.Configuration.ConfigurationManager.AppSettings["VisualUrl"];
int access = ((GroupPrincipal)User).GetAccessByVirtualPath(url + "/Admin/ModuleAccessC.aspx");
if (access < 1)
{
Response.Redirect("~/Error/Error.aspx");
}
this.Isfrist.Value = "0";
this.TreeDepartment.Attributes.Add("OnClick", "Tt()");
this.TreeUser.Attributes.Add("OnClick", "ShowCheckBox()");
this.TreeDepartment.CollapseAll();
this.TreeDepartment.ExpandDepth = 1;
this.HiddenField1.Value = "0";
this.DepartPastValue.Value = "";
this.DepartPastValue1.Value = "";
DataSet gps = Groups.Provider.GetGroupsDataSet();
MembershipUserCollection users = Membership.GetAllUsers();
ViewState.Add("groups", gps);
ViewState.Add("users", users);
TreeNode tn = new TreeNode();
TreeUser.Nodes.Add(tn);
TreeuserBind(tn, "00000000-0000-0000-0000-000000000000", gps, users, new string[] { "" }, "");
tn.SelectAction = TreeNodeSelectAction.Select;
CreateTreeNodes("DefaultSiteMapProvider", false);
//flagsuccess = Page.ClientScript.GetCallbackEventReference(this, "list", "ShowServerParent", "context");
}
#endregion
}
/// <summary>
/// 递归绑定部门树
/// </summary>
/// <param name="tn">树节点</param>
/// <param name="prkey">上层节点id</param>
/// <param name="ds">添加入树的数据</param>
/// <param name="mc">用户数据</param>
private void TreeuserBind(TreeNode tn, string prkey, DataSet ds, MembershipUserCollection mc, string[] rols, string url)
{
ds.Tables[0].DefaultView.RowFilter = "ParentGroupID='" + prkey + "'";
ds.Tables[0].DefaultView.Sort = "GroupIndex ASC";
bool istru = false;
if (ds.Tables[0].DefaultView.Count > 0)
{
foreach (DataRow dv in ds.Tables[0].DefaultView.ToTable().Rows)
{
if (prkey == "00000000-0000-0000-0000-000000000000")
{
tn.Value = dv["GroupID"].ToString();
tn.Text = dv["GroupName"].ToString();
//TreeuserBind(tn, dv["GroupID"].ToString(), ds, mc, rols, url);
}
else
{
TreeNode treenod = new TreeNode();
treenod.Value = dv["GroupID"].ToString();
string tit = getTitle(dv["GroupID"].ToString(), "", rols, true, url, ref istru);
treenod.Text = dv["GroupName"].ToString() + tit;
if (tit.Contains("checked='true'"))
treenod.Checked = true;
treenod.SelectAction = TreeNodeSelectAction.Select;
tn.ChildNodes.Add(treenod);
}
}
}
//---------新添加 添加用户
if (prkey != "00000000-0000-0000-0000-000000000000")
{
foreach (string us in Groups.GetUsersInGroup(Groups.GetGroupById(new Guid(prkey))))
{
TreeNode tuser = new TreeNode();
tuser.SelectAction = TreeNodeSelectAction.None;
string tit = getTitle(prkey, mc[us].ProviderUserKey.ToString(), rols, false, url, ref istru);
tuser.Text = mc[us].Comment + tit;
if (tit.Contains("checked='true'"))
tuser.Checked = true;
tuser.Value = mc[us].ProviderUserKey.ToString();
tn.ChildNodes.Add(tuser);
}
}
//----------------------------
}
/// <summary>
/// 勾选上级部门和下级部门
/// </summary>
/// <param name="tn"></param>
public void getchild(TreeNode tn)
{
foreach (TreeNode tnc in tn.ChildNodes)
{
if (tnc.Checked == true)
{
SetCheck(tnc);
}
getchild(tnc);
}
}
/// <summary>
/// 勾选上级节点
/// </summary>
/// <param name="tn"></param>
public void SetCheck(TreeNode tn)
{
if (tn.Parent != null)
{
tn.Parent.Checked = true;
SetCheck(tn.Parent);
}
}
/// <summary>
/// 根据权限动态创建radio
/// </summary>
/// <param name="gid">组id</param>
/// <param name="uid">用户id</param>
/// <param name="roles">规则数组</param>
/// <param name="isGroup">是否组</param>
/// <param name="url">模块url</param>
/// <param name="istrue">是否checkbox打勾</param>
/// <returns>返回html代码</returns>
private string getTitle(string gid, string uid, string[] roles, bool isGroup, string url, ref bool istrue)
{
if (url == "")
return "";
DataTable dt = GetSign(url);
string title = "";
for (int i = 0; i < roles.Length; i++)
{
if (!string.IsNullOrEmpty(roles[i]))
{
string role = roles[i];
string quanxian = (1 << i).ToString();
//获取需要勾选的部门的id
//获取指定用户所在的群组
//从数据库找到此用户针对此模块的权限,进行填充
//这里的逻辑设计,如果此用户没有此模块的上层权限,则不具备设置此模块的权限,
int acces = 0;
if (!isGroup)//是用户
{
DataRow[] udrs = dt.Select("id='" + uid + "' and isuser='" + true.ToString() + "'");
foreach (DataRow dr in udrs)
{
acces |= int.Parse(dr["access"].ToString());
}
if (acces > 0)//对此模块具有权限
{
if ((acces & (1 << i)) == 0)
{
// false
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + uid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
else
{
//true
int icount = 0;
bool usi = false;
foreach (DataRow dr in udrs)
{
if ((int.Parse(dr["access"].ToString()) & int.Parse(quanxian)) == int.Parse(quanxian))//找到他自己的权限
{
usi = true;
break;
}
icount++;
}
//---------------找他的部门的权限
//---------如果上级组有权限,则他针对此权限禁用
string uen = "";
if (usi)
uen = udrs[icount]["isenable"].ToString();
//<-----------------------------------------
title += "\t\t\t\t\t<span><input checked='true' runat='server' id='" + gid + uid + this.HiddenField1.Value + "' " + uen + " type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = true;
}
}
else//对此模块不具备权限
{
//false
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + uid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
}
else
{
DataRow[] gdrs = dt.Select("id='" + gid + "' and isuser='" + false.ToString() + "'");
foreach (DataRow dr in gdrs)
{
acces |= int.Parse(dr["access"].ToString());
}
if (acces > 0)//对此模块具有权限
{
if ((acces & (1 << i)) == 0) //无权限
{
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
else
{
int icount = 0;
bool gsi = false;
foreach (DataRow dr in gdrs)
{
if ((int.Parse(dr["access"].ToString()) & int.Parse(quanxian)) == int.Parse(quanxian))//找到他自己的权限
{
gsi = true;
break;
}
icount++;
}
//-------------------找上级部门权限
string en = "";
if (gsi)
en = gdrs[icount]["isenable"].ToString();
title += "\t\t\t\t\t<span><input runat='server' checked='true' id='" + gid + this.HiddenField1.Value + "' " + en + " type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = true;
}
}
else
{
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
}
this.HiddenField1.Value = (int.Parse(this.HiddenField1.Value) + 1).ToString();
}
}
this.HiddenField1.Value = "0";
return title;
}
/// <summary>
/// 数取创建部门树需要的数据
/// </summary>
/// <param name="provider"></param>
/// <param name="showRootNode"></param>
private void CreateTreeNodes(string provider, bool showRootNode)
{
SiteMapDataSource ds = new SiteMapDataSource();
ds.SiteMapProvider = provider;
ds.DataBind();
SiteMapNode node = ds.Provider.RootNode;
if (showRootNode)
{
AppendNode(TreeDepartment.Nodes, node);
}
else
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
AppendNode(TreeDepartment.Nodes, node.ChildNodes[i]);
}
}
}
/// <summary>
/// 添加部门树节点
/// </summary>
/// <param name="nodes"></param>
/// <param name="node"></param>
private void AppendNode(TreeNodeCollection nodes, SiteMapNode node)
{
TreeNode childNode = new TreeNode(node.Title);
StringBuilder sb = new StringBuilder();
if (node["rolesList"] != null)
{
if (node.Roles != null && node.Roles.Count > 0)
{
foreach (string role in node.Roles)
{
sb.Append(role + ",");
}
string text = sb.ToString();
childNode.Value = text.Substring(0, text.Length - 1);
}
else
{
string temp = node["rolesList"];
if (temp != null)
{
childNode.Value = temp;
}
else
{
childNode.Value = string.Empty;
}
if (childNode.Value == string.Empty)
childNode.SelectAction = TreeNodeSelectAction.None;
}
}
childNode.Target = node.Url;
Groups.AddAccessRecord(childNode.Target, childNode.Text, childNode.Value, null);
nodes.Add(childNode);
foreach (SiteMapNode subNode in node.ChildNodes)
{
AppendNode(childNode.ChildNodes, subNode);
}
}
/// <summary>
/// 查找角色
/// </summary>
/// <param name="provider"></param>
/// <param name="VirtualPath"></param>
/// <returns></returns>
protected string FindRole(string provider, string VirtualPath)
{
SiteMapNode node = Find(provider, VirtualPath);
string text = null;
StringBuilder sb = new StringBuilder();
if (node != null)
{
if (node["roleslist"] != null)
{
text = node["roleslist"];
}
}
return text;
}
/// <summary>
/// 查找结点
/// </summary>
/// <param name="provider"></param>
/// <param name="VirtualPath"></param>
/// <returns></returns>
protected SiteMapNode Find(string provider, string VirtualPath)
{
SiteMapDataSource ds = new SiteMapDataSource();
ds.SiteMapProvider = provider;
ds.DataBind();
VirtualPath = VirtualPath.Trim();
SiteMapProvider nodefind = ds.Provider;
SiteMapNode node = new SiteMapNode(nodefind, VirtualPath);
node = nodefind.FindSiteMapNode(VirtualPath);
return node;
}
/// <summary>
/// 将选中节点从前台传回,保存
/// </summary>
/// <param name="sValue"></param>
private void SaveSetting(string sValue)
{
//if (Isfrist.Value == "1")
//{
try
{
string[] allValues = sValue.Split("*".ToCharArray());
string[] dataValues = allValues[0].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string val in dataValues)
{
bool Isuser = false;
if (val.Length > 70)//是用户
{
Isuser = true;
}
int access = 0;
if (Isuser)
{
access = Groups.GetAccessByMemberId(new Guid(val.Substring(36, 36)), TreeDepartment.SelectedNode.Target);
}
else
{
access = Groups.GetAccessByMemberId(new Guid(val.Substring(0, 36)), TreeDepartment.SelectedNode.Target);
}
if (bool.Parse(allValues[1]))
{
if (Isuser)
{
access |= int.Parse(val.Substring(73, val.Length - 73));
}
else
{
access |= int.Parse(val.Substring(37, val.Length - 37));
}
}
else
{
if (access != 0)
{
if (Isuser)
{
access = access & (access ^ int.Parse(val.Substring(73, val.Length - 73)));
}
else
{
access = access & (access ^ int.Parse(val.Substring(37, val.Length - 37)));
}
}
}
if (Isuser)
{
//Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
Groups.AddMembersInAccessRecord(new Guid(val.Substring(36, 36)), TreeDepartment.SelectedNode.Target, access);
}
else
{
if(!bool.Parse(allValues[1]))
DelChildAccess(val.Substring(0, 36), TreeDepartment.SelectedNode.Target, int.Parse(val.Substring(37, val.Length - 37)));
//Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
Groups.AddMembersInAccessRecord(new Guid(val.Substring(0, 36)), TreeDepartment.SelectedNode.Target, access);
}
}
flagsuccess = "1";
if (dataValues.Length == 0)
flagsuccess = "3";
}
catch
{
flagsuccess = "0";
}
//access +=
//已经将数据传回
//Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target);
//Groups.GetAccessByMemberId(new Guid(dataValues[0].Substring(0, 36)), TreeDepartment.SelectedNode.Target);
//string[] dataValue = new string[dataValues.Length - 1];
//for (int j = 0; j < dataValue.Length; j++)
//{
// dataValue[j] = dataValues[j];
//}
//bool sign = true;
//if (dataValue[0] != null && dataValue[0].Length != 0)
//{
// ArrayList done = new ArrayList();
// foreach (string val in dataValue)
// {
// bool dosign = false;
// foreach (object donestring in done)
// {
// if (val == donestring.ToString())
// {
// dosign = true;
// break;
// }
// }
// if (!dosign)
// {
// if (val.Length != 0)
// {
// bool isUser = val.Length > 70 ? true : false;
// if (sign)
// {
// Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
// sign = false;
// }
// int access = 0;
// Guid mid = new Guid(isUser ? val.Substring(36, 36) : val.Substring(0, 36));
// foreach (string dv in dataValue)
// {
// if (val.Length == dv.Length)
// {
// if (isUser)
// {
// if (val.Substring(0, 72) == dv.Substring(0, 72))
// {
// done.Add(dv);
// access += int.Parse(dv.Substring(72, dv.Length - 72));
// }
// }
// else
// {
// if (val.Substring(0, 36) == dv.Substring(0, 36))
// {
// done.Add(dv);
// access += int.Parse(dv.Substring(36, dv.Length - 36));
// }
// }
// }
// }
// Groups.AddMembersInAccessRecord(mid, TreeDepartment.SelectedNode.Target, access);
// }
// else
// {
// flagsuccess = "0";
// }
//}
// }
//}
//else
//{
// flagsuccess = "2";
//}
//Isfrist.Value = "0";
//}
}
/// <summary>
/// 删除其下所有权限
/// </summary>
/// <param name="pID">父id</param>
/// <param name="url">模块url</param>
/// <param name="aval">权限值</param>
void DelChildAccess(string pID, string url, int aval)
{
int access = Groups.GetAccessByMemberId(new Guid(pID), url);
if (access != 0)
{
access = access & (access ^ aval);
Groups.AddMembersInAccessRecord(new Guid(pID), url, access);
}
foreach (string user in Groups.GetUsersInGroup(new Guid(pID)))
{
access = Groups.GetAccessByMemberId(new Guid(Membership.GetUser(user).ProviderUserKey.ToString()), url);
if (access != 0)
{
access = access & (access ^ aval);
Groups.AddMembersInAccessRecord(new Guid(Membership.GetUser(user).ProviderUserKey.ToString()), url, access);
}
}
GroupCollection childgroup = Groups.GetSubgroups(Groups.GetGroupById(new Guid(pID)));
foreach (Group g in childgroup)
{
DelChildAccess(g.GroupID.ToString(), url, aval);
}
}
/// <summary>
/// 部门树选择改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeDepartment_SelectedNodeChanged(object sender, EventArgs e)
{
TreeUser.Nodes.Clear();
DataSet ds = (DataSet)ViewState["groups"];
MembershipUserCollection mc = (MembershipUserCollection)ViewState["users"];
string[] rs = TreeDepartment.SelectedNode.Value.Split(",".ToCharArray());
TreeNode tn = new TreeNode();
TreeUser.Nodes.Add(tn);
TreeuserBind(tn, "00000000-0000-0000-0000-000000000000", ds, mc, rs, TreeDepartment.SelectedNode.Target);
tn.SelectAction = TreeNodeSelectAction.Expand;
TreeuserBind(tn, tn.Value, ds, mc, rs, TreeDepartment.SelectedNode.Target);
//getchild(tn);
tn.Expand();
this.Label2.Text = "";
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "checkboxn0enable", "<script type='text/javascript'>enablecheckbox();</script>");
}
//------------------------------------------------------------
/// <summary>
/// 传入url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
DataTable GetSign(string url)
{
//id,传入id,如果此id针对模块有权限,而此id的部门拥有此权限,则enable和check=true
Dictionary<string, int> mem = new Dictionary<string, int>();
ArrayList gal = Groups.GetMembersInAccess(url);
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("isuser");
dt.Columns.Add("access");
dt.Columns.Add("isenable");
DataRow dr;
for (int i = 0; i < gal.Count; i++)
{
if (Membership.GetUser(((Member)gal[i]).MemberID) != null)//是用户
{
dr = dt.NewRow();
dr[0] = ((Member)gal[i]).MemberID.ToString();
dr[1] = true.ToString();
dr[2] = ((Member)gal[i]).AccessValue.ToString();
dr[3] = "";
dt.Rows.Add(dr);
}
else
{
ArrayList child = new ArrayList();
getchilds(child, ((Member)gal[i]).MemberID.ToString());//得到其下所有数据
int access = Groups.GetAccessByMemberId(((Member)gal[i]).MemberID, url);
for (int ii = 0; ii < child.Count; ii++)
{
dr = dt.NewRow();
dr[0] = child[ii].ToString();
if (Membership.GetUser(new Guid(child[ii].ToString())) != null)
{
dr[1] = true.ToString();
}
else
{
dr[1] = false.ToString();
}
dr[2] = access.ToString();
if (ii == 0)
{
dr[3] = "";
}
else
{
dr[3] = "disabled = ' true'";
}
dt.Rows.Add(dr);
}
}
}//出来的就是所有用户
return dt;
}
/// <summary>
/// 得到al中的所有子节点
/// </summary>
/// <param name="al"></param>
/// <param name="id"></param>
void getchilds(ArrayList al, string id)
{
al.Add(id);
foreach (string i in Groups.GetUsersInGroup(new Guid(id)))
{
al.Add(Membership.GetUser(i).ProviderUserKey.ToString());
}
foreach (Group ss in Groups.GetSubgroups(Groups.GetGroupById(new Guid(id))))
{
getchilds(al, ss.GroupID.ToString());
}
}
#region ICallbackEventHandler 成员
public string flagsuccess = "";
public string GetCallbackResult()
{
return flagsuccess;
}
public void RaiseCallbackEvent(string eventArgument)
{
this.SaveSetting(eventArgument);
}
#endregion
/// <summary>
/// 部门树点击更改事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeUser_SelectedNodeChanged(object sender, EventArgs e)
{
if (this.TreeDepartment.SelectedNode != null)
{
string sss = this.TreeDepartment.SelectedNode.Text;
this.Label2.Text = "<font color=black>正在对</font> " + sss + "模块 <font color=black>分配用户权限</font>";
}
TreeNode tnnow = TreeUser.SelectedNode;
if (tnnow.Parent != null && TreeDepartment.SelectedNode != null)
{
bool temp = true;
tnnow.Text = Groups.GetGroupById(new Guid(tnnow.Value)).GroupName + getTitle(tnnow.Value, "", TreeDepartment.SelectedNode.Value.Split(",".ToCharArray()), true, TreeDepartment.SelectedNode.Target, ref temp);
}
if (tnnow.SelectAction != TreeNodeSelectAction.Expand)
{
DataSet ds = (DataSet)ViewState["groups"];
MembershipUserCollection mc = (MembershipUserCollection)ViewState["users"];
if (TreeDepartment.SelectedNode != null)
{
string[] rs = TreeDepartment.SelectedNode.Value.Split(",".ToCharArray());
TreeuserBind(tnnow, tnnow.Value, ds, mc, rs, TreeDepartment.SelectedNode.Target);
}
else
{
TreeuserBind(tnnow, tnnow.Value, ds, mc, new string[] { "" }, "");
}
tnnow.Expand();
tnnow.SelectAction = TreeNodeSelectAction.Expand;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Text;
using System.Collections.Generic;
public partial class Admin_ModuleAccessC : System.Web.UI.Page, ICallbackEventHandler
{
private Hashtable hTable = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
#region 控件初始化
if (!IsPostBack)
{
string url = System.Configuration.ConfigurationManager.AppSettings["VisualUrl"];
int access = ((GroupPrincipal)User).GetAccessByVirtualPath(url + "/Admin/ModuleAccessC.aspx");
if (access < 1)
{
Response.Redirect("~/Error/Error.aspx");
}
this.Isfrist.Value = "0";
this.TreeDepartment.Attributes.Add("OnClick", "Tt()");
this.TreeUser.Attributes.Add("OnClick", "ShowCheckBox()");
this.TreeDepartment.CollapseAll();
this.TreeDepartment.ExpandDepth = 1;
this.HiddenField1.Value = "0";
this.DepartPastValue.Value = "";
this.DepartPastValue1.Value = "";
DataSet gps = Groups.Provider.GetGroupsDataSet();
MembershipUserCollection users = Membership.GetAllUsers();
ViewState.Add("groups", gps);
ViewState.Add("users", users);
TreeNode tn = new TreeNode();
TreeUser.Nodes.Add(tn);
TreeuserBind(tn, "00000000-0000-0000-0000-000000000000", gps, users, new string[] { "" }, "");
tn.SelectAction = TreeNodeSelectAction.Select;
CreateTreeNodes("DefaultSiteMapProvider", false);
//flagsuccess = Page.ClientScript.GetCallbackEventReference(this, "list", "ShowServerParent", "context");
}
#endregion
}
/// <summary>
/// 递归绑定部门树
/// </summary>
/// <param name="tn">树节点</param>
/// <param name="prkey">上层节点id</param>
/// <param name="ds">添加入树的数据</param>
/// <param name="mc">用户数据</param>
private void TreeuserBind(TreeNode tn, string prkey, DataSet ds, MembershipUserCollection mc, string[] rols, string url)
{
ds.Tables[0].DefaultView.RowFilter = "ParentGroupID='" + prkey + "'";
ds.Tables[0].DefaultView.Sort = "GroupIndex ASC";
bool istru = false;
if (ds.Tables[0].DefaultView.Count > 0)
{
foreach (DataRow dv in ds.Tables[0].DefaultView.ToTable().Rows)
{
if (prkey == "00000000-0000-0000-0000-000000000000")
{
tn.Value = dv["GroupID"].ToString();
tn.Text = dv["GroupName"].ToString();
//TreeuserBind(tn, dv["GroupID"].ToString(), ds, mc, rols, url);
}
else
{
TreeNode treenod = new TreeNode();
treenod.Value = dv["GroupID"].ToString();
string tit = getTitle(dv["GroupID"].ToString(), "", rols, true, url, ref istru);
treenod.Text = dv["GroupName"].ToString() + tit;
if (tit.Contains("checked='true'"))
treenod.Checked = true;
treenod.SelectAction = TreeNodeSelectAction.Select;
tn.ChildNodes.Add(treenod);
}
}
}
//---------新添加 添加用户
if (prkey != "00000000-0000-0000-0000-000000000000")
{
foreach (string us in Groups.GetUsersInGroup(Groups.GetGroupById(new Guid(prkey))))
{
TreeNode tuser = new TreeNode();
tuser.SelectAction = TreeNodeSelectAction.None;
string tit = getTitle(prkey, mc[us].ProviderUserKey.ToString(), rols, false, url, ref istru);
tuser.Text = mc[us].Comment + tit;
if (tit.Contains("checked='true'"))
tuser.Checked = true;
tuser.Value = mc[us].ProviderUserKey.ToString();
tn.ChildNodes.Add(tuser);
}
}
//----------------------------
}
/// <summary>
/// 勾选上级部门和下级部门
/// </summary>
/// <param name="tn"></param>
public void getchild(TreeNode tn)
{
foreach (TreeNode tnc in tn.ChildNodes)
{
if (tnc.Checked == true)
{
SetCheck(tnc);
}
getchild(tnc);
}
}
/// <summary>
/// 勾选上级节点
/// </summary>
/// <param name="tn"></param>
public void SetCheck(TreeNode tn)
{
if (tn.Parent != null)
{
tn.Parent.Checked = true;
SetCheck(tn.Parent);
}
}
/// <summary>
/// 根据权限动态创建radio
/// </summary>
/// <param name="gid">组id</param>
/// <param name="uid">用户id</param>
/// <param name="roles">规则数组</param>
/// <param name="isGroup">是否组</param>
/// <param name="url">模块url</param>
/// <param name="istrue">是否checkbox打勾</param>
/// <returns>返回html代码</returns>
private string getTitle(string gid, string uid, string[] roles, bool isGroup, string url, ref bool istrue)
{
if (url == "")
return "";
DataTable dt = GetSign(url);
string title = "";
for (int i = 0; i < roles.Length; i++)
{
if (!string.IsNullOrEmpty(roles[i]))
{
string role = roles[i];
string quanxian = (1 << i).ToString();
//获取需要勾选的部门的id
//获取指定用户所在的群组
//从数据库找到此用户针对此模块的权限,进行填充
//这里的逻辑设计,如果此用户没有此模块的上层权限,则不具备设置此模块的权限,
int acces = 0;
if (!isGroup)//是用户
{
DataRow[] udrs = dt.Select("id='" + uid + "' and isuser='" + true.ToString() + "'");
foreach (DataRow dr in udrs)
{
acces |= int.Parse(dr["access"].ToString());
}
if (acces > 0)//对此模块具有权限
{
if ((acces & (1 << i)) == 0)
{
// false
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + uid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
else
{
//true
int icount = 0;
bool usi = false;
foreach (DataRow dr in udrs)
{
if ((int.Parse(dr["access"].ToString()) & int.Parse(quanxian)) == int.Parse(quanxian))//找到他自己的权限
{
usi = true;
break;
}
icount++;
}
//---------------找他的部门的权限
//---------如果上级组有权限,则他针对此权限禁用
string uen = "";
if (usi)
uen = udrs[icount]["isenable"].ToString();
//<-----------------------------------------
title += "\t\t\t\t\t<span><input checked='true' runat='server' id='" + gid + uid + this.HiddenField1.Value + "' " + uen + " type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = true;
}
}
else//对此模块不具备权限
{
//false
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + uid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
}
else
{
DataRow[] gdrs = dt.Select("id='" + gid + "' and isuser='" + false.ToString() + "'");
foreach (DataRow dr in gdrs)
{
acces |= int.Parse(dr["access"].ToString());
}
if (acces > 0)//对此模块具有权限
{
if ((acces & (1 << i)) == 0) //无权限
{
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
else
{
int icount = 0;
bool gsi = false;
foreach (DataRow dr in gdrs)
{
if ((int.Parse(dr["access"].ToString()) & int.Parse(quanxian)) == int.Parse(quanxian))//找到他自己的权限
{
gsi = true;
break;
}
icount++;
}
//-------------------找上级部门权限
string en = "";
if (gsi)
en = gdrs[icount]["isenable"].ToString();
title += "\t\t\t\t\t<span><input runat='server' checked='true' id='" + gid + this.HiddenField1.Value + "' " + en + " type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = true;
}
}
else
{
title += "\t\t\t\t\t<span><input runat='server' id='" + gid + this.HiddenField1.Value + "' type='radio' onclick='CheckedTest(this.id)' value='" + quanxian + "'>" + role + "</span>";
istrue = false;
}
}
this.HiddenField1.Value = (int.Parse(this.HiddenField1.Value) + 1).ToString();
}
}
this.HiddenField1.Value = "0";
return title;
}
/// <summary>
/// 数取创建部门树需要的数据
/// </summary>
/// <param name="provider"></param>
/// <param name="showRootNode"></param>
private void CreateTreeNodes(string provider, bool showRootNode)
{
SiteMapDataSource ds = new SiteMapDataSource();
ds.SiteMapProvider = provider;
ds.DataBind();
SiteMapNode node = ds.Provider.RootNode;
if (showRootNode)
{
AppendNode(TreeDepartment.Nodes, node);
}
else
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
AppendNode(TreeDepartment.Nodes, node.ChildNodes[i]);
}
}
}
/// <summary>
/// 添加部门树节点
/// </summary>
/// <param name="nodes"></param>
/// <param name="node"></param>
private void AppendNode(TreeNodeCollection nodes, SiteMapNode node)
{
TreeNode childNode = new TreeNode(node.Title);
StringBuilder sb = new StringBuilder();
if (node["rolesList"] != null)
{
if (node.Roles != null && node.Roles.Count > 0)
{
foreach (string role in node.Roles)
{
sb.Append(role + ",");
}
string text = sb.ToString();
childNode.Value = text.Substring(0, text.Length - 1);
}
else
{
string temp = node["rolesList"];
if (temp != null)
{
childNode.Value = temp;
}
else
{
childNode.Value = string.Empty;
}
if (childNode.Value == string.Empty)
childNode.SelectAction = TreeNodeSelectAction.None;
}
}
childNode.Target = node.Url;
Groups.AddAccessRecord(childNode.Target, childNode.Text, childNode.Value, null);
nodes.Add(childNode);
foreach (SiteMapNode subNode in node.ChildNodes)
{
AppendNode(childNode.ChildNodes, subNode);
}
}
/// <summary>
/// 查找角色
/// </summary>
/// <param name="provider"></param>
/// <param name="VirtualPath"></param>
/// <returns></returns>
protected string FindRole(string provider, string VirtualPath)
{
SiteMapNode node = Find(provider, VirtualPath);
string text = null;
StringBuilder sb = new StringBuilder();
if (node != null)
{
if (node["roleslist"] != null)
{
text = node["roleslist"];
}
}
return text;
}
/// <summary>
/// 查找结点
/// </summary>
/// <param name="provider"></param>
/// <param name="VirtualPath"></param>
/// <returns></returns>
protected SiteMapNode Find(string provider, string VirtualPath)
{
SiteMapDataSource ds = new SiteMapDataSource();
ds.SiteMapProvider = provider;
ds.DataBind();
VirtualPath = VirtualPath.Trim();
SiteMapProvider nodefind = ds.Provider;
SiteMapNode node = new SiteMapNode(nodefind, VirtualPath);
node = nodefind.FindSiteMapNode(VirtualPath);
return node;
}
/// <summary>
/// 将选中节点从前台传回,保存
/// </summary>
/// <param name="sValue"></param>
private void SaveSetting(string sValue)
{
//if (Isfrist.Value == "1")
//{
try
{
string[] allValues = sValue.Split("*".ToCharArray());
string[] dataValues = allValues[0].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string val in dataValues)
{
bool Isuser = false;
if (val.Length > 70)//是用户
{
Isuser = true;
}
int access = 0;
if (Isuser)
{
access = Groups.GetAccessByMemberId(new Guid(val.Substring(36, 36)), TreeDepartment.SelectedNode.Target);
}
else
{
access = Groups.GetAccessByMemberId(new Guid(val.Substring(0, 36)), TreeDepartment.SelectedNode.Target);
}
if (bool.Parse(allValues[1]))
{
if (Isuser)
{
access |= int.Parse(val.Substring(73, val.Length - 73));
}
else
{
access |= int.Parse(val.Substring(37, val.Length - 37));
}
}
else
{
if (access != 0)
{
if (Isuser)
{
access = access & (access ^ int.Parse(val.Substring(73, val.Length - 73)));
}
else
{
access = access & (access ^ int.Parse(val.Substring(37, val.Length - 37)));
}
}
}
if (Isuser)
{
//Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
Groups.AddMembersInAccessRecord(new Guid(val.Substring(36, 36)), TreeDepartment.SelectedNode.Target, access);
}
else
{
if(!bool.Parse(allValues[1]))
DelChildAccess(val.Substring(0, 36), TreeDepartment.SelectedNode.Target, int.Parse(val.Substring(37, val.Length - 37)));
//Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
Groups.AddMembersInAccessRecord(new Guid(val.Substring(0, 36)), TreeDepartment.SelectedNode.Target, access);
}
}
flagsuccess = "1";
if (dataValues.Length == 0)
flagsuccess = "3";
}
catch
{
flagsuccess = "0";
}
//access +=
//已经将数据传回
//Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target);
//Groups.GetAccessByMemberId(new Guid(dataValues[0].Substring(0, 36)), TreeDepartment.SelectedNode.Target);
//string[] dataValue = new string[dataValues.Length - 1];
//for (int j = 0; j < dataValue.Length; j++)
//{
// dataValue[j] = dataValues[j];
//}
//bool sign = true;
//if (dataValue[0] != null && dataValue[0].Length != 0)
//{
// ArrayList done = new ArrayList();
// foreach (string val in dataValue)
// {
// bool dosign = false;
// foreach (object donestring in done)
// {
// if (val == donestring.ToString())
// {
// dosign = true;
// break;
// }
// }
// if (!dosign)
// {
// if (val.Length != 0)
// {
// bool isUser = val.Length > 70 ? true : false;
// if (sign)
// {
// Groups.RemoveMembersFromAccess(TreeDepartment.SelectedNode.Target, Groups.GetMembersInAccess(TreeDepartment.SelectedNode.Target));
// sign = false;
// }
// int access = 0;
// Guid mid = new Guid(isUser ? val.Substring(36, 36) : val.Substring(0, 36));
// foreach (string dv in dataValue)
// {
// if (val.Length == dv.Length)
// {
// if (isUser)
// {
// if (val.Substring(0, 72) == dv.Substring(0, 72))
// {
// done.Add(dv);
// access += int.Parse(dv.Substring(72, dv.Length - 72));
// }
// }
// else
// {
// if (val.Substring(0, 36) == dv.Substring(0, 36))
// {
// done.Add(dv);
// access += int.Parse(dv.Substring(36, dv.Length - 36));
// }
// }
// }
// }
// Groups.AddMembersInAccessRecord(mid, TreeDepartment.SelectedNode.Target, access);
// }
// else
// {
// flagsuccess = "0";
// }
//}
// }
//}
//else
//{
// flagsuccess = "2";
//}
//Isfrist.Value = "0";
//}
}
/// <summary>
/// 删除其下所有权限
/// </summary>
/// <param name="pID">父id</param>
/// <param name="url">模块url</param>
/// <param name="aval">权限值</param>
void DelChildAccess(string pID, string url, int aval)
{
int access = Groups.GetAccessByMemberId(new Guid(pID), url);
if (access != 0)
{
access = access & (access ^ aval);
Groups.AddMembersInAccessRecord(new Guid(pID), url, access);
}
foreach (string user in Groups.GetUsersInGroup(new Guid(pID)))
{
access = Groups.GetAccessByMemberId(new Guid(Membership.GetUser(user).ProviderUserKey.ToString()), url);
if (access != 0)
{
access = access & (access ^ aval);
Groups.AddMembersInAccessRecord(new Guid(Membership.GetUser(user).ProviderUserKey.ToString()), url, access);
}
}
GroupCollection childgroup = Groups.GetSubgroups(Groups.GetGroupById(new Guid(pID)));
foreach (Group g in childgroup)
{
DelChildAccess(g.GroupID.ToString(), url, aval);
}
}
/// <summary>
/// 部门树选择改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeDepartment_SelectedNodeChanged(object sender, EventArgs e)
{
TreeUser.Nodes.Clear();
DataSet ds = (DataSet)ViewState["groups"];
MembershipUserCollection mc = (MembershipUserCollection)ViewState["users"];
string[] rs = TreeDepartment.SelectedNode.Value.Split(",".ToCharArray());
TreeNode tn = new TreeNode();
TreeUser.Nodes.Add(tn);
TreeuserBind(tn, "00000000-0000-0000-0000-000000000000", ds, mc, rs, TreeDepartment.SelectedNode.Target);
tn.SelectAction = TreeNodeSelectAction.Expand;
TreeuserBind(tn, tn.Value, ds, mc, rs, TreeDepartment.SelectedNode.Target);
//getchild(tn);
tn.Expand();
this.Label2.Text = "";
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "checkboxn0enable", "<script type='text/javascript'>enablecheckbox();</script>");
}
//------------------------------------------------------------
/// <summary>
/// 传入url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
DataTable GetSign(string url)
{
//id,传入id,如果此id针对模块有权限,而此id的部门拥有此权限,则enable和check=true
Dictionary<string, int> mem = new Dictionary<string, int>();
ArrayList gal = Groups.GetMembersInAccess(url);
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("isuser");
dt.Columns.Add("access");
dt.Columns.Add("isenable");
DataRow dr;
for (int i = 0; i < gal.Count; i++)
{
if (Membership.GetUser(((Member)gal[i]).MemberID) != null)//是用户
{
dr = dt.NewRow();
dr[0] = ((Member)gal[i]).MemberID.ToString();
dr[1] = true.ToString();
dr[2] = ((Member)gal[i]).AccessValue.ToString();
dr[3] = "";
dt.Rows.Add(dr);
}
else
{
ArrayList child = new ArrayList();
getchilds(child, ((Member)gal[i]).MemberID.ToString());//得到其下所有数据
int access = Groups.GetAccessByMemberId(((Member)gal[i]).MemberID, url);
for (int ii = 0; ii < child.Count; ii++)
{
dr = dt.NewRow();
dr[0] = child[ii].ToString();
if (Membership.GetUser(new Guid(child[ii].ToString())) != null)
{
dr[1] = true.ToString();
}
else
{
dr[1] = false.ToString();
}
dr[2] = access.ToString();
if (ii == 0)
{
dr[3] = "";
}
else
{
dr[3] = "disabled = ' true'";
}
dt.Rows.Add(dr);
}
}
}//出来的就是所有用户
return dt;
}
/// <summary>
/// 得到al中的所有子节点
/// </summary>
/// <param name="al"></param>
/// <param name="id"></param>
void getchilds(ArrayList al, string id)
{
al.Add(id);
foreach (string i in Groups.GetUsersInGroup(new Guid(id)))
{
al.Add(Membership.GetUser(i).ProviderUserKey.ToString());
}
foreach (Group ss in Groups.GetSubgroups(Groups.GetGroupById(new Guid(id))))
{
getchilds(al, ss.GroupID.ToString());
}
}
#region ICallbackEventHandler 成员
public string flagsuccess = "";
public string GetCallbackResult()
{
return flagsuccess;
}
public void RaiseCallbackEvent(string eventArgument)
{
this.SaveSetting(eventArgument);
}
#endregion
/// <summary>
/// 部门树点击更改事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeUser_SelectedNodeChanged(object sender, EventArgs e)
{
if (this.TreeDepartment.SelectedNode != null)
{
string sss = this.TreeDepartment.SelectedNode.Text;
this.Label2.Text = "<font color=black>正在对</font> " + sss + "模块 <font color=black>分配用户权限</font>";
}
TreeNode tnnow = TreeUser.SelectedNode;
if (tnnow.Parent != null && TreeDepartment.SelectedNode != null)
{
bool temp = true;
tnnow.Text = Groups.GetGroupById(new Guid(tnnow.Value)).GroupName + getTitle(tnnow.Value, "", TreeDepartment.SelectedNode.Value.Split(",".ToCharArray()), true, TreeDepartment.SelectedNode.Target, ref temp);
}
if (tnnow.SelectAction != TreeNodeSelectAction.Expand)
{
DataSet ds = (DataSet)ViewState["groups"];
MembershipUserCollection mc = (MembershipUserCollection)ViewState["users"];
if (TreeDepartment.SelectedNode != null)
{
string[] rs = TreeDepartment.SelectedNode.Value.Split(",".ToCharArray());
TreeuserBind(tnnow, tnnow.Value, ds, mc, rs, TreeDepartment.SelectedNode.Target);
}
else
{
TreeuserBind(tnnow, tnnow.Value, ds, mc, new string[] { "" }, "");
}
tnnow.Expand();
tnnow.SelectAction = TreeNodeSelectAction.Expand;
}
}
}