Skip to content

Commit 6417948

Browse files
authored
Fixes issue with the delivery api can find unpublished content
1 parent e4d9974 commit 6417948

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

src/Umbraco.Core/Routing/NewDefaultUrlProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private string GetLegacyRouteFormatById(Guid key, string? culture)
177177
UrlMode mode,
178178
string? culture)
179179
{
180-
if (string.IsNullOrWhiteSpace(route))
180+
if (string.IsNullOrWhiteSpace(route) || route.Equals("#"))
181181
{
182182
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
183183
{

src/Umbraco.Core/Services/DocumentUrlService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,21 +426,26 @@ public async Task DeleteUrlsFromCacheAsync(IEnumerable<Guid> documentKeysEnumera
426426

427427
private bool IsContentPublished(Guid contentKey, string culture) => _publishStatusQueryService.IsDocumentPublished(contentKey, culture);
428428

429-
public string GetLegacyRouteFormat(Guid docuemntKey, string? culture, bool isDraft)
429+
public string GetLegacyRouteFormat(Guid documentKey, string? culture, bool isDraft)
430430
{
431-
Attempt<int> documentIdAttempt = _idKeyMap.GetIdForKey(docuemntKey, UmbracoObjectTypes.Document);
431+
Attempt<int> documentIdAttempt = _idKeyMap.GetIdForKey(documentKey, UmbracoObjectTypes.Document);
432432

433433
if(documentIdAttempt.Success is false)
434434
{
435435
return "#";
436436
}
437437

438-
if (_documentNavigationQueryService.TryGetAncestorsOrSelfKeys(docuemntKey,
438+
if (_documentNavigationQueryService.TryGetAncestorsOrSelfKeys(documentKey,
439439
out IEnumerable<Guid> ancestorsOrSelfKeys) is false)
440440
{
441441
return "#";
442442
}
443443

444+
if(isDraft is false && culture != null && _publishStatusQueryService.IsDocumentPublished(documentKey, culture) is false)
445+
{
446+
return "#";
447+
}
448+
444449
var cultureOrDefault = string.IsNullOrWhiteSpace(culture) is false ? culture : _languageService.GetDefaultIsoCodeAsync().GetAwaiter().GetResult();
445450

446451
Guid[] ancestorsOrSelfKeysArray = ancestorsOrSelfKeys as Guid[] ?? ancestorsOrSelfKeys.ToArray();

src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using Umbraco.Cms.Core.DependencyInjection;
44
using Umbraco.Cms.Core.Models.PublishedContent;
55
using Umbraco.Cms.Core.PublishedCache;
6+
using Umbraco.Cms.Core.Routing;
67
using Umbraco.Cms.Core.Services;
78
using Umbraco.Cms.Core.Services.Navigation;
8-
using Umbraco.Cms.Infrastructure.HybridCache.Services;
99
using Umbraco.Extensions;
1010

1111
namespace Umbraco.Cms.Infrastructure.HybridCache;
@@ -95,30 +95,31 @@ public IEnumerable<IPublishedContent> GetAtRoot(string? culture = null)
9595
public IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType)
9696
=> _documentCacheService.GetByContentType(contentType);
9797

98-
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
98+
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
9999
public IPublishedContent? GetByRoute(bool preview, string route, bool? hideTopLevelNode = null, string? culture = null)
100100
{
101101
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
102102
Guid? key = documentUrlService.GetDocumentKeyByRoute(route, culture, null, preview);
103103
return key is not null ? GetById(preview, key.Value) : null;
104104
}
105105

106-
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
106+
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
107107
public IPublishedContent? GetByRoute(string route, bool? hideTopLevelNode = null, string? culture = null)
108108
{
109109
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
110110
Guid? key = documentUrlService.GetDocumentKeyByRoute(route, culture, null, false);
111111
return key is not null ? GetById(key.Value) : null;
112112
}
113113

114-
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
114+
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
115115
public string? GetRouteById(bool preview, int contentId, string? culture = null)
116116
{
117-
IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>();
117+
IPublishedUrlProvider publishedUrlProvider = StaticServiceProvider.Instance.GetRequiredService<IPublishedUrlProvider>();
118118
IPublishedContent? content = GetById(preview, contentId);
119-
return content is not null ? documentUrlService.GetLegacyRouteFormat(content.Key, culture, preview) : null;
119+
120+
return content is not null ? publishedUrlProvider.GetUrl(content, UrlMode.Relative, culture) : null;
120121
}
121122

122-
[Obsolete("Use IDocumentUrlService.GetDocumentKeyByRoute instead, scheduled for removal in v17")]
123+
[Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")]
123124
public string? GetRouteById(int contentId, string? culture = null) => GetRouteById(false, contentId, culture);
124125
}

src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,17 @@ public async Task RefreshContentAsync(IContent content)
270270

271271
await _databaseCacheRepository.RefreshContentAsync(draftCacheNode, content.PublishedState);
272272

273-
if (content.PublishedState == PublishedState.Publishing)
273+
if (content.PublishedState == PublishedState.Publishing || content.PublishedState == PublishedState.Unpublishing)
274274
{
275275
var publishedCacheNode = _cacheNodeFactory.ToContentCacheNode(content, false);
276276

277277
await _databaseCacheRepository.RefreshContentAsync(publishedCacheNode, content.PublishedState);
278+
279+
if (content.PublishedState == PublishedState.Unpublishing)
280+
{
281+
await _hybridCache.RemoveAsync(GetCacheKey(publishedCacheNode.Key, false));
282+
}
283+
278284
}
279285

280286
scope.Complete();

tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ public async Task Can_Get_Draft_Of_Published_Content_By_Id()
8383
Assert.IsFalse(textPage.IsPublished());
8484
}
8585

86+
[Test]
87+
public async Task Cannot_get_unpublished_content()
88+
{
89+
// Arrange
90+
var unpublishAttempt = await ContentPublishingService.UnpublishAsync(PublishedTextPage.Key.Value, null, Constants.Security.SuperUserKey);
91+
92+
//Act
93+
var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, false);
94+
95+
// Assert
96+
Assert.IsNull(textPage);
97+
}
98+
8699
[Test]
87100
public async Task Can_Get_Draft_Of_Published_Content_By_Key()
88101
{

0 commit comments

Comments
 (0)