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