Fixes issue with the delivery api can find unpublished content
This commit is contained in:
Bjarke Berg
2024-11-04 12:27:47 +01:00
committed by GitHub
parent e4d99747d1
commit 6417948a58
5 changed files with 37 additions and 12 deletions

View File

@@ -177,7 +177,7 @@ public class NewDefaultUrlProvider : IUrlProvider
UrlMode mode,
string? culture)
{
if (string.IsNullOrWhiteSpace(route))
if (string.IsNullOrWhiteSpace(route) || route.Equals("#"))
{
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{

View File

@@ -426,21 +426,26 @@ public class DocumentUrlService : IDocumentUrlService
private bool IsContentPublished(Guid contentKey, string culture) => _publishStatusQueryService.IsDocumentPublished(contentKey, culture);
public string GetLegacyRouteFormat(Guid docuemntKey, string? culture, bool isDraft)
public string GetLegacyRouteFormat(Guid documentKey, string? culture, bool isDraft)
{
Attempt<int> documentIdAttempt = _idKeyMap.GetIdForKey(docuemntKey, UmbracoObjectTypes.Document);
Attempt<int> documentIdAttempt = _idKeyMap.GetIdForKey(documentKey, UmbracoObjectTypes.Document);
if(documentIdAttempt.Success is false)
{
return "#";
}
if (_documentNavigationQueryService.TryGetAncestorsOrSelfKeys(docuemntKey,
if (_documentNavigationQueryService.TryGetAncestorsOrSelfKeys(documentKey,
out IEnumerable<Guid> ancestorsOrSelfKeys) is false)
{
return "#";
}
if(isDraft is false && culture != null && _publishStatusQueryService.IsDocumentPublished(documentKey, culture) is false)
{
return "#";
}
var cultureOrDefault = string.IsNullOrWhiteSpace(culture) is false ? culture : _languageService.GetDefaultIsoCodeAsync().GetAwaiter().GetResult();
Guid[] ancestorsOrSelfKeysArray = ancestorsOrSelfKeys as Guid[] ?? ancestorsOrSelfKeys.ToArray();

View File

@@ -3,9 +3,9 @@ using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Navigation;
using Umbraco.Cms.Infrastructure.HybridCache.Services;
using Umbraco.Extensions;
namespace Umbraco.Cms.Infrastructure.HybridCache;
@@ -95,7 +95,7 @@ public sealed class DocumentCache : IPublishedContentCache
public IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType)
=> _documentCacheService.GetByContentType(contentType);
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
public IPublishedContent? GetByRoute(bool preview, string route, bool? hideTopLevelNode = null, string? culture = null)
{
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
@@ -103,7 +103,7 @@ public sealed class DocumentCache : IPublishedContentCache
return key is not null ? GetById(preview, key.Value) : null;
}
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
public IPublishedContent? GetByRoute(string route, bool? hideTopLevelNode = null, string? culture = null)
{
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
@@ -111,14 +111,15 @@ public sealed class DocumentCache : IPublishedContentCache
return key is not null ? GetById(key.Value) : null;
}
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
public string? GetRouteById(bool preview, int contentId, string? culture = null)
{
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
IPublishedUrlProvider publishedUrlProvider = StaticServiceProvider.Instance.GetRequiredService<IPublishedUrlProvider>();
IPublishedContent? content = GetById(preview, contentId);
return content is not null ? documentUrlService.GetLegacyRouteFormat(content.Key, culture, preview) : null;
return content is not null ? publishedUrlProvider.GetUrl(content, UrlMode.Relative, culture) : null;
}
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
public string? GetRouteById(int contentId, string? culture = null) => GetRouteById(false, contentId, culture);
}

View File

@@ -270,11 +270,17 @@ internal sealed class DocumentCacheService : IDocumentCacheService
await _databaseCacheRepository.RefreshContentAsync(draftCacheNode, content.PublishedState);
if (content.PublishedState == PublishedState.Publishing)
if (content.PublishedState == PublishedState.Publishing || content.PublishedState == PublishedState.Unpublishing)
{
var publishedCacheNode = _cacheNodeFactory.ToContentCacheNode(content, false);
await _databaseCacheRepository.RefreshContentAsync(publishedCacheNode, content.PublishedState);
if (content.PublishedState == PublishedState.Unpublishing)
{
await _hybridCache.RemoveAsync(GetCacheKey(publishedCacheNode.Key, false));
}
}
scope.Complete();

View File

@@ -83,6 +83,19 @@ public class DocumentHybridCacheTests : UmbracoIntegrationTestWithContentEditing
Assert.IsFalse(textPage.IsPublished());
}
[Test]
public async Task Cannot_get_unpublished_content()
{
// Arrange
var unpublishAttempt = await ContentPublishingService.UnpublishAsync(PublishedTextPage.Key.Value, null, Constants.Security.SuperUserKey);
//Act
var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, false);
// Assert
Assert.IsNull(textPage);
}
[Test]
public async Task Can_Get_Draft_Of_Published_Content_By_Key()
{