diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index a1e4754bf2..bc19ee4ad2 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -366,55 +366,86 @@ namespace Umbraco.Web.Editors /// /// Change the sort order for media /// - /// + /// /// [EnsureUserPermissionForContent("move.ParentId", 'M')] - public HttpResponseMessage PostMove(ContentMove move) + public HttpResponseMessage PostMove(MoveOrCopy move) { - if (move == null) - { - return Request.CreateResponse(HttpStatusCode.NotFound); - } + var toMove = ValidateMoveOrCopy(move); - var contentService = Services.ContentService; - try - { - contentService.Move(contentService.GetById(move.Id), move.ParentId); - return Request.CreateResponse(HttpStatusCode.OK); - } - catch (Exception ex) - { - LogHelper.Error("Could not move content", ex); - throw; - } + Services.ContentService.Move(toMove, move.ParentId); + + return Request.CreateResponse(HttpStatusCode.OK); } /// /// Copies a /// - /// + /// /// [EnsureUserPermissionForContent("copy.ParentId", 'C')] - public HttpResponseMessage PostCopy(ContentMove copy) + public HttpResponseMessage PostCopy(MoveOrCopy copy) { - if (copy == null) + var toCopy = ValidateMoveOrCopy(copy); + + Services.ContentService.Copy(toCopy, copy.ParentId, copy.RelateToOriginal); + + return Request.CreateResponse(HttpStatusCode.OK); + } + + /// + /// Ensures the item can be moved/copied to the new location + /// + /// + /// + private IContent ValidateMoveOrCopy(MoveOrCopy model) + { + if (model == null) { - return Request.CreateResponse(HttpStatusCode.NotFound); + throw new HttpResponseException(HttpStatusCode.NotFound); } var contentService = Services.ContentService; - try + var toMove = contentService.GetById(model.Id); + if (toMove == null) { - contentService.Copy(contentService.GetById(copy.Id), copy.ParentId, true); - return Request.CreateResponse(HttpStatusCode.OK); + throw new HttpResponseException(HttpStatusCode.NotFound); } - catch (Exception ex) + if (model.ParentId < 0) { - LogHelper.Error("Could not copy content", ex); - throw; + //cannot move if the content item is not allowed at the root + if (toMove.ContentType.AllowedAsRoot == false) + { + throw new HttpResponseException( + Request.CreateValidationErrorResponse(ui.Text("moveOrCopy", "notAllowedAtRoot", Security.CurrentUser))); + } } - } + else + { + var parent = contentService.GetById(model.ParentId); + if (parent == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + //check if the item is allowed under this one + if (parent.ContentType.AllowedContentTypes.Select(x => x.Id).ToArray() + .Any(x => x.Value == toMove.ContentType.Id) == false) + { + throw new HttpResponseException( + Request.CreateValidationErrorResponse(ui.Text("moveOrCopy", "notAllowedByContentType", Security.CurrentUser))); + } + + // Check on paths + if ((string.Format(",{0},", parent.Path)).IndexOf(string.Format(",{0},", toMove.Id), StringComparison.Ordinal) > -1) + { + throw new HttpResponseException( + Request.CreateValidationErrorResponse(ui.Text("moveOrCopy", "notAllowedByPath", Security.CurrentUser))); + } + } + + return toMove; + } private void ShowMessageForStatus(PublishStatus status, ContentItemDisplay display) { diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentMove.cs b/src/Umbraco.Web/Models/ContentEditing/MoveOrCopy.cs similarity index 62% rename from src/Umbraco.Web/Models/ContentEditing/ContentMove.cs rename to src/Umbraco.Web/Models/ContentEditing/MoveOrCopy.cs index 7b1e209504..6aa87f9e74 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentMove.cs +++ b/src/Umbraco.Web/Models/ContentEditing/MoveOrCopy.cs @@ -8,10 +8,10 @@ using System.Text; namespace Umbraco.Web.Models.ContentEditing { /// - /// A model representing a new sort order for a content/media item + /// A model representing a model for moving or copying /// [DataContract(Name = "content", Namespace = "")] - public class ContentMove + public class MoveOrCopy { /// /// The Id of the node to move or copy to @@ -21,11 +21,18 @@ namespace Umbraco.Web.Models.ContentEditing public int ParentId { get; set; } /// - /// ´The id of the node to move or copy + /// The id of the node to move or copy /// [DataMember(Name = "id", IsRequired = true)] [Required] public int Id { get; set; } + + /// + /// Boolean indicating whether copying the object should create a relation to it's original + /// + [DataMember(Name = "relateToOriginal", IsRequired = true)] + [Required] + public bool RelateToOriginal { get; set; } } } diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 729585950c..0c085bfcab 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -313,7 +313,7 @@ - +