-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Delivery API: Adding allow list for content types #21111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
312c4ef
Adding allow content type alias settings and validator
NillasKA da2cfd0
Creating private helper method for tests
NillasKA 0de2194
Revisiting logic for disallow types
NillasKA d25d847
Adding tests for validator
NillasKA 605fe4b
Obsolete unnecessary methods and overloads.
AndyButland 4a412cd
Fix warning and update naming in tests.
AndyButland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Umbraco.Core/Configuration/Models/Validation/DeliveryApiSettingsValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) Umbraco. | ||
| // See LICENSE for more details. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Core.Configuration.Models.Validation; | ||
|
|
||
| /// <summary> | ||
| /// Validator for configuration represented as <see cref="DeliveryApiSettings" />. | ||
| /// </summary> | ||
| public class DeliveryApiSettingsValidator : IValidateOptions<DeliveryApiSettings> | ||
| { | ||
| private readonly ILogger<DeliveryApiSettingsValidator> _logger; | ||
|
|
||
| public DeliveryApiSettingsValidator(ILogger<DeliveryApiSettingsValidator> logger) | ||
| => _logger = logger; | ||
|
|
||
| /// <inheritdoc /> | ||
| public ValidateOptionsResult Validate(string? name, DeliveryApiSettings options) | ||
| { | ||
| ValidateContentTypeAliasOverlap(options); | ||
|
|
||
| return ValidateOptionsResult.Success; | ||
| } | ||
|
|
||
| private void ValidateContentTypeAliasOverlap(DeliveryApiSettings options) | ||
| { | ||
| if (options.AllowedContentTypeAliases.Count == 0 || options.DisallowedContentTypeAliases.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var overlappingAliases = options.AllowedContentTypeAliases | ||
| .Where(alias => options.DisallowedContentTypeAliases.InvariantContains(alias)) | ||
| .ToArray(); | ||
|
|
||
| if (overlappingAliases.Length > 0) | ||
| { | ||
| _logger.LogWarning( | ||
| "Delivery API configuration contains content type aliases that appear in both AllowedContentTypeAliases and DisallowedContentTypeAliases: {Aliases}. " + | ||
| "The allow list takes precedence, so these content types will be allowed. Consider removing them from the disallow list to avoid confusion.", | ||
| string.Join(", ", overlappingAliases)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 32 additions & 3 deletions
35
src/Umbraco.Core/Extensions/DeliveryApiSettingsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,48 @@ | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| namespace Umbraco.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for determining whether content types or content items are allowed to be exposed through | ||
| /// the Delivery API based on the configured allow and disallow lists. | ||
| /// </summary> | ||
| public static class DeliveryApiSettingsExtensions | ||
| { | ||
| [Obsolete("Please use the overload of IsAllowedContentType taking a content type alias. Scheduled for removal in Umbraco 19.")] | ||
| public static bool IsAllowedContentType(this DeliveryApiSettings settings, IPublishedContent content) | ||
| => settings.IsAllowedContentType(content.ContentType.Alias); | ||
|
|
||
| [Obsolete("Please use IsAllowedContentType and negate the result instead. Scheduled for removal in Umbraco 19.")] | ||
| public static bool IsDisallowedContentType(this DeliveryApiSettings settings, IPublishedContent content) | ||
| => settings.IsDisallowedContentType(content.ContentType.Alias); | ||
|
|
||
| /// <summary> | ||
| /// Determines whether a content type alias is allowed to be exposed through the Delivery API. | ||
| /// </summary> | ||
| /// <param name="settings">The Delivery API settings.</param> | ||
| /// <param name="contentTypeAlias">The content type alias to check.</param> | ||
| /// <returns> | ||
| /// <c>true</c> if the content type is allowed; otherwise, <c>false</c>. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// If the allow list is configured (non-empty), only content types in the allow list are permitted. | ||
| /// The allow list takes precedence - if a content type is in both allow and disallow lists, it is allowed. | ||
| /// If the allow list is empty, all content types are allowed except those in the disallow list. | ||
| /// </remarks> | ||
| public static bool IsAllowedContentType(this DeliveryApiSettings settings, string contentTypeAlias) | ||
| => settings.IsDisallowedContentType(contentTypeAlias) is false; | ||
| { | ||
| // If allow list is configured, it takes precedence. | ||
| if (settings.AllowedContentTypeAliases.Count > 0) | ||
| { | ||
| return settings.AllowedContentTypeAliases.InvariantContains(contentTypeAlias); | ||
| } | ||
|
|
||
| // Otherwise the content type is allowed if it's not in the disallow list. | ||
| return settings.DisallowedContentTypeAliases.InvariantContains(contentTypeAlias) is false; | ||
| } | ||
|
|
||
| [Obsolete("Please use IsAllowedContentType and negate the result instead. Scheduled for removal in Umbraco 19.")] | ||
| public static bool IsDisallowedContentType(this DeliveryApiSettings settings, string contentTypeAlias) | ||
|
NillasKA marked this conversation as resolved.
|
||
| => settings.DisallowedContentTypeAliases.InvariantContains(contentTypeAlias); | ||
| => settings.IsAllowedContentType(contentTypeAlias) is false; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...itTests/Umbraco.Core/Configuration/Models/Validation/DeliveryApiSettingsValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Umbraco. | ||
| // See LICENSE for more details. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Configuration.Models.Validation; | ||
|
|
||
| namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Configuration.Models.Validation; | ||
|
|
||
| [TestFixture] | ||
| public class DeliveryApiSettingsValidatorTests | ||
| { | ||
| private Mock<ILogger<DeliveryApiSettingsValidator>> _loggerMock; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() => _loggerMock = new Mock<ILogger<DeliveryApiSettingsValidator>>(); | ||
|
|
||
| [Test] | ||
| public void Returns_Success_For_Configuration_With_Only_AllowList() | ||
| { | ||
| var validator = new DeliveryApiSettingsValidator(_loggerMock.Object); | ||
| var options = new DeliveryApiSettings | ||
| { | ||
| AllowedContentTypeAliases = new HashSet<string> { "content1", "content2" }, | ||
| }; | ||
|
|
||
| ValidateOptionsResult result = validator.Validate("settings", options); | ||
|
|
||
| Assert.IsTrue(result.Succeeded); | ||
| VerifyNoWarningLogged(); | ||
| } | ||
|
Check warning on line 34 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/Configuration/Models/Validation/DeliveryApiSettingsValidatorTests.cs
|
||
|
|
||
| [Test] | ||
| public void Returns_Success_For_Configuration_With_Only_DisallowList() | ||
| { | ||
| var validator = new DeliveryApiSettingsValidator(_loggerMock.Object); | ||
| var options = new DeliveryApiSettings | ||
| { | ||
| DisallowedContentTypeAliases = new HashSet<string> { "content1", "content2" }, | ||
| }; | ||
|
|
||
| ValidateOptionsResult result = validator.Validate("settings", options); | ||
|
|
||
| Assert.IsTrue(result.Succeeded); | ||
| VerifyNoWarningLogged(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Returns_Success_For_Configuration_With_No_Overlapping_Lists() | ||
| { | ||
| var validator = new DeliveryApiSettingsValidator(_loggerMock.Object); | ||
| var options = new DeliveryApiSettings | ||
| { | ||
| AllowedContentTypeAliases = new HashSet<string> { "content1", "content2" }, | ||
| DisallowedContentTypeAliases = new HashSet<string> { "content3", "content4" }, | ||
| }; | ||
|
|
||
| ValidateOptionsResult result = validator.Validate("settings", options); | ||
|
|
||
| Assert.IsTrue(result.Succeeded); | ||
| VerifyNoWarningLogged(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Returns_Success_But_Logs_Warning_For_Overlapping_Lists() | ||
| { | ||
| var validator = new DeliveryApiSettingsValidator(_loggerMock.Object); | ||
| var options = new DeliveryApiSettings | ||
| { | ||
| AllowedContentTypeAliases = new HashSet<string> { "content1", "content2" }, | ||
| DisallowedContentTypeAliases = new HashSet<string> { "content1", "content3" }, | ||
| }; | ||
|
|
||
| ValidateOptionsResult result = validator.Validate("settings", options); | ||
|
|
||
| Assert.IsTrue(result.Succeeded); | ||
|
|
||
| VerifyWarningLogged(); | ||
| } | ||
|
|
||
| private void VerifyWarningLogged() | ||
| { | ||
| var warningCount = GetWarningLogCount(); | ||
| Assert.AreEqual(1, warningCount); | ||
| } | ||
|
|
||
| private void VerifyNoWarningLogged() | ||
| { | ||
| var warningCount = GetWarningLogCount(); | ||
| Assert.AreEqual(0, warningCount); | ||
| } | ||
|
|
||
| private int GetWarningLogCount() => | ||
| _loggerMock.Invocations | ||
| .Count(invocation => | ||
| invocation.Method.Name == nameof(ILogger.Log) && | ||
| invocation.Arguments.OfType<LogLevel>().Any(level => level == LogLevel.Warning)); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.