PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/menu.ascx.cs

#
C# | 108 lines | 68 code | 17 blank | 23 comment | 10 complexity | a54e3091845234e96098bf1db2cf1b6f MD5 | raw file
  1namespace Admin
  2{
  3    using System;
  4    using System.Linq;
  5    using System.Web;
  6    using System.Web.UI;
  7    using System.Web.UI.HtmlControls;
  8
  9    using BlogEngine.Core;
 10
 11    using Resources;
 12
 13    /// <summary>
 14    /// The Menu control.
 15    /// </summary>
 16    public partial class Menu : UserControl
 17    {
 18        #region Public Methods
 19
 20        /// <summary>
 21        /// Adds the item.
 22        /// </summary>
 23        /// <param name="text">
 24        /// The text string.
 25        /// </param>
 26        /// <param name="url">
 27        /// The URL string.
 28        /// </param>
 29        public void AddItem(string text, string url)
 30        {
 31            var a = new HtmlAnchor { InnerHtml = string.Format("<span>{0}</span>", text), HRef = url };
 32
 33            System.Diagnostics.Debug.WriteLine("AddItem: " + a.HRef.ToString());
 34
 35            var li = new HtmlGenericControl("li");
 36            li.Controls.Add(a);
 37            ulMenu.Controls.Add(li);
 38        }
 39
 40        #endregion
 41
 42        #region Methods
 43
 44        /// <summary>
 45        /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
 46        /// </summary>
 47        /// <param name="e">
 48        /// The <see cref="T:System.EventArgs"/> object that contains the event data.
 49        /// </param>
 50        protected override void OnLoad(EventArgs e)
 51        {
 52            base.OnLoad(e);
 53
 54            if (!Page.IsCallback)
 55            {
 56                BindMenu();
 57            }
 58        }
 59
 60        /// <summary>
 61        /// The bind menu.
 62        /// </summary>
 63        private void BindMenu()
 64        {
 65            var sitemap = SiteMap.Providers["SecuritySiteMap"];
 66            if (sitemap != null)
 67            {
 68                string adminRootFolder = string.Format("{0}admin", Utils.RelativeWebRoot);
 69
 70                var root = sitemap.RootNode;
 71                if (root != null)
 72                {
 73                    foreach (
 74                        var adminNode in root.ChildNodes.Cast<SiteMapNode>().Where(
 75                            adminNode => adminNode.IsAccessibleToUser(HttpContext.Current)).Where(
 76                            adminNode => Security.IsAuthenticated ))
 77                    {
 78                        if (adminNode.Url.EndsWith("#/blogs") && !Blog.CurrentInstance.IsPrimary)
 79                            continue;
 80
 81                        if (adminNode.Url.EndsWith("#/blogs") && !Security.IsAdministrator)
 82                            continue;
 83
 84                        var a = new HtmlAnchor
 85                        {
 86                            // replace the RelativeWebRoot in adminNode.Url with the RelativeWebRoot of the current
 87                            // blog instance.  So a URL like /admin/Dashboard.aspx becomes /blog/admin/Dashboard.aspx.
 88                            HRef = Utils.RelativeWebRoot + adminNode.Url.Substring(Utils.ApplicationRelativeWebRoot.Length), 
 89                            InnerHtml = string.Format("<span>{0}</span>", Utils.Translate(adminNode.Title, adminNode.Title))
 90                        };
 91
 92                        var li = new HtmlGenericControl("li");
 93                        li.Controls.Add(a);
 94                        ulMenu.Controls.Add(li);
 95                    }
 96                }
 97            }
 98
 99            if (Security.IsAuthenticated)
100            {
101                AddItem(labels.myProfile, string.Format("{0}admin/#/users/profile", Utils.RelativeWebRoot));
102                AddItem(labels.changePassword, string.Format("{0}Account/change-password.aspx", Utils.RelativeWebRoot));
103            }
104        }
105
106        #endregion
107    }
108}