PageRenderTime 91ms CodeModel.GetById 48ms RepoModel.GetById 9ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Security/SecuritySiteMapProvider.cs

#
C# | 67 lines | 45 code | 9 blank | 13 comment | 10 complexity | 346cbe45ea7992c7a118495fc1abb04a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. /// <summary>
  9. /// Implementation of the XmlSiteMapProvider that is Rights aware.
  10. /// </summary>
  11. public class SecuritySiteMapProvider : XmlSiteMapProvider
  12. {
  13. /// <summary>
  14. /// Returns whether the SiteMapNode is accessible to the current user.
  15. /// </summary>
  16. public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
  17. {
  18. // We are checking Rights, and other custom attributes here.
  19. // Roles may also be part of the SiteMapNode. Let the base class
  20. // check for that. If false, return false, otherwise, continue
  21. // with our checks.
  22. if (!base.IsAccessibleToUser(context, node))
  23. return false;
  24. bool primaryBlogInstanceOnly;
  25. if (!string.IsNullOrWhiteSpace(node["primaryBlogInstanceOnly"]) && bool.TryParse(node["primaryBlogInstanceOnly"], out primaryBlogInstanceOnly))
  26. {
  27. if (primaryBlogInstanceOnly && !Blog.CurrentInstance.IsPrimary)
  28. {
  29. return false;
  30. }
  31. }
  32. if (!Utils.StringIsNullOrWhitespace(node["rights"]))
  33. {
  34. // By default, all specified Rights must exist.
  35. // We allow this to be overridden via the "rightsAuthorizationCheck"
  36. // attribute.
  37. AuthorizationCheck authCheck = AuthorizationCheck.HasAll;
  38. if (!Utils.StringIsNullOrWhitespace(node["rightsAuthorizationCheck"]))
  39. {
  40. authCheck = Utils.ParseEnum<AuthorizationCheck>(node["rightsAuthorizationCheck"], AuthorizationCheck.HasAll);
  41. }
  42. string[] rightsRaw = node["rights"].Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
  43. List<Rights> rightsToCheck = new List<Rights>();
  44. foreach (string r in rightsRaw)
  45. {
  46. Rights right = Utils.ParseEnum<Rights>(r.Trim(), Rights.None);
  47. if (right != Rights.None)
  48. rightsToCheck.Add(right);
  49. }
  50. if (rightsToCheck.Count > 0)
  51. {
  52. return Security.IsAuthorizedTo(authCheck, rightsToCheck.ToArray());
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. }