Port v7@2aa0dfb2c5 - WIP
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@@ -23,9 +22,7 @@ using Umbraco.Web.Models.Mapping;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.WebApi;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web.Http.Controllers;
|
||||
using Examine;
|
||||
using Umbraco.Web.WebApi.Binders;
|
||||
using Umbraco.Web.WebApi.Filters;
|
||||
using Constants = Umbraco.Core.Constants;
|
||||
@@ -78,7 +75,7 @@ namespace Umbraco.Web.Editors
|
||||
}
|
||||
|
||||
var emptyContent = Services.MediaService.CreateMedia("", parentId, contentType.Alias, Security.GetUserId().ResultOr(0));
|
||||
var mapped = Mapper.Map<IMedia, MediaItemDisplay>(emptyContent);
|
||||
var mapped = ContextMapper.Map<IMedia, MediaItemDisplay>(emptyContent, UmbracoContext);
|
||||
|
||||
//remove this tab if it exists: umbContainerView
|
||||
var containerTab = mapped.Tabs.FirstOrDefault(x => x.Alias == Constants.Conventions.PropertyGroups.ListViewGroupName);
|
||||
@@ -126,7 +123,7 @@ namespace Umbraco.Web.Editors
|
||||
//HandleContentNotFound will throw an exception
|
||||
return null;
|
||||
}
|
||||
return Mapper.Map<IMedia, MediaItemDisplay>(foundContent);
|
||||
return ContextMapper.Map<IMedia, MediaItemDisplay>(foundContent, UmbracoContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -146,7 +143,7 @@ namespace Umbraco.Web.Editors
|
||||
//HandleContentNotFound will throw an exception
|
||||
return null;
|
||||
}
|
||||
return Mapper.Map<IMedia, MediaItemDisplay>(foundContent);
|
||||
return ContextMapper.Map<IMedia, MediaItemDisplay>(foundContent, UmbracoContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -175,7 +172,7 @@ namespace Umbraco.Web.Editors
|
||||
public IEnumerable<MediaItemDisplay> GetByIds([FromUri]int[] ids)
|
||||
{
|
||||
var foundMedia = Services.MediaService.GetByIds(ids);
|
||||
return foundMedia.Select(Mapper.Map<IMedia, MediaItemDisplay>);
|
||||
return foundMedia.Select(media => ContextMapper.Map<IMedia, MediaItemDisplay>(media, UmbracoContext));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -461,6 +458,7 @@ namespace Umbraco.Web.Editors
|
||||
/// <returns></returns>
|
||||
[FileUploadCleanupFilter]
|
||||
[MediaPostValidate]
|
||||
[OutgoingEditorModelEvent]
|
||||
public MediaItemDisplay PostSave(
|
||||
[ModelBinder(typeof(MediaItemBinder))]
|
||||
MediaItemSave contentItem)
|
||||
@@ -487,7 +485,7 @@ namespace Umbraco.Web.Editors
|
||||
{
|
||||
//ok, so the absolute mandatory data is invalid and it's new, we cannot actually continue!
|
||||
// add the modelstate to the outgoing object and throw validation response
|
||||
var forDisplay = Mapper.Map<IMedia, MediaItemDisplay>(contentItem.PersistedContent);
|
||||
var forDisplay = ContextMapper.Map<IMedia, MediaItemDisplay>(contentItem.PersistedContent, UmbracoContext);
|
||||
forDisplay.Errors = ModelState.ToErrorDictionary();
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(forDisplay));
|
||||
}
|
||||
@@ -497,7 +495,7 @@ namespace Umbraco.Web.Editors
|
||||
var saveStatus = Services.MediaService.WithResult().Save(contentItem.PersistedContent, (int)Security.CurrentUser.Id);
|
||||
|
||||
//return the updated model
|
||||
var display = Mapper.Map<IMedia, MediaItemDisplay>(contentItem.PersistedContent);
|
||||
var display = AutoMapperExtensions.MapWithUmbracoContext<IMedia, MediaItemDisplay>(contentItem.PersistedContent, UmbracoContext);
|
||||
|
||||
//lasty, if it is not valid, add the modelstate to the outgoing object and throw a 403
|
||||
HandleInvalidModelState(display);
|
||||
@@ -596,15 +594,17 @@ namespace Umbraco.Web.Editors
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[EnsureUserPermissionForMedia("folder.ParentId")]
|
||||
public MediaItemDisplay PostAddFolder(EntityBasic folder)
|
||||
|
||||
public MediaItemDisplay PostAddFolder(PostedFolder folder)
|
||||
{
|
||||
var intParentId = GetParentIdAsInt(folder.ParentId, validatePermissions:true);
|
||||
|
||||
var mediaService = Services.MediaService;
|
||||
var f = mediaService.CreateMedia(folder.Name, folder.ParentId, Constants.Conventions.MediaTypes.Folder);
|
||||
|
||||
var f = mediaService.CreateMedia(folder.Name, intParentId, Constants.Conventions.MediaTypes.Folder);
|
||||
mediaService.Save(f, Security.CurrentUser.Id);
|
||||
|
||||
return Mapper.Map<IMedia, MediaItemDisplay>(f);
|
||||
return ContextMapper.Map<IMedia, MediaItemDisplay>(f, UmbracoContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -636,63 +636,12 @@ namespace Umbraco.Web.Editors
|
||||
}
|
||||
|
||||
//get the string json from the request
|
||||
int parentId; bool entityFound; GuidUdi parentUdi;
|
||||
string currentFolderId = result.FormData["currentFolder"];
|
||||
// test for udi
|
||||
if (GuidUdi.TryParse(currentFolderId, out parentUdi))
|
||||
{
|
||||
currentFolderId = parentUdi.Guid.ToString();
|
||||
}
|
||||
|
||||
if (int.TryParse(currentFolderId, out parentId) == false)
|
||||
{
|
||||
// if a guid then try to look up the entity
|
||||
Guid idGuid;
|
||||
if (Guid.TryParse(currentFolderId, out idGuid))
|
||||
{
|
||||
var entity = Services.EntityService.Get(idGuid);
|
||||
if (entity != null)
|
||||
{
|
||||
entityFound = true;
|
||||
parentId = entity.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new EntityNotFoundException(currentFolderId, "The passed id doesn't exist");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Request.CreateValidationErrorResponse("The request was not formatted correctly, the currentFolder is not an integer or Guid");
|
||||
}
|
||||
|
||||
if (entityFound == false)
|
||||
{
|
||||
return Request.CreateValidationErrorResponse("The request was not formatted correctly, the currentFolder is not an integer or Guid");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//ensure the user has access to this folder by parent id!
|
||||
if (CheckPermissions(
|
||||
new Dictionary<string, object>(),
|
||||
Security.CurrentUser,
|
||||
Services.MediaService,
|
||||
Services.EntityService,
|
||||
parentId) == false)
|
||||
{
|
||||
return Request.CreateResponse(
|
||||
HttpStatusCode.Forbidden,
|
||||
new SimpleNotificationModel(new Notification(
|
||||
Services.TextService.Localize("speechBubbles/operationFailedHeader"),
|
||||
Services.TextService.Localize("speechBubbles/invalidUserPermissionsText"),
|
||||
SpeechBubbleIcon.Warning)));
|
||||
}
|
||||
|
||||
int parentId = GetParentIdAsInt(currentFolderId, validatePermissions: true);
|
||||
|
||||
var tempFiles = new PostedFiles();
|
||||
var mediaService = Services.MediaService;
|
||||
|
||||
|
||||
var mediaService = ApplicationContext.Services.MediaService;
|
||||
|
||||
//in case we pass a path with a folder in it, we will create it and upload media to it.
|
||||
if (result.FormData.ContainsKey("path"))
|
||||
{
|
||||
@@ -827,6 +776,69 @@ namespace Umbraco.Web.Editors
|
||||
return Request.CreateResponse(HttpStatusCode.OK, tempFiles);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given a parent id which could be a GUID, UDI or an INT, this will resolve the INT
|
||||
/// </summary>
|
||||
/// <param name="parentId"></param>
|
||||
/// <param name="validatePermissions">
|
||||
/// If true, this will check if the current user has access to the resolved integer parent id
|
||||
/// and if that check fails an unauthorized exception will occur
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
private int GetParentIdAsInt(string parentId, bool validatePermissions)
|
||||
{
|
||||
int intParentId;
|
||||
GuidUdi parentUdi;
|
||||
|
||||
// test for udi
|
||||
if (GuidUdi.TryParse(parentId, out parentUdi))
|
||||
{
|
||||
parentId = parentUdi.Guid.ToString();
|
||||
}
|
||||
|
||||
//if it's not an INT then we'll check for GUID
|
||||
if (int.TryParse(parentId, out intParentId) == false)
|
||||
{
|
||||
// if a guid then try to look up the entity
|
||||
Guid idGuid;
|
||||
if (Guid.TryParse(parentId, out idGuid))
|
||||
{
|
||||
var entity = Services.EntityService.Get(idGuid);
|
||||
if (entity != null)
|
||||
{
|
||||
intParentId = entity.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new EntityNotFoundException(parentId, "The passed id doesn't exist");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HttpResponseException(
|
||||
Request.CreateValidationErrorResponse("The request was not formatted correctly, the parentId is not an integer, Guid or UDI"));
|
||||
}
|
||||
}
|
||||
|
||||
//ensure the user has access to this folder by parent id!
|
||||
if (validatePermissions && CheckPermissions(
|
||||
new Dictionary<string, object>(),
|
||||
Security.CurrentUser,
|
||||
Services.MediaService,
|
||||
Services.EntityService,
|
||||
intParentId) == false)
|
||||
{
|
||||
throw new HttpResponseException(Request.CreateResponse(
|
||||
HttpStatusCode.Forbidden,
|
||||
new SimpleNotificationModel(new Notification(
|
||||
Services.TextService.Localize("speechBubbles/operationFailedHeader"),
|
||||
Services.TextService.Localize("speechBubbles/invalidUserPermissionsText"),
|
||||
SpeechBubbleIcon.Warning))));
|
||||
}
|
||||
|
||||
return intParentId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures the item can be moved/copied to the new location
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user