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