Permissions & Authorization for Users #24

Initial Release; Includes Database and MVC refactoring
This commit is contained in:
Gary Sharp
2013-10-10 19:13:16 +11:00
parent 172ce5524a
commit a099d68915
458 changed files with 40221 additions and 12130 deletions
+133
View File
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Disco.Services.Web.Bundles
{
public class Bundle
{
private DateTime? _FileLastModified { get; set; }
private string _FileHash { get; set; }
private string _VersionUrl { get; set; }
public string Url { get; private set; }
public string File { get; private set; }
public string FileHash
{
get
{
#if DEBUG
UpdateFileHash();
#endif
return _FileHash;
}
}
public string ContentType { get; private set; }
public string VersionUrl
{
get
{
#if DEBUG
return string.Format("{0}?v={1}", this.Url, this.FileHash);
#else
return _VersionUrl;
#endif
}
}
public Bundle(string Url, string File)
{
if (string.IsNullOrWhiteSpace(Url))
throw new ArgumentNullException("Url");
if (string.IsNullOrWhiteSpace(File))
throw new ArgumentNullException("File");
Uri fileUri;
if (!Uri.TryCreate(File, UriKind.Absolute, out fileUri))
{
File = HttpContext.Current.Server.MapPath(File);
}
var fileInfo = new FileInfo(File);
if (!fileInfo.Exists)
throw new FileNotFoundException(string.Format("Not Found: {0}", File), File);
this.Url = Url;
this.File = File;
switch (fileInfo.Extension.ToLower())
{
case ".css":
this.ContentType = "text/css";
break;
case ".js":
this.ContentType = "text/javascript";
break;
default:
throw new ArgumentException("Unsupported Bundle File Extension");
}
// Write File Hash
if (fileInfo.Length > 0)
UpdateFileHash();
else
this._FileHash = string.Empty;
//this.Version = fileInfo.LastWriteTimeUtc.Ticks;
this._VersionUrl = string.Format("{0}?v={1}", this.Url, this.FileHash);
}
private void UpdateFileHash()
{
if (System.IO.File.Exists(this.File))
{
var fileLastModified = System.IO.File.GetLastWriteTimeUtc(this.File);
if (!this._FileLastModified.HasValue || this._FileLastModified.Value != fileLastModified)
{
this._FileLastModified = fileLastModified;
var fileBytes = System.IO.File.ReadAllBytes(this.File);
if (fileBytes.Length > 0)
{
using (SHA256 sha = SHA256.Create())
{
byte[] hash = sha.ComputeHash(fileBytes);
this._FileHash = HttpServerUtility.UrlTokenEncode(hash);
return;
}
}
}
else
{
// Already Updated
return;
}
}
this._FileHash = string.Empty;
}
internal void ProcessRequest(HttpContext context)
{
// Write Content Type
context.Response.ContentType = this.ContentType;
// Write Headers
var cache = context.Response.Cache;
cache.SetOmitVaryStar(true);
cache.SetExpires(DateTime.Now.AddYears(1));
cache.SetValidUntilExpires(true);
cache.SetMaxAge(TimeSpan.FromDays(365));
cache.SetCacheability(HttpCacheability.Public);
// Write File
context.Response.WriteFile(this.File);
}
}
}
@@ -0,0 +1,66 @@
using Disco.Services.Web.Bundles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace Disco.Services.Web
{
public static class BundleExtensions
{
public static void BundleDeferred(this HtmlHelper htmlHelper, string BundleUrl)
{
// Ensure 'App-Relative' Url:
BundleUrl = BundleUrl.StartsWith("~/") ? BundleUrl : (BundleUrl.StartsWith("/") ? string.Concat("~", BundleUrl) : string.Concat("~/", BundleUrl));
var deferredBundles = htmlHelper.ViewContext.HttpContext.Items["Bundles.Deferred"] as List<string>;
if (deferredBundles == null)
{
deferredBundles = new List<string>();
htmlHelper.ViewContext.HttpContext.Items["Bundles.Deferred"] = deferredBundles;
}
if (!deferredBundles.Contains(BundleUrl))
deferredBundles.Add(BundleUrl);
}
public static HtmlString BundleRenderDeferred(this HtmlHelper htmlHelper)
{
var deferredBundles = htmlHelper.ViewContext.HttpContext.Items["Bundles.Deferred"] as List<string>;
var uiExtensionScripts = htmlHelper.ViewContext.HttpContext.Items["Bundles.UIExtensionScripts"] as List<HtmlString>;
var uiExtensionCss = htmlHelper.ViewContext.HttpContext.Items["Bundles.UIExtensionCss"] as List<HtmlString>;
if (deferredBundles != null || uiExtensionScripts != null || uiExtensionCss != null)
{
StringBuilder bundleUrls = new StringBuilder();
if (deferredBundles != null)
{
deferredBundles.Reverse();
foreach (string bundleUrl in deferredBundles)
{
bundleUrls.AppendLine(BundleTable.ResolveBundleHtmlElement(bundleUrl));
}
}
if (uiExtensionCss != null)
{
foreach (HtmlString extensionUrl in uiExtensionCss)
bundleUrls.Append("<link href=\"").Append(extensionUrl).AppendLine("\" rel=\"stylesheet\" type=\"text/css\" />");
}
if (uiExtensionScripts != null)
{
foreach (HtmlString extensionUrl in uiExtensionScripts)
bundleUrls.Append("<script src=\"").Append(extensionUrl).AppendLine("\" type=\"text/javascript\"></script>");
}
return new HtmlString(bundleUrls.ToString());
}
else
{
return new HtmlString(string.Empty);
}
}
}
}
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Disco.Services.Web.Bundles
{
internal sealed class BundleHandler : IHttpHandler
{
public Bundle RequestBundle { get; private set; }
public string BundleVirtualPath { get; private set; }
public BundleHandler(Bundle requestBundle, string bundleVirtualPath)
{
this.RequestBundle = requestBundle;
this.BundleVirtualPath = bundleVirtualPath;
}
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
context.Response.StatusCode = 304;
else
this.RequestBundle.ProcessRequest(context);
}
internal static bool RemapHandlerForBundleRequests(HttpApplication app)
{
var context = app.Context;
string bundleUrlFromContext = context.Request.AppRelativeCurrentExecutionFilePath + context.Request.PathInfo;
var bundle = BundleTable.GetBundleFor(bundleUrlFromContext);
if (bundle != null)
{
context.RemapHandler(new BundleHandler(bundle, bundleUrlFromContext));
return true;
}
return false;
}
}
}
@@ -0,0 +1,44 @@
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Disco.Services.Web.Bundles.BundleModule), "PreApplicationStart")]
namespace Disco.Services.Web.Bundles
{
public class BundleModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache);
}
private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (BundleTable.Count > 0)
{
BundleHandler.RemapHandlerForBundleRequests(app);
}
}
private static bool _startWasCalled;
public static void PreApplicationStart()
{
if (!_startWasCalled)
{
_startWasCalled = true;
DynamicModuleUtility.RegisterModule(typeof(BundleModule));
}
}
public void Dispose()
{
// Dispose Nothing
}
}
}
+70
View File
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Disco.Services.Web.Bundles
{
public static class BundleTable
{
private static Dictionary<string, Bundle> _bundles;
static BundleTable()
{
_bundles = new Dictionary<string, Bundle>();
}
public static void Add(Bundle Bundle)
{
_bundles[Bundle.Url] = Bundle;
}
public static int Count
{
get
{
return _bundles.Count;
}
}
internal static Bundle GetBundleFor(string Url)
{
if (_bundles.ContainsKey(Url))
{
return _bundles[Url];
}
return null;
}
public static string ResolveBundleUrl(string BundleUrl)
{
var bundle = GetBundleFor(BundleUrl);
if (bundle == null)
throw new ArgumentException(string.Format("Unknown Bundle Url: {0}", BundleUrl), "BundleUrl");
return VirtualPathUtility.ToAbsolute(bundle.VersionUrl);
}
public static string ResolveBundleHtmlElement(string BundleUrl)
{
var bundle = GetBundleFor(BundleUrl);
if (bundle == null)
throw new ArgumentException(string.Format("Unknown Bundle Url: {0}", BundleUrl), "BundleUrl");
var bundleUrl = VirtualPathUtility.ToAbsolute(bundle.VersionUrl);
switch (bundle.ContentType)
{
case "text/css":
return string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", bundleUrl);
case "text/javascript":
return string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>", bundleUrl);
default:
throw new ArgumentException("Unsupported Bundle Content Type", "BundleUrl");
}
}
}
}