This commit is contained in:
Stephan
2012-09-28 07:08:37 -02:00
79 changed files with 3199 additions and 384 deletions

View File

@@ -0,0 +1,20 @@
using System;
namespace Umbraco.Core
{
/// <summary>
/// Helper methods for Activation
/// </summary>
internal static class ActivatorHelper
{
/// <summary>
/// Creates an instance of a type using that type's default constructor.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateInstance<T>() where T : class, new()
{
return Activator.CreateInstance(typeof(T)) as T;
}
}
}

View File

@@ -115,7 +115,7 @@ namespace Umbraco.Core.Configuration
/// We will use the 'Path' (default ~/umbraco) to create it but since it cannot contain '/' and people may specify a path of ~/asdf/asdf/admin
/// we will convert the '/' to '-' and use that as the path. its a bit lame but will work.
/// </remarks>
internal static string MvcArea
internal static string UmbracoMvcArea
{
get
{

View File

@@ -1145,6 +1145,71 @@ namespace Umbraco.Core.Configuration
}
}
/// <summary>
/// Enables MVC, and at the same time disable webform masterpage templates.
/// This ensure views are automaticly created instead of masterpages.
/// Views are display in the tree instead of masterpages and a MVC template editor
/// is used instead of the masterpages editor
/// </summary>
/// <value><c>true</c> if umbraco defaults to using MVC views for templating, otherwise <c>false</c>.</value>
private static bool? _enableMvc;
public static bool EnableMvcSupport
{
get
{
if (_enableMvc == null)
{
try
{
bool enableMvc = false;
string value = GetKey("/settings/templates/enableMvcSupport");
if (value != null)
if (bool.TryParse(value, out enableMvc))
_enableMvc = enableMvc;
}
catch (Exception ex)
{
Trace.WriteLine("Could not load /settings/templates/enableMvcSupport from umbracosettings.config:\r\n {0}",
ex.Message);
_enableMvc = false;
}
}
return _enableMvc == true;
}
}
private static string[] _mvcViewExtensions;
public static string[] MvcViewExtensions
{
get
{
string[] defaultValue = "cshtml".Split(',');
if (_mvcViewExtensions == null)
{
try
{
string value = GetKey("/settings/templates/mvcViewExtensions");
if (!string.IsNullOrEmpty(value))
_mvcViewExtensions = value.Split(',');
else
_mvcViewExtensions = defaultValue;
}
catch (Exception ex)
{
Trace.WriteLine("Could not load /settings/templates/mvcViewExtensions from umbracosettings.config:\r\n {0}",
ex.Message);
_mvcViewExtensions = defaultValue;
}
}
return _mvcViewExtensions;
}
}
/// <summary>
/// Configuration regarding webservices
/// </summary>

View File

@@ -165,6 +165,23 @@ namespace Umbraco.Core.IO
return true;
}
public static bool ValidateEditPath(string filePath, string[] validDirs)
{
foreach (var dir in validDirs)
{
var validDir = dir;
if (!filePath.StartsWith(MapPath(SystemDirectories.Root)))
filePath = MapPath(filePath);
if (!validDir.StartsWith(MapPath(SystemDirectories.Root)))
validDir = MapPath(validDir);
if (filePath.StartsWith(validDir))
return true;
}
throw new FileSecurityException(String.Format("The filepath '{0}' is not within an allowed directory for this type of files", filePath.Replace(MapPath(SystemDirectories.Root), "")));
}
public static bool ValidateFileExtension(string filePath, List<string> validFileExtensions)
{
if (!filePath.StartsWith(MapPath(SystemDirectories.Root)))

View File

@@ -7,7 +7,6 @@ namespace Umbraco.Core
{
public interface IThumbnailProvider
{
int Priority { get; }
bool CanProvideThumbnail(string fileUrl);
string GetThumbnailUrl(string fileUrl);
}

107
src/Umbraco.Core/Mandate.cs Normal file
View File

@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Umbraco.Core
{
/// <summary>
/// Helper class for mandating values, for example on method parameters.
/// </summary>
internal static class Mandate
{
/// <summary>
/// Mandates that the specified parameter is not null.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="paramName">Name of the param.</param>
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is null.</exception>
public static void ParameterNotNull<T>(T value, string paramName) where T : class
{
That(value != null, () => new ArgumentNullException(paramName));
}
/// <summary>
/// Mandates that the specified parameter is not null.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="paramName">Name of the param.</param>
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is null or whitespace.</exception>
public static void ParameterNotNullOrEmpty(string value, string paramName)
{
That(!string.IsNullOrWhiteSpace(value), () => new ArgumentNullException(paramName));
}
/// <summary>
/// Mandates that the specified sequence is not null and has at least one element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sequence">The sequence.</param>
/// <param name="paramName">Name of the param.</param>
public static void ParameterNotNullOrEmpty<T>(IEnumerable<T> sequence, string paramName)
{
ParameterNotNull(sequence, paramName);
ParameterCondition(sequence.Any(), paramName);
}
/// <summary>
/// Mandates that the specified parameter matches the condition.
/// </summary>
/// <param name="condition">The condition to check.</param>
/// <param name="paramName">Name of the param.</param>
/// <exception cref="ArgumentException">If the condition is false.</exception>
public static void ParameterCondition(bool condition, string paramName)
{
ParameterCondition(condition, paramName, (string)null);
}
/// <summary>
/// Mandates that the specified parameter matches the condition.
/// </summary>
/// <param name="condition">The condition to check.</param>
/// <param name="paramName">Name of the param.</param>
/// <param name="message">The message.</param>
/// <exception cref="ArgumentException">If the condition is false.</exception>
public static void ParameterCondition(bool condition, string paramName, string message)
{
// Warning: don't make this method have an optional message parameter (removing the other ParameterCondition overload) as it will
// make binaries compiled against previous Framework libs incompatible unneccesarily
message = message ?? "A parameter passed into a method was not a valid value";
That(condition, () => new ArgumentException(message, paramName));
}
/// <summary>
/// Mandates that the specified condition is true, otherwise throws an exception specified in <typeparamref name="TException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="condition">if set to <c>true</c>, throws exception <typeparamref name="TException"/>.</param>
/// <exception cref="Exception">An exception of type <typeparamref name="TException"/> is raised if the condition is false.</exception>
public static void That<TException>(bool condition) where TException : Exception, new()
{
if (!condition)
throw ActivatorHelper.CreateInstance<TException>();
}
/// <summary>
/// Mandates that the specified condition is true, otherwise throws an exception specified in <typeparamref name="TException"/>.
/// </summary>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="condition">if set to <c>true</c>, throws exception <typeparamref name="TException"/>.</param>
/// <param name="defer">Deffered expression to call if the exception should be raised.</param>
/// <exception cref="Exception">An exception of type <typeparamref name="TException"/> is raised if the condition is false.</exception>
public static void That<TException>(bool condition, Func<TException> defer) where TException : Exception, new()
{
if (!condition)
{
throw defer.Invoke();
}
// Here is an example of how this method is actually called
//object myParam = null;
//Mandate.That(myParam != null,
// textManager => new ArgumentNullException(textManager.Get("blah", new {User = "blah"})));
}
}
}

View File

@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
namespace Umbraco.Core.ObjectResolution
{
internal abstract class ManyObjectsResolverBase<TResolver, TResolved> : ResolverBase<TResolver>
where TResolved : class
where TResolver : class
@@ -94,6 +96,40 @@ namespace Umbraco.Core.ObjectResolution
/// </summary>
protected ObjectLifetimeScope LifetimeScope { get; private set; }
private int _defaultPluginWeight = 10;
/// <summary>
/// Used in conjunction with GetSortedValues and WeightedPluginAttribute, if any of the objects
/// being resolved do not contain the WeightedPluginAttribute then this will be the default weight applied
/// to the object.
/// </summary>
protected virtual int DefaultPluginWeight
{
get { return _defaultPluginWeight; }
set { _defaultPluginWeight = value; }
}
/// <summary>
/// If a resolver requries that objects are resolved with a specific order using the WeightedPluginAttribute
/// then this method should be used instead of the Values property.
/// </summary>
/// <returns></returns>
protected IEnumerable<TResolved> GetSortedValues()
{
var vals = Values.ToList();
//ensure they are sorted
vals.Sort((f1, f2) =>
{
Func<object, int> getWeight = o =>
{
var weightAttribute = f1.GetType().GetCustomAttribute<WeightedPluginAttribute>(true);
return weightAttribute != null ? weightAttribute.Weight : DefaultPluginWeight;
};
return getWeight(f1).CompareTo(getWeight(f2));
});
return vals;
}
/// <summary>
/// Returns the list of new object instances.
/// </summary>

View File

@@ -0,0 +1,19 @@
using System;
namespace Umbraco.Core.ObjectResolution
{
/// <summary>
/// Some many object resolvers require that the objects that they resolve have weights applied to them so that
/// the objects are returned in a sorted order, this attribute is used in these scenarios.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
internal class WeightedPluginAttribute : Attribute
{
public WeightedPluginAttribute(int weight)
{
Weight = weight;
}
public int Weight { get; private set; }
}
}

View File

@@ -49,6 +49,7 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="ActivatorHelper.cs" />
<Compile Include="ApplicationContext.cs" />
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="Attempt.cs" />
@@ -89,7 +90,9 @@
<Compile Include="IO\IFileSystemExtensions.cs" />
<Compile Include="IO\IMediaFileSystem.cs" />
<Compile Include="Macros\MacroTagParser.cs" />
<Compile Include="Mandate.cs" />
<Compile Include="NameValueCollectionExtensions.cs" />
<Compile Include="ObjectResolution\WeightedPluginAttribute.cs" />
<Compile Include="PropertyEditors\DatePickerPropertyEditorValueConverter.cs" />
<Compile Include="PropertyEditors\IPropertyEditorValueConverter.cs" />
<Compile Include="Dynamics\IDynamicDocumentDataSource.cs" />