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
@@ -1,41 +0,0 @@
using Disco.BI.UserBI;
using Disco.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace Disco.Web
{
public class AuthorizeDiscoUsersAttribute : AuthorizeAttribute
{
string[] authorizedTypes;
public AuthorizeDiscoUsersAttribute(params string[] AuthorizedUserTypes)
{
if (AuthorizedUserTypes == null)
throw new ArgumentNullException("AuthorizedUserTypes");
if (AuthorizedUserTypes.Length == 0)
throw new ArgumentOutOfRangeException("AuthorizedUserTypes", "At least one Authorized User Type must be specified");
authorizedTypes = AuthorizedUserTypes;
}
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
var DiscoUser = UserCache.CurrentUser;
if (DiscoUser != null && authorizedTypes.Contains(DiscoUser.Type))
return true;
return false;
}
}
}
@@ -1,133 +0,0 @@
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.Web.Extensions.MvcExtensions.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);
}
}
}
@@ -1,65 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Disco.Web.Extensions.MvcExtensions.Bundles;
namespace Disco.Web.Extensions
{
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);
}
}
}
}
@@ -1,52 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Disco.Web.Extensions.MvcExtensions.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;
}
}
}
@@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Disco.Web.Extensions.MvcExtensions.Bundles.BundleModule), "PreApplicationStart")]
namespace Disco.Web.Extensions.MvcExtensions.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
}
}
}
@@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Disco.Web.Extensions.MvcExtensions.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");
}
}
}
}
@@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Optimization;
namespace Disco.Web.Extensions.MvcExtensions.Bundles
{
public class BundleUnordered : IBundleOrderer
{
public IEnumerable<System.IO.FileInfo> OrderFiles(BundleContext context, IEnumerable<System.IO.FileInfo> files)
{
return files;
}
}
}
@@ -1,63 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Optimization;
using System.IO;
namespace Disco.Web.Extensions.MvcExtensions.Bundles
{
public class JsJoin : IBundleTransform
{
internal static string JsContentType;
static JsJoin()
{
JsContentType = "text/javascript";
}
public void Process(BundleContext context, BundleResponse response)
{
if (context == null)
throw new ArgumentNullException("context");
if (response == null)
throw new ArgumentNullException("response");
// Not Needed - the new Transforms pipeline has already loaded the content
//if (!context.EnableInstrumentation && string.IsNullOrEmpty(response.Content))
//{
// try
// {
// StringBuilder bundleContent = new StringBuilder();
// foreach (FileInfo file in response.Files)
// {
// string fileContent = File.ReadAllText(file.FullName);
// bundleContent.AppendLine(fileContent);
// }
// response.Content = bundleContent.ToString();
// }
// catch (Exception ex)
// {
// GenerateErrorResponse(response, new string[] { ex.GetType().Name, ex.Message, ex.StackTrace });
// }
//}
response.ContentType = JsContentType;
}
internal static void GenerateErrorResponse(BundleResponse bundle, ICollection<string> errors)
{
StringBuilder builder = new StringBuilder();
builder.Append("/* ");
builder.Append("Bundle creation failed [JsJoin].").Append("\r\n");
foreach (string str in errors)
{
builder.Append(str).Append("\r\n");
}
builder.Append(" */\r\n");
bundle.Content = builder.ToString();
}
}
}
@@ -1,140 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using dotless.Core;
using dotless.Core.configuration;
using System.Web.Optimization;
namespace Disco.Web.Extensions.MvcExtensions.Bundles
{
public class LessCompile : IBundleTransform
{
internal static string CssContentType;
internal static readonly ILessEngine Instance;
static LessCompile()
{
CssContentType = "text/css";
Instance = new EngineFactory(new DotlessConfiguration()
{
CacheEnabled = false,
MinifyOutput = true
}).GetEngine();
}
public void Process(BundleContext context, BundleResponse response)
{
if (context == null)
throw new ArgumentNullException("context");
if (response == null)
throw new ArgumentNullException("response");
if (!context.EnableInstrumentation)
{
try
{
StringBuilder bundleContent = new StringBuilder();
Uri appRootPath = new Uri(HttpContext.Current.Request.PhysicalApplicationPath);
var restoreEnvironmentCurrentDirectory = Environment.CurrentDirectory;
foreach (FileInfo file in response.Files)
{
string fileContent = File.ReadAllText(file.FullName);
Uri fileRootPath = new Uri(file.DirectoryName + "/");
// Less Compile
Environment.CurrentDirectory = file.DirectoryName;
fileContent = Instance.TransformToCss(fileContent, file.FullName);
// Embed Images
fileContent = EmbedCssImages(fileContent, fileRootPath, appRootPath);
bundleContent.Append(fileContent);
}
if (!Environment.CurrentDirectory.Equals(restoreEnvironmentCurrentDirectory))
Environment.CurrentDirectory = restoreEnvironmentCurrentDirectory;
response.Content = bundleContent.ToString();
}
catch (Exception ex)
{
GenerateErrorResponse(response, new string[] { ex.GetType().Name, ex.Message, ex.StackTrace });
}
}
response.ContentType = CssContentType;
}
private static string EmbedCssImages(string cssContent, Uri fileRootPath, Uri appRootPath)
{
return Regex.Replace(cssContent, "url\\((.*?)\\)", m =>
{
var cssFilename = m.Groups[1].Value.Trim(new char[] { '\'', '"' });
Uri fileUri;
if (cssFilename.StartsWith("/"))
fileUri = new Uri(appRootPath, cssFilename);
else
fileUri = new Uri(fileRootPath, cssFilename);
if (File.Exists(fileUri.LocalPath))
{
var fileInfo = new FileInfo(fileUri.LocalPath);
// Ensure File is < 250kb
if (fileInfo.Length < 256000)
{
string contentType = null;
switch (fileInfo.Extension)
{
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".jpg":
case ".jpeg":
contentType = "image/jpeg";
break;
default:
return m.Value;
}
StringBuilder sb = new StringBuilder();
sb.Append("url(data:");
sb.Append(contentType);
sb.Append(";base64,");
sb.Append(Convert.ToBase64String(File.ReadAllBytes(fileInfo.FullName)));
sb.Append(")");
return sb.ToString();
}
else
{
return string.Format("url(/{0})", appRootPath.MakeRelativeUri(fileUri).ToString());
}
}
else
{
throw new FileNotFoundException(string.Format("Unable to embed css image, file not found: '{0}' at '{1}'", cssFilename, fileUri.AbsolutePath), cssFilename);
}
});
}
internal static void GenerateErrorResponse(BundleResponse bundle, ICollection<string> errors)
{
StringBuilder builder = new StringBuilder();
builder.Append("/* ");
builder.Append("Bundle creation failed [LessCompile].").Append("\r\n");
foreach (string str in errors)
{
builder.Append(str).Append("\r\n");
}
builder.Append(" */\r\n");
bundle.Content = builder.ToString();
}
}
}
@@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Disco.Web
{
[AuthorizeDiscoUsersAttribute(Disco.Models.Repository.User.Types.Admin)]
public class dbAdminController : dbController
{
}
}
@@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Disco.Data.Repository;
namespace Disco.Web
{
[OutputCache(Duration = 0, Location = System.Web.UI.OutputCacheLocation.None)]
public class dbController : Controller
{
protected DiscoDataContext dbContext;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
this.dbContext = new DiscoDataContext();
this.dbContext.Configuration.LazyLoadingEnabled = false;
base.OnActionExecuting(filterContext);
}
protected override void Dispose(bool disposing)
{
if (this.dbContext != null)
{
this.dbContext.Dispose();
this.dbContext = null;
}
base.Dispose(disposing);
}
}
}