importing more stuff from Belle proj

This commit is contained in:
Shannon Deminick
2013-05-26 19:54:50 -10:00
parent 8b841e19eb
commit faae0b85cb
17 changed files with 741 additions and 298 deletions

View File

@@ -0,0 +1,13 @@
require.config("##RequireJsConfig##");
require("##RequireJsInitialize##", function (angular, myApp) {
//This function will be called when all the dependencies
//listed above are loaded. Note that this function could
//be called before the page is loaded.
//This callback is optional.
jQuery(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
});

View File

@@ -0,0 +1,33 @@
{
/*NOTE: This is actually /Belle/js because we are loading in requireJs from /Belle already*/
baseUrl: 'js',
waitSeconds: 120,
paths: {
jquery: '../lib/jquery/jquery-1.8.2.min',
jqueryCookie: '../lib/jquery/jquery.cookie',
bootstrap: '../lib/bootstrap/js/bootstrap',
underscore: '../lib/underscore/underscore',
angular: '../lib/angular/angular',
angularResource: '../lib/angular/angular-resource',
statemanager: '../lib/angular/statemanager',
text: '../lib/require/text',
async: '../lib/require/async',
namespaceMgr: '../lib/Umbraco/NamespaceManager',
myApp: '../../../Content/JavaScript/myApp'
},
shim: {
'angular' : {'exports' : 'angular'},
'angular-resource': { deps: ['angular'] },
'statemanager': { deps: ['angular'] },
'bootstrap': { deps: ['jquery'] },
'jqueryCookie': { deps: ['jquery'] },
'angular-statemanager' : {deps:['angular']},
'underscore': {exports: '_'}
},
priority: [
"angular"
],
urlArgs: 'v=1.1'
}

View File

@@ -0,0 +1,79 @@
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Manifest;
namespace Umbraco.Web.UI.JavaScript
{
/// <summary>
/// Reads from all defined manifests and ensures that any of their initialization is output with the
/// main Umbraco initialization output.
/// </summary>
internal class RequireJsInit
{
private readonly DirectoryInfo _pluginsDir;
private readonly ManifestParser _parser;
public RequireJsInit(DirectoryInfo pluginsDir)
{
_pluginsDir = pluginsDir;
_parser = new ManifestParser(_pluginsDir);
}
private static readonly Regex Token = new Regex("(\"##\\w+?##\")", RegexOptions.Compiled);
/// <summary>
/// Processes all found manifest files and outputs the main.js file containing all plugin manifests
/// </summary>
public string GetJavascriptInitialization(JObject umbracoConfig, JArray umbracoInit)
{
foreach (var m in _parser.GetManifests())
{
ManifestParser.MergeJObjects(umbracoConfig, m.JavaScriptConfig, true);
ManifestParser.MergeJArrays(umbracoInit, m.JavaScriptInitialize);
}
return ParseMain(umbracoConfig.ToString(), umbracoInit.ToString());
}
/// <summary>
/// Returns the default config as a JObject
/// </summary>
/// <returns></returns>
internal static JObject GetDefaultConfig()
{
var config = Resources.RequireJsConfig;
var jObj = JsonConvert.DeserializeObject<JObject>(config);
return jObj;
}
/// <summary>
/// Returns the default config as a JArray
/// </summary>
/// <returns></returns>
internal static JArray GetDefaultInitialization()
{
var init = Resources.RequireJsInitialize;
var jArr = JsonConvert.DeserializeObject<JArray>(init);
return jArr;
}
/// <summary>
/// Parses the JsResources.Main and replaces the replacement tokens accordingly.
/// </summary>
/// <param name="replacements"></param>
/// <returns></returns>
internal static string ParseMain(params string[] replacements)
{
var count = 0;
return Token.Replace(Resources.Main, match =>
{
var replaced = replacements[count];
count++;
return replaced;
});
}
}
}

View File

@@ -0,0 +1,7 @@
[
'angular',
'jquery',
'underscore',
'namespaceMgr',
'myApp'
]

View File

@@ -0,0 +1,138 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18034
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Umbraco.Web.UI.JavaScript {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Umbraco.Web.UI.JavaScript.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized string similar to require.config(&quot;##RequireJsConfig##&quot;);
///
///require(&quot;##RequireJsInitialize##&quot;, function (angular, myApp) {
///
/// //This function will be called when all the dependencies
/// //listed above are loaded. Note that this function could
/// //be called before the page is loaded.
/// //This callback is optional.
///
/// jQuery(document).ready(function() {
/// angular.bootstrap(document, [&apos;myApp&apos;]);
/// });
///});.
/// </summary>
internal static string Main {
get {
return ResourceManager.GetString("Main", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to {
///
/// /*NOTE: This is actually /Belle/js because we are loading in requireJs from /Belle already*/
/// baseUrl: &apos;js&apos;,
///
/// waitSeconds: 120,
/// paths: {
/// jquery: &apos;../lib/jquery/jquery-1.8.2.min&apos;,
/// jqueryCookie: &apos;../lib/jquery/jquery.cookie&apos;,
/// bootstrap: &apos;../lib/bootstrap/js/bootstrap&apos;,
/// underscore: &apos;../lib/underscore/underscore&apos;,
/// angular: &apos;../lib/angular/angular&apos;,
/// angularResource: &apos;../lib/angular/angular-resource&apos;,
/// statemanager: &apos;../ [rest of string was truncated]&quot;;.
/// </summary>
internal static string RequireJsConfig {
get {
return ResourceManager.GetString("RequireJsConfig", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to [
/// &apos;angular&apos;,
/// &apos;jquery&apos;,
/// &apos;underscore&apos;,
/// &apos;namespaceMgr&apos;,
/// &apos;myApp&apos;
///].
/// </summary>
internal static string RequireJsInitialize {
get {
return ResourceManager.GetString("RequireJsInitialize", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to //TODO: This would be nicer as an angular module so it can be injected into stuff... that&apos;d be heaps nicer, but
///// how to do that when this is not a regular JS file, it is a server side JS file and RequireJS seems to only want
///// to force load JS files ?
///
/////create the namespace (NOTE: This loads before any dependencies so we don&apos;t have a namespace mgr so we just create it manually)
///var Umbraco = {};
///Umbraco.Sys = {};
/////define a global static object
///Umbraco.Sys.ServerVariables = ##Variables## ;.
/// </summary>
internal static string ServerVariables {
get {
return ResourceManager.GetString("ServerVariables", resourceCulture);
}
}
}
}

View File

@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Main" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Main.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="RequireJsConfig" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>requirejsconfig.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="RequireJsInitialize" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>requirejsinitialize.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="ServerVariables" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>servervariables.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>

View File

@@ -0,0 +1,9 @@
//TODO: This would be nicer as an angular module so it can be injected into stuff... that'd be heaps nicer, but
// how to do that when this is not a regular JS file, it is a server side JS file and RequireJS seems to only want
// to force load JS files ?
//create the namespace (NOTE: This loads before any dependencies so we don't have a namespace mgr so we just create it manually)
var Umbraco = {};
Umbraco.Sys = {};
//define a global static object
Umbraco.Sys.ServerVariables = ##Variables## ;

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace Umbraco.Web.UI.JavaScript
{
internal class ServerVariablesParser
{
/// <summary>
/// Can allow developers to add custom variables on startup
/// </summary>
internal static EventHandler<Dictionary<string, object>> Parsing;
internal const string Token = "##Variables##";
internal static string Parse(Dictionary<string, object> items)
{
var vars = JsResources.ServerVariables;
if (Parsing != null)
{
Parsing(null, items);
}
var json = JObject.FromObject(items);
return vars.Replace(Token, json.ToString());
}
}
}

View File

@@ -292,6 +292,12 @@
<Compile Include="Cache\UserCacheRefresher.cs" />
<Compile Include="Cache\UserTypeCacheRefresher.cs" />
<Compile Include="Configuration\WebRouting.cs" />
<Compile Include="UI\JavaScript\RequireJsInit.cs" />
<Compile Include="UI\JavaScript\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Models\ContentEditing\ContentItemBase.cs" />
<Compile Include="Models\ContentEditing\ContentItemDisplay.cs" />
<Compile Include="Models\ContentEditing\ContentItemDto.cs" />
@@ -374,6 +380,7 @@
<Compile Include="Search\LuceneIndexerExtensions.cs" />
<Compile Include="Security\ValidateRequestAttempt.cs" />
<Compile Include="Security\WebSecurity.cs" />
<Compile Include="UI\JavaScript\ServerVariablesParser.cs" />
<Compile Include="umbraco.presentation\LegacyClasses.cs" />
<Compile Include="umbraco.presentation\umbraco\controls\ContentTypeControlNew.ascx.cs">
<SubType>ASPXCodeBehind</SubType>
@@ -1794,6 +1801,10 @@
<Compile Include="WebServices\UmbracoWebService.cs">
<SubType>Component</SubType>
</Compile>
<EmbeddedResource Include="UI\JavaScript\Main.js" />
<EmbeddedResource Include="UI\JavaScript\RequireJsConfig.js" />
<EmbeddedResource Include="UI\JavaScript\RequireJsInitialize.js" />
<EmbeddedResource Include="UI\JavaScript\ServerVariables.js" />
<Content Include="umbraco.presentation\umbracobase\readme.txt" />
<Content Include="umbraco.presentation\umbraco\controls\Tree\CustomTreeService.asmx" />
<Content Include="umbraco.presentation\umbraco\developer\RelationTypes\EditRelationType.aspx" />
@@ -2010,6 +2021,10 @@
<Generator>MSDiscoCodeGenerator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
<EmbeddedResource Include="UI\JavaScript\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Mvc\Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>

View File

@@ -1,103 +1,103 @@
//using System.IO;
//using System.Net;
//using System.Net.Http;
//using System.Threading.Tasks;
//using System.Web;
//using System.Web.Http;
//using System.Web.Http.Controllers;
//using System.Web.Http.ModelBinding;
//using Newtonsoft.Json;
//using Umbraco.Web.Models.ContentEditing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using Newtonsoft.Json;
using Umbraco.Web.Models.ContentEditing;
//namespace Umbraco.Web.WebApi
//{
// /// <summary>
// /// Binds the content model to the controller action for the posted multi-part Post
// /// </summary>
// internal class ContentItemBinder : IModelBinder
// {
// public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
// {
// //NOTE: Validation is done in the filter
// if (!actionContext.Request.Content.IsMimeMultipartContent())
// {
// throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// }
namespace Umbraco.Web.WebApi
{
/// <summary>
/// Binds the content model to the controller action for the posted multi-part Post
/// </summary>
internal class ContentItemBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
//NOTE: Validation is done in the filter
if (!actionContext.Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// var root = HttpContext.Current.Server.MapPath("~/App_Data/TEMP/FileUploads");
// //ensure it exists
// Directory.CreateDirectory(root);
// var provider = new MultipartFormDataStreamProvider(root);
var root = HttpContext.Current.Server.MapPath("~/App_Data/TEMP/FileUploads");
//ensure it exists
Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root);
// var task = Task.Run(() => GetModel(actionContext.Request.Content, provider))
// .ContinueWith(x =>
// {
// if (x.IsFaulted && x.Exception != null)
// {
// throw x.Exception;
// }
// bindingContext.Model = x.Result;
// });
// task.Wait();
// return bindingContext.Model != null;
// }
var task = Task.Run(() => GetModel(actionContext.Request.Content, provider))
.ContinueWith(x =>
{
if (x.IsFaulted && x.Exception != null)
{
throw x.Exception;
}
bindingContext.Model = x.Result;
});
// /// <summary>
// /// Builds the model from the request contents
// /// </summary>
// /// <param name="content"></param>
// /// <param name="provider"></param>
// /// <returns></returns>
// private async Task<ContentItemSave> GetModel(HttpContent content, MultipartFormDataStreamProvider provider)
// {
// var result = await content.ReadAsMultipartAsync(provider);
task.Wait();
// if (result.FormData["contentItem"] == null)
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter"
// });
// }
// //get the string json from the request
// var contentItem = result.FormData["contentItem"];
return bindingContext.Model != null;
}
// //transform the json into an object
// var model = JsonConvert.DeserializeObject<ContentItemSave>(contentItem);
/// <summary>
/// Builds the model from the request contents
/// </summary>
/// <param name="content"></param>
/// <param name="provider"></param>
/// <returns></returns>
private async Task<ContentItemSave> GetModel(HttpContent content, MultipartFormDataStreamProvider provider)
{
var result = await content.ReadAsMultipartAsync(provider);
// //get the files
// foreach (var file in result.FileData)
// {
// var parts = file.Headers.ContentDisposition.Name.Trim(new char[] {'\"'}).Split('_');
// if (parts.Length != 2)
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited"
// });
// }
// int propertyId;
// if (!int.TryParse(parts[1], out propertyId))
// {
// throw new HttpResponseException(
// new HttpResponseMessage(HttpStatusCode.BadRequest)
// {
// ReasonPhrase = "The request was not formatted correctly the file name's 2nd part must be an integer"
// });
// }
// model.UploadedFiles.Add(new ContentItemFile
// {
// FilePath = file.LocalFileName,
// PropertyId = propertyId
// });
// }
if (result.FormData["contentItem"] == null)
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter"
});
}
// return model;
// }
// }
//}
//get the string json from the request
var contentItem = result.FormData["contentItem"];
//transform the json into an object
var model = JsonConvert.DeserializeObject<ContentItemSave>(contentItem);
//get the files
foreach (var file in result.FileData)
{
var parts = file.Headers.ContentDisposition.Name.Trim(new char[] { '\"' }).Split('_');
if (parts.Length != 2)
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited"
});
}
int propertyId;
if (!int.TryParse(parts[1], out propertyId))
{
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.BadRequest)
{
ReasonPhrase = "The request was not formatted correctly the file name's 2nd part must be an integer"
});
}
model.UploadedFiles.Add(new ContentItemFile
{
FilePath = file.LocalFileName,
PropertyId = propertyId
});
}
return model;
}
}
}

View File

@@ -1,150 +1,180 @@
//using System.Linq;
//using System.Net;
//using System.Net.Http;
//using System.Web.Http.Controllers;
//using System.Web.Http.Filters;
//using Umbraco.Core.PropertyEditors;
//using Umbraco.Web.Models.ContentEditing;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Models.ContentEditing;
//namespace Umbraco.Web.WebApi.Filters
//{
// /// <summary>
// /// Validates the content item
// /// </summary>
// /// <remarks>
// /// There's various validation happening here both value validation and structure validation
// /// to ensure that malicious folks are not trying to post invalid values or to invalid properties.
// /// </remarks>
// internal class ContentItemValidationFilterAttribute : ActionFilterAttribute
// {
// /// <summary>
// /// Returns true so that other filters can execute along with this one
// /// </summary>
// public override bool AllowMultiple
// {
// get { return true; }
// }
namespace Umbraco.Web.WebApi.Filters
{
/// <summary>
/// Validates the content item
/// </summary>
/// <remarks>
/// There's various validation happening here both value validation and structure validation
/// to ensure that malicious folks are not trying to post invalid values or to invalid properties.
/// </remarks>
internal class ContentItemValidationFilterAttribute : ActionFilterAttribute
{
private readonly ApplicationContext _applicationContext;
// /// <summary>
// /// Performs the validation
// /// </summary>
// /// <param name="actionContext"></param>
// public override void OnActionExecuting(HttpActionContext actionContext)
// {
// var contentItem = actionContext.ActionArguments["contentItem"] as ContentItemSave;
// if (contentItem == null)
// {
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No " + typeof(ContentItemSave) + " found in request");
// return;
// }
public ContentItemValidationFilterAttribute(ApplicationContext applicationContext)
{
_applicationContext = applicationContext;
}
// //now do each validation step
// ContentItemDto existingContent;
// if (!ValidateExistingContent(contentItem, actionContext, out existingContent)) return;
// if (!ValidateProperties(contentItem, existingContent, actionContext)) return;
// if (!ValidateData(contentItem, existingContent, actionContext)) return;
// }
public ContentItemValidationFilterAttribute()
: this(ApplicationContext.Current)
{
}
// /// <summary>
// /// Ensure the content exists
// /// </summary>
// /// <param name="postedItem"></param>
// /// <param name="actionContext"></param>
// /// <param name="found"></param>
// /// <returns></returns>
// private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
// {
// //TODO: We need to of course change this to the real umbraco api
// found = TestContentService.GetContentItem(postedItem.Id);
// if (found == null)
// {
// var message = string.Format("content with id: {0} was not found", postedItem.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
// return true;
// }
/// <summary>
/// Returns true so that other filters can execute along with this one
/// </summary>
public override bool AllowMultiple
{
get { return true; }
}
// /// <summary>
// /// Ensure all of the ids in the post are valid
// /// </summary>
// /// <param name="postedItem"></param>
// /// <param name="actionContext"></param>
// /// <param name="realItem"></param>
// /// <returns></returns>
// private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
// {
// foreach (var p in postedItem.Properties)
// {
// //ensure the property actually exists in our server side properties
// if (!realItem.Properties.Contains(p))
// {
// //TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
// //save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.
/// <summary>
/// Performs the validation
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(HttpActionContext actionContext)
{
var contentItem = actionContext.ActionArguments["contentItem"] as ContentItemSave;
if (contentItem == null)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No " + typeof(ContentItemSave) + " found in request");
return;
}
// var message = string.Format("property with id: {0} was not found", p.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
// }
// return true;
// }
//now do each validation step
ContentItemDto existingContent;
if (!ValidateExistingContent(contentItem, actionContext, out existingContent)) return;
if (!ValidateProperties(contentItem, existingContent, actionContext)) return;
if (!ValidateData(contentItem, existingContent, actionContext)) return;
}
// //TODO: Validate the property type data
// private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
// {
// foreach (var p in realItem.Properties)
// {
// var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
// if (editor == null)
// {
// var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
// return false;
// }
/// <summary>
/// Ensure the content exists
/// </summary>
/// <param name="postedItem"></param>
/// <param name="actionContext"></param>
/// <param name="found"></param>
/// <returns></returns>
private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
{
var item = _applicationContext.Services.ContentService.GetById(postedItem.Id);
if (item == null)
{
var message = string.Format("content with id: {0} was not found", postedItem.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
found = null;
return false;
}
// //get the posted value for this property
// var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;
//TODO: Convert this over to a mapping engine, just need people to vote on podio for my question about that.
found = new ContentItemDto
{
Id = item.Id,
Properties = item.Properties.Select(p => new ContentPropertyDto
{
Alias = p.Alias,
Description = p.PropertyType.Description,
Label = p.PropertyType.Name,
Id = p.Id,
DataType = _applicationContext.Services.DataTypeService.GetDataTypeDefinitionById(p.PropertyType.DataTypeDefinitionId)
}).ToList()
};
return true;
}
// //get the pre-values for this property
// var preValues = TestContentService.GetPreValue(p.DataType.Id);
/// <summary>
/// Ensure all of the ids in the post are valid
/// </summary>
/// <param name="postedItem"></param>
/// <param name="actionContext"></param>
/// <param name="realItem"></param>
/// <returns></returns>
private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
{
foreach (var p in postedItem.Properties)
{
//ensure the property actually exists in our server side properties
if (!realItem.Properties.Contains(p))
{
//TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
//save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.
// //TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
// // * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
// // * Or, pass in the overridden values stored with the doc type property separately
var message = string.Format("property with id: {0} was not found", p.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
return false;
}
}
return true;
}
// foreach (var v in editor.ValueEditor.Validators)
// {
// foreach (var result in v.Validate(postedValue, preValues, editor))
// {
// //if there are no member names supplied then we assume that the validation message is for the overall property
// // not a sub field on the property editor
// if (!result.MemberNames.Any())
// {
// //add a model state error for the entire property
// actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
// }
// else
// {
// //there's assigned field names so we'll combine the field name with the property name
// // so that we can try to match it up to a real sub field of this editor
// foreach (var field in result.MemberNames)
// {
// actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
// }
// }
// }
// }
// }
//TODO: Validate the property type data
private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
{
foreach (var p in realItem.Properties)
{
var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
if (editor == null)
{
var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
return false;
}
// //create the response if there any errors
// if (!actionContext.ModelState.IsValid)
// {
// actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
// }
//get the posted value for this property
var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;
// return actionContext.ModelState.IsValid;
// }
//get the pre-values for this property
var preValues = _applicationContext.Services.DataTypeService.GetPreValueAsString(p.DataType.Id);
// }
//}
//TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
// * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
// * Or, pass in the overridden values stored with the doc type property separately
foreach (var v in editor.ValueEditor.Validators)
{
foreach (var result in v.Validate(postedValue, preValues, editor))
{
//if there are no member names supplied then we assume that the validation message is for the overall property
// not a sub field on the property editor
if (!result.MemberNames.Any())
{
//add a model state error for the entire property
actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
}
else
{
//there's assigned field names so we'll combine the field name with the property name
// so that we can try to match it up to a real sub field of this editor
foreach (var field in result.MemberNames)
{
actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
}
}
}
}
}
//create the response if there any errors
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
}
return actionContext.ModelState.IsValid;
}
}
}