Beispiel #1
0
        private void BindMenu()
        {
            if (string.IsNullOrEmpty(this.Path))
            {
                return;
            }

            XmlDocument menuDoc = new XmlDocument();

            menuDoc.Load(Server.MapPath("~/App_Data/adminmenu.xml"));
            string[] parentPaths = Path.Split('/');
            string   xpath       = string.Empty;

            foreach (string parentPath in parentPaths)
            {
                xpath += GetMenuItemXPath(parentPath) + "/";
            }
            string parentXPath = xpath.Substring(0, xpath.Length - 1);

            xpath = xpath + "*";

            List <AdminMenuItem> menuItems = new List <AdminMenuItem>();

            // PARENT NODE
            XmlNode parentNode = menuDoc.DocumentElement.SelectSingleNode(parentXPath);

            if (parentNode != null && CheckPermissions(parentNode))
            {
                //  CURRENT PAGE URL
                string currentPageUrl = Request.AppRelativeCurrentExecutionFilePath.ToLowerInvariant();

                // ADD SUB ITEMS
                XmlNodeList nodes = menuDoc.DocumentElement.SelectNodes(xpath);
                foreach (XmlNode node in nodes)
                {
                    // IF USER IS NOT ALLOWED TO SEE THIS URL SKIP THIS NODE AND CONTINUE WITH NEXT
                    if (!CheckPermissions(node))
                    {
                        continue;
                    }

                    string title       = string.Empty;
                    string description = string.Empty;
                    string url         = string.Empty;

                    // USE THE navtitle ATTRIBUTE AS DEFAULT VALUE, iF NOT PRESENT USE title ATTRIBUTE
                    if (node.Attributes["navtitle"] != null)
                    {
                        title = node.Attributes["navtitle"].Value;
                    }
                    else if (node.Attributes["title"] != null)
                    {
                        title = node.Attributes["title"].Value;
                    }

                    if (node.Attributes["description"] != null)
                    {
                        description = node.Attributes["description"].Value;
                    }

                    if (node.Attributes["url"] != null)
                    {
                        url = node.Attributes["url"].Value;
                    }

                    // IDENTIFY CURRENT PAGE ITEM
                    bool isCurrentPageItem = url.ToLowerInvariant() == currentPageUrl;

                    menuItems.Add(new AdminMenuItem()
                    {
                        Title = title, Description = description, Url = Page.ResolveUrl(url), IsCurrentPageItem = isCurrentPageItem
                    });
                }

                MenuItemRepeater.DataSource = menuItems;
                MenuItemRepeater.DataBind();
            }
            else
            {
                MainPanel.Visible = false;
            }
        }
        private void BindMenu()
        {
            if (string.IsNullOrEmpty(this.Path))
            {
                return;
            }

            XmlDocument menuDoc = new XmlDocument();

            menuDoc.Load(Server.MapPath("~/App_Data/adminmenu.xml"));
            string[] parentPaths = Path.Split('/');
            string   xpath       = string.Empty;

            foreach (string parentPath in parentPaths)
            {
                xpath += GetMenuItemXPath(parentPath) + "/";
            }
            string parentXPath = xpath.Substring(0, xpath.Length - 1);

            xpath = xpath + "*";

            List <AdminMenuItem> menuItems = new List <AdminMenuItem>();

            // ADD PARENT NODE
            XmlNode parentNode = menuDoc.DocumentElement.SelectSingleNode(parentXPath);

            if (CheckPermissions(parentNode))
            {
                if (parentNode.Attributes["title"] != null)
                {
                    this.HeaderText = parentNode.Attributes["title"].Value;
                }

                if (parentNode.Attributes["description"] != null)
                {
                    this.Description = parentNode.Attributes["description"].Value;
                }

                if (parentNode.Attributes["url"] != null)
                {
                    this.Url = Page.ResolveUrl(parentNode.Attributes["url"].Value);
                }

                // ADD SUB ITEMS
                XmlNodeList nodes = menuDoc.DocumentElement.SelectNodes(xpath);
                foreach (XmlNode node in nodes)
                {
                    // IF USER IS NOT ALLOWED TO SEE THIS URL SKIP THIS NODE AND CONTINUE WITH NEXT
                    if (!CheckPermissions(node))
                    {
                        continue;
                    }

                    string title       = string.Empty;
                    string description = string.Empty;
                    string url         = string.Empty;

                    if (node.Attributes["title"] != null)
                    {
                        title = node.Attributes["title"].Value;
                    }

                    if (node.Attributes["description"] != null)
                    {
                        description = node.Attributes["description"].Value;
                    }

                    if (node.Attributes["url"] != null)
                    {
                        url = node.Attributes["url"].Value;
                    }

                    menuItems.Add(new AdminMenuItem()
                    {
                        Title = title, Description = description, Url = Page.ResolveUrl(url)
                    });
                }

                MenuItemRepeater.DataSource = menuItems;
                MenuItemRepeater.DataBind();
            }
            else
            {
                MainPanel.Visible = false;
            }
        }
Beispiel #3
0
        private void BindMenu(string path)
        {
            XmlDocument menuDoc = new XmlDocument();

            menuDoc.Load(Server.MapPath("~/App_Data/adminmenu.xml"));
            string[] parentPaths = path.Split('/');
            string   xpath       = string.Empty;

            foreach (string parentPath in parentPaths)
            {
                xpath += GetMenuItemXPath(parentPath) + "/";
            }
            xpath = xpath + "*";
            string parentXPath = xpath.Substring(0, xpath.Length - 2);

            // bind the caption
            string  parentTitle = string.Empty;
            XmlNode parentNode  = menuDoc.DocumentElement.SelectSingleNode(parentXPath);

            if (parentNode != null && parentNode.Attributes["title"] != null)
            {
                parentTitle = parentNode.Attributes["title"].Value;
            }
            Caption.Text = string.Format(Caption.Text, parentTitle);

            XmlNodeList          nodes     = menuDoc.DocumentElement.SelectNodes(xpath);
            List <AdminMenuItem> menuItems = new List <AdminMenuItem>();

            foreach (XmlNode node in nodes)
            {
                if (node.Attributes["roles"] != null)
                {
                    string[] roles   = node.Attributes["roles"].Value.Split(',');
                    bool     allowed = false;
                    foreach (string role in roles)
                    {
                        // IF USER IS IN AN ALLOWED ROLE, THEN BREAK ADD THE MENU ITEM AND CONTINUE WITH NEXT
                        if (Page.User.IsInRole(role))
                        {
                            allowed = true;
                            break;
                        }
                    }

                    // IF USER IS NOT ALLOWED TO SEE THIS URL SKIP THIS NODE AND CONTINUE WITH NEXT
                    if (!allowed)
                    {
                        continue;
                    }
                }

                string title       = string.Empty;
                string description = string.Empty;
                string url         = string.Empty;

                if (node.Attributes["title"] != null)
                {
                    title = node.Attributes["title"].Value;
                }

                if (node.Attributes["description"] != null)
                {
                    description = node.Attributes["description"].Value;
                }

                if (node.Attributes["url"] != null)
                {
                    url = node.Attributes["url"].Value;
                }

                menuItems.Add(new AdminMenuItem()
                {
                    Title = title, Description = description, Url = Page.ResolveUrl(url)
                });
            }

            MenuItemRepeater.DataSource = menuItems;
            MenuItemRepeater.DataBind();
        }