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.Xml;
using hqgl.user;
using hqgl.dept;
using System.Text.RegularExpressions;
public partial class tree : System.Web.UI.Page
{
    private string allow = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        userchuli uch = new userchuli();
        uch.user_yanzheng();
        deptchuli dch = new deptchuli();
        string uid = uch.user_getdeptid();
        allow = dch.dept_getfunc(uid);
        if (!Page.IsPostBack)
        {
            string id = Request["id"];
            if (!string.IsNullOrEmpty(id))
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("web.sitemap"));
                XmlNodeList xln = xdoc.LastChild.LastChild.ChildNodes;
                XmlNode xnn = xdoc;
                foreach (XmlNode xn in xln)
                {
                    string tid = xn.Attributes["ID"].Value;
                    if (tid == id)
                    {
                        xnn = xn;
                    }
                }
                XmlDataSource xds = new XmlDataSource();
                xds.EnableCaching = false;
                xds.Data =AuthenPath(xnn,true);
                TreeView1.DataSource = xds;
                TreeView1.DataBind();
            }
            else
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("web.sitemap"));
                XmlDataSource xds = new XmlDataSource();
                xds.EnableCaching = false;
                xds.Data = AuthenPath(xdoc.LastChild, false) ;
                TreeView1.DataSource = xds;
                TreeView1.DataBind();
            }
        }
    }
    public string AuthenPath(XmlNode txml,bool f)
    {
        for (int i = 0; i < txml.ChildNodes.Count;i++ )
        {
            XmlNode xn = txml.ChildNodes[i];
            string index = xn.Attributes["index"].Value;
            if (Check(index))
                continue;
            else
            {
                if (xn.ChildNodes.Count > 0)
                    AuthenPath(xn, true);
                if (xn.ChildNodes.Count == 0)
                {
                    txml.RemoveChild(xn);
                    i--;
                }
            }
        }
        if (f)
            return txml.OuterXml;
        else
            return txml.InnerXml;
    }
    public bool Check(string index)
    {
        string[] regs = allow.Split(',');
        for (int i = 0; i < regs.Length; i++)
        {
            Regex reg = new Regex("^"+regs[i] + ".*");
            if(reg.IsMatch(index))
                return true;
        }
        return false;
    }
}
