Example #1
0
        public void load_course_files()
        {
            for (int i = 0; i < courseList.Count; i++)
            {
                HtmlAgilityPack.HtmlDocument doc = getHtmlDoc(courseList[i].getCourseHref());
                //li[@class='section main clearfix']
                HtmlAgilityPack.HtmlNodeCollection htmlNodeColl = doc.DocumentNode.SelectNodes("//div[@id='page-content']//ul[@class='topics']//li");
                //HtmlAgilityPack.HtmlNodeCollection htmlNodeColl2 = doc.DocumentNode.SelectNodes("//div[@id='page-content']//ul[@class='topics']//ul");



                courseFilesList = new List <CourseFile>();
                for (int j = 0; j < htmlNodeColl.Count; j++)
                {
                    if (htmlNodeColl[j].Descendants("a").Any(y => y.Attributes["onclick"] != null))
                    {
                        var element = htmlNodeColl[j].Descendants("a").First(y => y.Attributes["onclick"] != null);
                        if (element.Attributes["onclick"].Value != "")
                        {
                            String        referenceUrl = getReferenceUrl(element.Attributes["onclick"].Value);
                            List <String> fileLinks    = new List <String>();
                            fileLinks = getFileLink(referenceUrl);
                            foreach (String link in fileLinks)
                            {
                                if (!courseFilesList.Exists(x => x.getFileName() == getFileName(link)))
                                {
                                    CourseFile cf = new CourseFile();
                                    cf.setFileLink(link);
                                    cf.setFileName(getFileName(link));
                                    courseFilesList.Add(cf);
                                }
                            }
                        }
                        else if (element.Attributes["href"].Value != "" &&
                                 element.Attributes["href"].Value.Contains("folder"))
                        {
                            String referenceUrl = getReferenceUrl(element.Attributes["href"].Value);



                            List <String> fileLinks = new List <String>();
                            fileLinks = getFileLinkFolder(referenceUrl);

                            foreach (String link in fileLinks)
                            {
                                if (!courseFilesList.Exists(x => x.getFileName() == getFileName(link)))
                                {
                                    CourseFile cf = new CourseFile();
                                    cf.setFileLink(link);
                                    cf.setFileName(getFileName(link));
                                    courseFilesList.Add(cf);
                                }
                            }
                        }
                    }
                    else
                    {
                        HtmlAgilityPack.HtmlNodeCollection htmlNodeColl2 = doc.DocumentNode.SelectNodes("//div[@id='page-content']//ul[@class='topics']//li");


                        for (int k = 0; k < htmlNodeColl2.Count(); k++)
                        {
                            if (htmlNodeColl[k].Descendants("a").Any(y => y.Attributes["href"] != null))
                            {
                                var element = htmlNodeColl[k].Descendants("a").First(y => y.Attributes["href"] != null);
                                if (element.Attributes["href"].Value != "")
                                {
                                    String referenceUrl = element.Attributes["href"].Value;

                                    if (referenceUrl.Contains("resource"))
                                    {
                                        HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(referenceUrl);
                                        request2.CookieContainer = container;
                                        WebResponse response2   = request2.GetResponse();
                                        Uri         responseUrl = response2.ResponseUri;
                                        response2.Close();
                                        String responseUrlString = responseUrl.ToString();

                                        if (!courseFilesList.Exists(x => x.getFileName() == getFileName(responseUrlString)))
                                        {
                                            if (getFileName(responseUrlString).EndsWith(".pdf"))
                                            {
                                                CourseFile cf = new CourseFile();
                                                cf.setFileLink(responseUrlString);
                                                cf.setFileName(getFileName(responseUrlString));
                                                courseFilesList.Add(cf);
                                            }
                                        }
                                    }
                                }
                            }
                        }


                        /*
                         */
                    }
                }
                courseFileDict.Add(courseList[i], courseFilesList);
            }
        }
        public override void Close()
        {
            string html = sbHtml.ToString();

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option");
            HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("dt");
            HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("dd");
            HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("dl");
            htmlDoc.LoadHtml(html);
            //查找需要权限控制的html标签
            HtmlAgilityPack.HtmlNodeCollection chkRightNodes = htmlDoc.DocumentNode.SelectNodes("//@" + AuthManage.CHECKRIGHTAG);

            if (chkRightNodes == null || chkRightNodes.Count() == 0)
            {
                byte[] originalData = System.Text.UTF8Encoding.UTF8.GetBytes(html);
                responseStream.Write(originalData, 0, originalData.Length);
                htmlDoc = null;
                base.Close();
                responseStream.Close();
                return;
            }
            //用户权限代码列表
            List <UserRight> sysUserRights  = this.AllUserRight.Where(u => u.DataId.HasValue == true && u.DataId.Value == 0).ToList();
            List <string>    userRightCodes = sysUserRights.Select(r => r.Code).Distinct().ToList();

            foreach (var node in chkRightNodes)
            {
                if (node.Attributes.Contains(AuthManage.CHECKRIGHTAG) == false)
                {
                    continue;
                }
                string rightCode = node.Attributes[AuthManage.CHECKRIGHTAG].Value;
                //多个权限 MODULECODE_RIGHTCODE|1|数据对象Id_数据实例Id,MODULECODE_RIGHTCODE
                //MODULECODE_RIGHTCODE|userId_1
                //MODULECODE_RIGHTCODE|数据对象Id_数据实例Id
                ////MODULECODE_RIGHTCODE|userId_1|数据对象Id_数据实例Id
                // MODULECODE_RIGHTCODE --权限代码
                //userId_1 --标示具有指定权限代码并且数据的创建人是本人
                //数据对象Id_数据实例Id --指定该权限项验证的是指定数据对象,数据实例的权限(比如某个项目的权限)
                string[] arrCode  = rightCode.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                bool     hasRight = false;
                #region 判断是否有权限
                foreach (var c in arrCode)
                {
                    //格式, 1|MODULECODE_RIGHTCODE
                    string[] arrc = c.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    switch (arrc.Length)
                    {
                    case 1:
                        //只有系统权限项判断
                        if (userRightCodes.Contains(arrc[0]) == true)
                        {
                            hasRight = true;
                        }
                        break;

                    case 2:
                        //系统权限项+创建用户 或者 数据权限+指定数据对象+指定对象实例Id
                        string[] arrSecond = arrc[1].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                        if (arrSecond.Length == 2)
                        {
                            if (arrSecond[0] == AuthManage.USERTAG)
                            {
                                //创建用户
                                hasRight = userRightCodes.Contains(arrc[0]) == true && arrSecond[1] == this.UserId.ToString();
                            }
                            else
                            {
                                hasRight = this.CheckDataRight(int.Parse(arrSecond[0]), int.Parse(arrSecond[1]), arrc[0], null);
                            }
                        }
                        break;

                    case 3:
                        //数据权限(项目),并且配合创建人
                        //创建人
                        string[] arrUser = arrc[1].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                        //数据对象-数据实例
                        string[] arrData = arrc[2].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                        if (arrUser.Length == 2 && arrData.Length == 2)
                        {
                            hasRight = this.CheckDataRight(int.Parse(arrData[0]), int.Parse(arrData[1]), arrc[0], int.Parse(arrUser[1]));
                        }
                        break;
                    }
                    if (hasRight == true)
                    {
                        break;
                    }
                }
                if (hasRight == false)
                {
                    node.ParentNode.RemoveChild(node, false);
                }
                #endregion
            }
            //过滤后的html代码
            var sbFilter = new StringBuilder();
            using (var writer = new StringWriter(sbFilter))
            {
                htmlDoc.Save(writer);
            }
            byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(sbFilter.ToString());

            responseStream.Write(data, 0, data.Length);
            base.Close();
            responseStream.Close();
        }