PageRenderTime 1598ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/WebUtils.cs

#
C# | 151 lines | 97 code | 13 blank | 41 comment | 6 complexity | 5c02e74045589715cb36ab229ea8e52b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace App_Code
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using BlogEngine.Core;
  8. public static class WebUtils
  9. {
  10. /// <summary>
  11. /// Checks to see if the current user has the rights to access an
  12. /// admin settings page.
  13. /// </summary>
  14. /// <param name="checkOnly">
  15. /// If true, check only. If false and rights are insufficient, user
  16. /// will be redirected to the login page.
  17. /// </param>
  18. /// <returns>True if user has sufficient rights</returns>
  19. public static bool CheckRightsForAdminSettingsPage(bool checkOnly)
  20. {
  21. if (checkOnly)
  22. {
  23. return
  24. Security.IsAuthorizedTo(AuthorizationCheck.HasAll,
  25. BlogEngine.Core.Rights.AccessAdminSettingsPages);
  26. }
  27. else
  28. {
  29. Security.DemandUserHasRight(AuthorizationCheck.HasAll, true,
  30. BlogEngine.Core.Rights.AccessAdminSettingsPages);
  31. }
  32. return true;
  33. }
  34. /// <summary>
  35. /// Checks to see if the current user has the rights to moderate comments pages.
  36. /// </summary>
  37. /// <param name="checkOnly">
  38. /// If true, check only. If false and rights are insufficient, user
  39. /// will be redirected to the login page.
  40. /// </param>
  41. /// <returns>True if user has sufficient rights</returns>
  42. public static bool CheckRightsForAdminCommentsPages(bool checkOnly)
  43. {
  44. if (checkOnly)
  45. {
  46. return
  47. Security.IsAuthorizedTo(AuthorizationCheck.HasAll,
  48. BlogEngine.Core.Rights.ModerateComments);
  49. }
  50. else
  51. {
  52. Security.DemandUserHasRight(AuthorizationCheck.HasAll, true,
  53. BlogEngine.Core.Rights.ModerateComments);
  54. }
  55. return true;
  56. }
  57. /// <summary>
  58. /// Checks to see if the current user has the rights to view Pages pages.
  59. /// </summary>
  60. /// <param name="checkOnly">
  61. /// If true, check only. If false and rights are insufficient, user
  62. /// will be redirected to the login page.
  63. /// </param>
  64. /// <returns>True if user has sufficient rights</returns>
  65. public static bool CheckRightsForAdminPagesPages(bool checkOnly)
  66. {
  67. Rights[] rights =
  68. {
  69. Rights.CreateNewPages,
  70. Rights.EditOwnPages,
  71. Rights.EditOtherUsersPages,
  72. Rights.PublishOwnPages,
  73. Rights.PublishOtherUsersPages
  74. };
  75. if (checkOnly)
  76. {
  77. return Security.IsAuthorizedTo(AuthorizationCheck.HasAny, rights);
  78. }
  79. else
  80. {
  81. Security.DemandUserHasRight(AuthorizationCheck.HasAny, true, rights);
  82. }
  83. return true;
  84. }
  85. /// <summary>
  86. /// Checks to see if the current user has the rights to view Post pages.
  87. /// </summary>
  88. /// <param name="checkOnly">
  89. /// If true, check only. If false and rights are insufficient, user
  90. /// will be redirected to the login page.
  91. /// </param>
  92. /// <returns>True if user has sufficient rights</returns>
  93. public static bool CheckRightsForAdminPostPages(bool checkOnly)
  94. {
  95. Rights[] rights =
  96. {
  97. Rights.CreateNewPosts,
  98. Rights.EditOwnPosts,
  99. Rights.EditOtherUsersPosts,
  100. Rights.PublishOwnPosts,
  101. Rights.PublishOtherUsersPosts
  102. };
  103. if (checkOnly)
  104. {
  105. return Security.IsAuthorizedTo(AuthorizationCheck.HasAny, rights);
  106. }
  107. else
  108. {
  109. Security.DemandUserHasRight(AuthorizationCheck.HasAny, true, rights);
  110. }
  111. return true;
  112. }
  113. /// <summary>
  114. /// Checks to see if the current blog is the primary blog.
  115. /// </summary>
  116. /// <param name="checkOnly">
  117. /// If true, check only. If false and is not the primary blog, user
  118. /// will be redirected to the login page.
  119. /// </param>
  120. /// <returns>True if user has sufficient rights</returns>
  121. public static bool CheckIfPrimaryBlog(bool checkOnly)
  122. {
  123. if (checkOnly)
  124. {
  125. return Blog.CurrentInstance.IsPrimary;
  126. }
  127. else
  128. {
  129. if (!Blog.CurrentInstance.IsPrimary)
  130. {
  131. Security.RedirectForUnauthorizedRequest();
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. }
  138. }