diff --git a/src/Umbraco.Web/Editors/EntityController.cs b/src/Umbraco.Web/Editors/EntityController.cs index 747e0c3713..8b97fbc551 100644 --- a/src/Umbraco.Web/Editors/EntityController.cs +++ b/src/Umbraco.Web/Editors/EntityController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.ModelBinding; @@ -21,14 +22,14 @@ using Examine.SearchCriteria; namespace Umbraco.Web.Editors { - /// - /// API controller to deal with Macro data - /// - [PluginController("UmbracoApi")] - public class MacroController : UmbracoAuthorizedJsonController - { - - } + ///// + ///// API controller to deal with Macro data + ///// + //[PluginController("UmbracoApi")] + //public class MacroController : UmbracoAuthorizedJsonController + //{ + // public EntityBasic Get + //} /// /// The API controller used for getting entity objects, basic name, icon, id representation of umbraco objects that are based on CMSNode @@ -39,167 +40,293 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] public class EntityController : UmbracoAuthorizedJsonController { - [EnsureUserPermissionForContent("id")] - [UmbracoApplicationAuthorize(Constants.Applications.Content)] - public EntityBasic GetDocumentById(int id) + //[EnsureUserPermissionForContent("id")] + //[UmbracoApplicationAuthorize(Constants.Applications.Content)] + //public EntityBasic GetDocumentById(int id) + //{ + // return Mapper.Map(Services.EntityService.Get(id, UmbracoObjectTypes.Document)); + //} + + //[EnsureUserPermissionForContent("id")] + //[UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] + //[FilterAllowedOutgoingContent(typeof(IEnumerable))] + //public IEnumerable GetDocumentChildren(int id) + //{ + // return GetChildren(id, UmbracoObjectTypes.Document); + //} + + //[FilterAllowedOutgoingContent(typeof(IEnumerable))] + //[UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] + //public IEnumerable GetDocumentsByIds([FromUri]int[] ids) + //{ + // if (ids == null) throw new ArgumentNullException("ids"); + // return GetEntitiesById(ids, UmbracoObjectTypes.Document); + //} + + //[FilterAllowedOutgoingContent(typeof(IEnumerable))] + //[UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] + //public IEnumerable SearchDocuments([FromUri]string query) + //{ + // var internalSearcher = ExamineManager.Instance.SearchProviderCollection[Constants.Examine.InternalSearcher]; + // var criteria = internalSearcher.CreateSearchCriteria("content", BooleanOperation.Or); + // var fields = new[] { "id", "__nodeName", "bodyText" }; + // var term = new[] { query.ToLower().Escape() }; + // var operation = criteria.GroupedOr(fields, term).Compile(); + + // var results = internalSearcher.Search(operation) + // .Select(x => int.Parse(x["id"])); + + // return GetDocumentsByIds(results.ToArray()); + //} + + ///// + ///// The user must have access to either content or media for this to return data + ///// + ///// + ///// + //[UmbracoApplicationAuthorizeAttribute( + // Constants.Applications.Media, + // Constants.Applications.Content)] + //[EnsureUserPermissionForMedia("id")] + //public EntityBasic GetMediaById(int id) + //{ + // return GetEntityById(id, UmbracoObjectTypes.Media); + //} + + ///// + ///// The user must have access to either content or media for this to return data + ///// + ///// + ///// + //[UmbracoApplicationAuthorizeAttribute( + // Constants.Applications.Media, + // Constants.Applications.Content)] + //[EnsureUserPermissionForMedia("id")] + //[FilterAllowedOutgoingMedia(typeof(IEnumerable))] + //public IEnumerable GetMediaChildren(int id) + //{ + // return GetChildren(id, UmbracoObjectTypes.Media); + //} + + + ///// + ///// The user must have access to either content or media for this to return data + ///// + ///// + ///// + //[UmbracoApplicationAuthorizeAttribute( + // Constants.Applications.Media, + // Constants.Applications.Content)] + //[EnsureUserPermissionForMedia("id")] + //[FilterAllowedOutgoingMedia(typeof(IEnumerable))] + //public IEnumerable SearchMedia([FromUri]string query) + //{ + // var internalSearcher = ExamineManager.Instance.SearchProviderCollection[Constants.Examine.InternalSearcher]; + // var criteria = internalSearcher.CreateSearchCriteria("media", BooleanOperation.Or); + // var fields = new[] { "id", "__nodeName"}; + // var term = new[] { query.ToLower().Escape() }; + // var operation = criteria.GroupedOr(fields, term).Compile(); + + // var results = internalSearcher.Search(operation) + // .Select(x => int.Parse(x["id"])); + + // return GetMediaByIds(results.ToArray()); + //} + + ///// + ///// The user must have access to either content or media for this to return data + ///// + ///// + ///// + //[UmbracoApplicationAuthorizeAttribute( + // Constants.Applications.Media, + // Constants.Applications.Content)] + //[FilterAllowedOutgoingMedia(typeof(IEnumerable))] + //public IEnumerable GetMediaByIds([FromUri]int[] ids) + //{ + // if (ids == null) throw new ArgumentNullException("ids"); + // return GetEntitiesById(ids, UmbracoObjectTypes.Media); + //} + + public EntityBasic GetById(int id, UmbracoEntityTypes type) { - return Mapper.Map(Services.EntityService.Get(id, UmbracoObjectTypes.Document)); + return GetResultForId(id, type); } - [EnsureUserPermissionForContent("id")] - [UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] - [FilterAllowedOutgoingContent(typeof(IEnumerable))] - public IEnumerable GetDocumentChildren(int id) + public IEnumerable GetByIds([FromUri]int[] ids, UmbracoEntityTypes type) { - return GetChildren(id, UmbracoObjectTypes.Document); + if (ids == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + return GetResultForIds(ids, type); } - [FilterAllowedOutgoingContent(typeof(IEnumerable))] - [UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] - public IEnumerable GetDocumentsByIds([FromUri]int[] ids) + public IEnumerable GetChildren(int id, UmbracoEntityTypes type) { - if (ids == null) throw new ArgumentNullException("ids"); - return GetEntitiesById(ids, UmbracoObjectTypes.Document); + return GetResultForChildren(id, type); } - [FilterAllowedOutgoingContent(typeof(IEnumerable))] - [UmbracoApplicationAuthorizeAttribute(Constants.Applications.Content)] - public IEnumerable SearchDocuments([FromUri]string query) + public IEnumerable GetAncestors(int id, UmbracoEntityTypes type) { - var internalSearcher = ExamineManager.Instance.SearchProviderCollection[Constants.Examine.InternalSearcher]; - var criteria = internalSearcher.CreateSearchCriteria("content", BooleanOperation.Or); - var fields = new[] { "id", "__nodeName", "bodyText" }; - var term = new[] { query.ToLower().Escape() }; - var operation = criteria.GroupedOr(fields, term).Compile(); - - var results = internalSearcher.Search(operation) - .Select(x => int.Parse(x["id"])); - - return GetDocumentsByIds(results.ToArray()); + return GetResultForAncestors(id, type); } - /// - /// The user must have access to either content or media for this to return data - /// - /// - /// - [UmbracoApplicationAuthorizeAttribute( - Constants.Applications.Media, - Constants.Applications.Content)] - [EnsureUserPermissionForMedia("id")] - public EntityBasic GetMediaById(int id) + public IEnumerable GetAll(UmbracoEntityTypes type) { - return GetEntityById(id, UmbracoObjectTypes.Media); + return GetResultForAll(type); } - /// - /// The user must have access to either content or media for this to return data - /// - /// - /// - [UmbracoApplicationAuthorizeAttribute( - Constants.Applications.Media, - Constants.Applications.Content)] - [EnsureUserPermissionForMedia("id")] - [FilterAllowedOutgoingMedia(typeof(IEnumerable))] - public IEnumerable GetMediaChildren(int id) + private IEnumerable GetResultForChildren(int id, UmbracoEntityTypes entityType) { - return GetChildren(id, UmbracoObjectTypes.Media); + var objectType = ConvertToObjectType(entityType); + if (objectType.HasValue) + { + //TODO: Need to check for Object types that support heirarchy here, some might not. + + return Services.EntityService.GetChildren(id).Select(Mapper.Map); + } + //now we need to convert the unknown ones + switch (entityType) + { + case UmbracoEntityTypes.Domain: + + case UmbracoEntityTypes.Language: + + case UmbracoEntityTypes.User: + + case UmbracoEntityTypes.Macro: + + default: + throw new NotSupportedException("The " + typeof(EntityController) + " does not currently support data for the type " + entityType); + } } - - /// - /// The user must have access to either content or media for this to return data - /// - /// - /// - [UmbracoApplicationAuthorizeAttribute( - Constants.Applications.Media, - Constants.Applications.Content)] - [EnsureUserPermissionForMedia("id")] - [FilterAllowedOutgoingMedia(typeof(IEnumerable))] - public IEnumerable SearchMedia([FromUri]string query) + private IEnumerable GetResultForAncestors(int id, UmbracoEntityTypes entityType) { - var internalSearcher = ExamineManager.Instance.SearchProviderCollection[Constants.Examine.InternalSearcher]; - var criteria = internalSearcher.CreateSearchCriteria("media", BooleanOperation.Or); - var fields = new[] { "id", "__nodeName"}; - var term = new[] { query.ToLower().Escape() }; - var operation = criteria.GroupedOr(fields, term).Compile(); + var objectType = ConvertToObjectType(entityType); + if (objectType.HasValue) + { + //TODO: Need to check for Object types that support heirarchy here, some might not. - var results = internalSearcher.Search(operation) - .Select(x => int.Parse(x["id"])); + var ids = Services.EntityService.Get(id).Path.Split(',').Select(int.Parse); + return ids.Select(m => Mapper.Map(Services.EntityService.Get(m))); + } + //now we need to convert the unknown ones + switch (entityType) + { + case UmbracoEntityTypes.Domain: - return GetMediaByIds(results.ToArray()); + case UmbracoEntityTypes.Language: + + case UmbracoEntityTypes.User: + + case UmbracoEntityTypes.Macro: + + default: + throw new NotSupportedException("The " + typeof(EntityController) + " does not currently support data for the type " + entityType); + } } - /// - /// The user must have access to either content or media for this to return data - /// - /// - /// - [UmbracoApplicationAuthorizeAttribute( - Constants.Applications.Media, - Constants.Applications.Content)] - [FilterAllowedOutgoingMedia(typeof(IEnumerable))] - public IEnumerable GetMediaByIds([FromUri]int[] ids) + private IEnumerable GetResultForAll(UmbracoEntityTypes entityType) { - if (ids == null) throw new ArgumentNullException("ids"); - return GetEntitiesById(ids, UmbracoObjectTypes.Media); + var objectType = ConvertToObjectType(entityType); + if (objectType.HasValue) + { + return Services.EntityService.GetAll(objectType.Value).Select(Mapper.Map); + } + //now we need to convert the unknown ones + switch (entityType) + { + case UmbracoEntityTypes.Domain: + + case UmbracoEntityTypes.Language: + + case UmbracoEntityTypes.User: + + case UmbracoEntityTypes.Macro: + + default: + throw new NotSupportedException("The " + typeof(EntityController) + " does not currently support data for the type " + entityType); + } } - //TODO: Need to add app level security for all of this below - - public EntityBasic GetById(int id, UmbracoObjectTypes? type = null) + private IEnumerable GetResultForIds(IEnumerable ids, UmbracoEntityTypes entityType) { - return type == null - ? Mapper.Map(Services.EntityService.Get(id)) - : GetEntityById(id, type.Value); + var objectType = ConvertToObjectType(entityType); + if (objectType.HasValue) + { + return ids.Select(id => Mapper.Map(Services.EntityService.Get(id))); + } + //now we need to convert the unknown ones + switch (entityType) + { + case UmbracoEntityTypes.Domain: + + case UmbracoEntityTypes.Language: + + case UmbracoEntityTypes.User: + + case UmbracoEntityTypes.Macro: + + default: + throw new NotSupportedException("The " + typeof(EntityController) + " does not currently support data for the type " + entityType); + } } - public IEnumerable GetByIds([FromUri]int[] ids, UmbracoObjectTypes? type = null) + private EntityBasic GetResultForId(int id, UmbracoEntityTypes entityType) { - if (ids == null) throw new ArgumentNullException("ids"); - - return type == null - ? ids.Select(id => Mapper.Map(Services.EntityService.Get(id))) - : GetEntitiesById(ids, type.Value); + var objectType = ConvertToObjectType(entityType); + if (objectType.HasValue) + { + return Mapper.Map(Services.EntityService.Get(id, objectType.Value)); + } + //now we need to convert the unknown ones + switch (entityType) + { + case UmbracoEntityTypes.Domain: + + case UmbracoEntityTypes.Language: + + case UmbracoEntityTypes.User: + + case UmbracoEntityTypes.Macro: + + default: + throw new NotSupportedException("The " + typeof(EntityController) + " does not currently support data for the type " + entityType); + } } - public IEnumerable GetChildren(int id, UmbracoObjectTypes? type = null) + private static UmbracoObjectTypes? ConvertToObjectType(UmbracoEntityTypes entityType) { - return type == null - ? Services.EntityService.GetChildren(id).Select(Mapper.Map) - : GetChildren(id, type.Value); - } - - public IEnumerable GetAncestors(int id, string type = null) - { - var ids = Services.EntityService.Get(id).Path.Split(',').Select(int.Parse); - - return string.IsNullOrEmpty(type) - ? ids.Select(m => Mapper.Map(Services.EntityService.Get(m))) - : GetEntitiesById(ids.ToArray(), (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes),type) ); - } - - public IEnumerable GetAll(UmbracoObjectTypes type = UmbracoObjectTypes.Document) - { - return Services.EntityService.GetAll(type).Select(Mapper.Map); - } - - private EntityBasic GetEntityById(int id, UmbracoObjectTypes type) - { - return Mapper.Map(Services.EntityService.Get(id, type)); - } - - private IEnumerable GetChildren(int id, UmbracoObjectTypes type) - { - return Services.EntityService.GetChildren(id, type).Select(Mapper.Map); - } - - private IEnumerable GetEntitiesById(IEnumerable ids, UmbracoObjectTypes type) - { - if (ids == null) throw new ArgumentNullException("ids"); - return ids.Select(id => Mapper.Map(Services.EntityService.Get(id, type))); + switch (entityType) + { + case UmbracoEntityTypes.Document: + return UmbracoObjectTypes.Document; + case UmbracoEntityTypes.Media: + return UmbracoObjectTypes.Media; + case UmbracoEntityTypes.MemberType: + return UmbracoObjectTypes.MediaType; + case UmbracoEntityTypes.Template: + return UmbracoObjectTypes.Template; + case UmbracoEntityTypes.MemberGroup: + return UmbracoObjectTypes.MemberGroup; + case UmbracoEntityTypes.ContentItem: + return UmbracoObjectTypes.ContentItem; + case UmbracoEntityTypes.MediaType: + return UmbracoObjectTypes.MediaType; + case UmbracoEntityTypes.DocumentType: + return UmbracoObjectTypes.DocumentType; + case UmbracoEntityTypes.Stylesheet: + return UmbracoObjectTypes.Stylesheet; + case UmbracoEntityTypes.Member: + return UmbracoObjectTypes.Member; + case UmbracoEntityTypes.DataType: + return UmbracoObjectTypes.DataType; + default: + //There is no UmbracoEntity conversion (things like Macros, Users, etc...) + return null; + } } } diff --git a/src/Umbraco.Web/Models/ContentEditing/UmbracoEntityTypes.cs b/src/Umbraco.Web/Models/ContentEditing/UmbracoEntityTypes.cs new file mode 100644 index 0000000000..f6d98cfbbb --- /dev/null +++ b/src/Umbraco.Web/Models/ContentEditing/UmbracoEntityTypes.cs @@ -0,0 +1,83 @@ +namespace Umbraco.Web.Models.ContentEditing +{ + /// + /// Represents the type's of Umbraco entities that can be resolved from the EntityController + /// + public enum UmbracoEntityTypes + { + /// + /// Domain + /// + Domain, + + /// + /// Language + /// + Language, + + /// + /// User + /// + User, + + /// + /// Macro + /// + Macro, + + /// + /// Document + /// + Document, + + /// + /// Media + /// + Media, + + /// + /// Member Type + /// + MemberType, + + /// + /// Template + /// + Template, + + /// + /// Member Group + /// + MemberGroup, + + /// + /// Content Item + /// + ContentItem, + + /// + /// "Media Type + /// + MediaType, + + /// + /// Document Type + /// + DocumentType, + + /// + /// Stylesheet + /// + Stylesheet, + + /// + /// Member + /// + Member, + + /// + /// Data Type + /// + DataType + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 44800e4adc..1136e982ef 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -303,6 +303,7 @@ +