Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public class AuthorizationModule : IHttpModule
  2.     {
  3.         public void Init(HttpApplication application)
  4.         {
  5.             application.AuthorizeRequest += new EventHandler(authorize);
  6.         }
  7.  
  8.         public void authorize(object sender, EventArgs e)
  9.         {
  10.             HttpApplication application = (HttpApplication)sender;
  11.  
  12.             if (existValidUser())
  13.             {
  14.                 using (SecurityDAO secDAO = new SecurityDAO())
  15.                 {
  16.                     Menu menu = secDAO.getMenuByPath(getVirtualPathAsLowerCase(application));
  17.                     if (menu != null)
  18.                     {
  19.                         foreach (Role menuRole in secDAO.getRolesFor(menu))
  20.                         {
  21.                             if (!userIsInRole(application, menuRole))
  22.                             {
  23.                                 throw new HttpException(401, "UnAuthorized access to " + application.Request.Path);
  24.                             }
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.  
  31.         private bool userIsInRole(HttpApplication application, Role menuRole)
  32.         {
  33.             return application.User.IsInRole(menuRole.Name);
  34.         }
  35.  
  36.         private string getVirtualPathAsLowerCase(HttpApplication application)
  37.         {
  38.             return WebAppUtil.removeVirtualPathAndConvertToLowerCase(application.Request.Path);
  39.         }
  40.  
  41.         private bool existValidUser()
  42.         {
  43.             return HttpContext.Current.User != null &&
  44.                 HttpContext.Current.User.Identity.IsAuthenticated &&
  45.                 HttpContext.Current.User.Identity.GetType() == typeof(FormsIdentity);
  46.         }
  47.        
  48.         public void Dispose()
  49.         {
  50.         }
  51.     }