Add support for GUIDs in media and content pickers

This commit is contained in:
Niels Hartvig
2017-01-09 14:45:40 +01:00
parent 6a374cbfa6
commit 1a9f336a59
6 changed files with 137 additions and 21 deletions

View File

@@ -96,7 +96,7 @@ namespace Umbraco.Web.Trees
LogHelper.Warn<ContentTreeControllerBase>("The user " + Security.CurrentUser.Username + " does not have access to the tree node " + id);
return new TreeNodeCollection();
}
// So there's an alt id specified, it's not the root node and the user has access to it, great! But there's one thing we
// need to consider:
// If the tree is being rendered in a dialog view we want to render only the children of the specified id, but
@@ -110,7 +110,7 @@ namespace Umbraco.Web.Trees
id = Constants.System.Root.ToString(CultureInfo.InvariantCulture);
}
}
var entities = GetChildEntities(id);
nodes.AddRange(entities.Select(entity => GetSingleTreeNode(entity, id, queryStrings)).Where(node => node != null));
return nodes;
@@ -122,11 +122,24 @@ namespace Umbraco.Web.Trees
protected IEnumerable<IUmbracoEntity> GetChildEntities(string id)
{
// use helper method to ensure we support both integer and guid lookups
int iid;
if (int.TryParse(id, out iid) == false)
// if it's the root node, we won't use the look up
if (id != "-1")
{
throw new InvalidCastException("The id for the media tree must be an integer");
var idEntity = GetEntityFromId(id);
if (idEntity == null)
{
throw new EntityNotFoundException(id, "The passed id doesn't exist");
}
iid = idEntity.Id;
}
else
{
iid = int.Parse(id);
}
//if a request is made for the root node data but the user's start node is not the default, then
// we need to return their start node data
@@ -134,11 +147,12 @@ namespace Umbraco.Web.Trees
{
//just return their single start node, it will show up under the 'Content' label
var startNode = Services.EntityService.Get(UserStartNode, UmbracoObjectType);
if (startNode == null)
if (startNode != null)
return new[] { startNode };
else
{
throw new EntityNotFoundException(UserStartNode, "User's start content node could not be found");
}
return new[] { startNode };
}
return Services.EntityService.GetChildren(iid, UmbracoObjectType).ToArray();
@@ -176,9 +190,9 @@ namespace Umbraco.Web.Trees
{
id = altStartId;
}
var nodes = GetTreeNodesInternal(id, queryStrings);
//only render the recycle bin if we are not in dialog and the start id id still the root
if (IsDialog(queryStrings) == false && id == Constants.System.Root.ToInvariantString())
{
@@ -210,8 +224,9 @@ namespace Umbraco.Web.Trees
/// </remarks>
private TreeNodeCollection GetTreeNodesInternal(string id, FormDataCollection queryStrings)
{
IUmbracoEntity current = GetEntityFromId(id);
//before we get the children we need to see if this is a container node
var current = Services.EntityService.Get(int.Parse(id), UmbracoObjectType);
//test if the parent is a listview / container
if (current != null && current.IsContainer())
@@ -286,5 +301,32 @@ namespace Umbraco.Web.Trees
{
return allowedUserOptions.Select(x => x.Action).OfType<ActionBrowse>().Any();
}
/// <summary>
/// Get an entity via an id that can be either an integer or a Guid
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
internal IUmbracoEntity GetEntityFromId(string id)
{
IUmbracoEntity entity;
Guid idGuid = Guid.Empty;
int idInt;
if (Guid.TryParse(id, out idGuid))
{
entity = Services.EntityService.GetByKey(idGuid, UmbracoObjectType);
}
else if (int.TryParse(id, out idInt))
{
entity = Services.EntityService.Get(idInt, UmbracoObjectType);
}
else
{
throw new InvalidCastException("Id must be either an integer or a Guid");
}
return entity;
}
}
}