Added a null check to IUmbracoHelperAccessor & IPublishedSnapshotAccessor

This commit is contained in:
Zeegaan
2021-08-10 10:55:29 +02:00
parent 3eb32a97a7
commit 5d264fefdb
28 changed files with 173 additions and 66 deletions

View File

@@ -1,7 +1,9 @@
using Umbraco.Cms.Infrastructure;
namespace Umbraco.Cms.Core
{
public interface IPublishedContentQueryAccessor
{
IPublishedContentQuery PublishedContentQuery { get;}
bool TryGetValue(out IPublishedContentQuery publishedContentQuery);
}
}

View File

@@ -32,15 +32,18 @@ namespace Umbraco.Cms.Infrastructure.ModelsBuilder
public static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor, PublishedItemType itemType, string alias)
{
var facade = publishedSnapshotAccessor.PublishedSnapshot;
if (!publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
switch (itemType)
{
case PublishedItemType.Content:
return facade.Content.GetContentType(alias);
return publishedSnapshot.Content.GetContentType(alias);
case PublishedItemType.Media:
return facade.Media.GetContentType(alias);
return publishedSnapshot.Media.GetContentType(alias);
case PublishedItemType.Member:
return facade.Members.GetContentType(alias);
return publishedSnapshot.Members.GetContentType(alias);
default:
throw new ArgumentOutOfRangeException(nameof(itemType));
}

View File

@@ -95,13 +95,16 @@ namespace Umbraco.Cms.Core.PropertyEditors
{
continue;
}
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
if (entity is IDocumentEntitySlim documentEntity)
{
icon = documentEntity.ContentTypeIcon;
published = culture == null ? documentEntity.Published : documentEntity.PublishedCultures.Contains(culture);
udi = new GuidUdi(Constants.UdiEntityType.Document, documentEntity.Key);
url = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#";
url = publishedSnapshot.Content.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#";
trashed = documentEntity.Trashed;
}
else if(entity is IContentEntitySlim contentEntity)
@@ -109,7 +112,7 @@ namespace Umbraco.Cms.Core.PropertyEditors
icon = contentEntity.ContentTypeIcon;
published = !contentEntity.Trashed;
udi = new GuidUdi(Constants.UdiEntityType.Media, contentEntity.Key);
url = _publishedSnapshotAccessor.PublishedSnapshot.Media.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#";
url = publishedSnapshot.Media.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#";
trashed = contentEntity.Trashed;
}
else

View File

@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
@@ -26,8 +26,12 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
BlockItemData data,
PropertyCacheLevel referenceCacheLevel, bool preview)
{
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
// hack! we need to cast, we have no choice beacuse we cannot make breaking changes.
var publishedContentCache = _publishedSnapshotAccessor.PublishedSnapshot.Content;
var publishedContentCache = publishedSnapshot.Content;
// only convert element types - content types will cause an exception when PublishedModelFactory creates the model
var publishedContentType = publishedContentCache.GetContentType(data.ContentTypeKey);

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;
@@ -63,10 +63,13 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
var mediaItems = new List<MediaWithCrops>();
var dtos = MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.Deserialize(_jsonSerializer, inter);
var configuration = propertyType.DataType.ConfigurationAs<MediaPicker3Configuration>();
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
foreach (var dto in dtos)
{
var mediaItem = _publishedSnapshotAccessor.PublishedSnapshot.Media.GetById(preview, dto.MediaKey);
var mediaItem = publishedSnapshot.Media.GetById(preview, dto.MediaKey);
if (mediaItem != null)
{
var localCrops = new ImageCropperValue

View File

@@ -58,7 +58,10 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
var links = new List<Link>();
var dtos = _jsonSerializer.Deserialize<IEnumerable<MultiUrlPickerValueEditor.LinkDto>>(inter.ToString());
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
foreach (var dto in dtos)
{
var type = LinkType.External;
@@ -71,8 +74,8 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
: LinkType.Content;
var content = type == LinkType.Media ?
_publishedSnapshotAccessor.PublishedSnapshot.Media.GetById(preview, dto.Udi.Guid) :
_publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(preview, dto.Udi.Guid);
publishedSnapshot.Media.GetById(preview, dto.Udi.Guid) :
publishedSnapshot.Content.GetById(preview, dto.Udi.Guid);
if (content == null || content.ContentType.ItemType == PublishedItemType.Element)
{

View File

@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
@@ -46,9 +46,12 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
var elementTypeAlias = sourceObject[NestedContentPropertyEditor.ContentTypeAliasPropertyKey]?.ToObject<string>();
if (string.IsNullOrEmpty(elementTypeAlias))
return null;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
// only convert element types - content types will cause an exception when PublishedModelFactory creates the model
var publishedContentType = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetContentType(elementTypeAlias);
var publishedContentType = publishedSnapshot.Content.GetContentType(elementTypeAlias);
if (publishedContentType == null || publishedContentType.IsElement == false)
return null;

View File

@@ -1,5 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Infrastructure;
namespace Umbraco.Cms.Core
{
@@ -7,11 +8,13 @@ namespace Umbraco.Cms.Core
{
private readonly IServiceProvider _serviceProvider;
public PublishedContentQueryAccessor(IServiceProvider serviceProvider)
public PublishedContentQueryAccessor(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
public bool TryGetValue(out IPublishedContentQuery publishedContentQuery)
{
_serviceProvider = serviceProvider;
publishedContentQuery = _serviceProvider.GetRequiredService<IPublishedContentQuery>();
return publishedContentQuery is not null;
}
public IPublishedContentQuery PublishedContentQuery => _serviceProvider.GetRequiredService<IPublishedContentQuery>();
}
}

View File

@@ -87,7 +87,11 @@ namespace Umbraco.Cms.Core.Routing
private void StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
{
var contentCache = _publishedSnapshotAccessor.PublishedSnapshot.Content;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
var contentCache = publishedSnapshot.Content;
var entityContent = contentCache.GetById(entity.Id);
if (entityContent == null)
return;
@@ -112,7 +116,11 @@ namespace Umbraco.Cms.Core.Routing
private void CreateRedirects(OldRoutesDictionary oldRoutes)
{
var contentCache = _publishedSnapshotAccessor.PublishedSnapshot.Content;
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
var contentCache = publishedSnapshot.Content;
foreach (var oldRoute in oldRoutes)
{

View File

@@ -624,7 +624,11 @@ namespace Umbraco.Cms.Core.Security
{
return null;
}
return _publishedSnapshotAccessor.PublishedSnapshot?.Members.Get(member);
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
return publishedSnapshot.Members.Get(member);
}
private enum MemberDataChangeType