Skip to content

Commit

Permalink
Refactor setting arrays to sets
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldbarendse committed Apr 15, 2024
1 parent 2d850bf commit e18c6c8
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ await memberApplicationManager.EnsureMemberApplicationAsync(
cancellationToken);
}

private bool ValidateRedirectUrls(Uri[] redirectUrls)
private bool ValidateRedirectUrls(IEnumerable<Uri> redirectUrls)
{
if (redirectUrls.Any() is false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Asp.Versioning;
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -82,6 +82,5 @@ public async Task<IActionResult> Get(
return Ok(PagedViewModel<HelpPageResponseModel>.Empty());
}

private bool IsAllowedUrl(string? url) =>
_helpPageSettings.HelpPageUrlAllowList is null || _helpPageSettings.HelpPageUrlAllowList.Contains(url);
private bool IsAllowedUrl(string? url) => url is null || _helpPageSettings.HelpPageUrlAllowList.Contains(url);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Api.Management.ViewModels.TemporaryFile;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Media;
Expand All @@ -22,8 +22,8 @@ public TemporaryFileConfigurationResponseModel Create() =>
new()
{
ImageFileTypes = _imageUrlGenerator.SupportedImageFileTypes.ToArray(),
DisallowedUploadedFilesExtensions = _contentSettings.DisallowedUploadedFileExtensions,
AllowedUploadedFileExtensions = _contentSettings.AllowedUploadedFileExtensions,
DisallowedUploadedFilesExtensions = _contentSettings.DisallowedUploadedFileExtensions.ToArray(),
AllowedUploadedFileExtensions = _contentSettings.AllowedUploadedFileExtensions.ToArray(),
MaxFileSize = _runtimeSettings.MaxRequestLength,
};
}
30 changes: 16 additions & 14 deletions src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ namespace Umbraco.Extensions;
public static class ContentSettingsExtensions
{
/// <summary>
/// Determines if file extension is allowed for upload based on (optional) white list and black list
/// held in settings.
/// Allow upload if extension is whitelisted OR if there is no whitelist and extension is NOT blacklisted.
/// Determines if file extension is allowed for upload based on (optional) allow list and deny list held in settings.
/// Disallowed file extensions are only considered if there are no allowed file extensions.
/// </summary>
public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension) =>
contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) ||
(contentSettings.AllowedUploadedFileExtensions.Any() == false &&
contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) == false);
/// <param name="contentSettings">The content settings.</param>
/// <param name="extension">The file extension.</param>
/// <returns>
/// <c>true</c> if the file extension is allowed for upload; otherwise, <c>false</c>.
/// </returns>
public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension)
=> contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) ||
(contentSettings.AllowedUploadedFileExtensions.Any() == false && contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) == false);

/// <summary>
/// Gets the auto-fill configuration for a specified property alias.
/// Gets the auto-fill configuration for a specified property alias.
/// </summary>
/// <param name="contentSettings"></param>
/// <param name="contentSettings">The content settings.</param>
/// <param name="propertyTypeAlias">The property type alias.</param>
/// <returns>The auto-fill configuration for the specified property alias, or null.</returns>
/// <returns>
/// The auto-fill configuration for the specified property alias or <c>null</c> if not configured.
/// </returns>
public static ImagingAutoFillUploadField? GetConfig(this ContentSettings contentSettings, string propertyTypeAlias)
{
ImagingAutoFillUploadField[] autoFillConfigs = contentSettings.Imaging.AutoFillImageProperties;
return autoFillConfigs?.FirstOrDefault(x => x.Alias == propertyTypeAlias);
}
=> contentSettings.Imaging.AutoFillImageProperties.FirstOrDefault(x => x.Alias == propertyTypeAlias);
}
8 changes: 4 additions & 4 deletions src/Umbraco.Core/Configuration/Models/BasicAuthSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public class BasicAuthSettings
[DefaultValue(StaticEnabled)]
public bool Enabled { get; set; } = StaticEnabled;

public ISet<string> AllowedIPs { get; set; } = new HashSet<string>();

public string[] AllowedIPs { get; set; } = Array.Empty<string>();
public SharedSecret SharedSecret { get; set; } = new SharedSecret();
public SharedSecret SharedSecret { get; set; } = new();

public bool RedirectToLoginPage { get; set; } = false;

}

public class SharedSecret
{
private const string StaticHeaderName = "X-Authentication-Shared-Secret";
private const string StaticHeaderName = "X-Authentication-Shared-Secret";

[DefaultValue(StaticHeaderName)]
public string? HeaderName { get; set; } = StaticHeaderName;

public string? Value { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ public class ContentDashboardSettings
/// <summary>
/// Gets the allowed addresses to retrieve data for the content dashboard.
/// </summary>
/// <value>The URLs.</value>
public string[]? ContentDashboardUrlAllowlist { get; set; }
public ISet<string> ContentDashboardUrlAllowlist { get; set; } = new HashSet<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ContentImagingSettings
{
internal const string StaticImageFileTypes = "jpeg,jpg,gif,bmp,png,tiff,tif,webp";

private static readonly ImagingAutoFillUploadField[] DefaultImagingAutoFillUploadField =
private static readonly ISet<ImagingAutoFillUploadField> DefaultImagingAutoFillUploadField = new HashSet<ImagingAutoFillUploadField>
{
new()
{
Expand All @@ -28,10 +28,11 @@ public class ContentImagingSettings
/// Gets or sets a value for the collection of accepted image file extensions.
/// </summary>
[DefaultValue(StaticImageFileTypes)]
public string[] ImageFileTypes { get; set; } = StaticImageFileTypes.Split(',');
public ISet<string> ImageFileTypes { get; set; } = new HashSet<string>(StaticImageFileTypes.Split(Constants.CharArrays.Comma));

/// <summary>
/// Gets or sets a value for the imaging autofill following media file upload fields.
/// </summary>
public ImagingAutoFillUploadField[] AutoFillImageProperties { get; set; } = DefaultImagingAutoFillUploadField;
/// <value>
public ISet<ImagingAutoFillUploadField> AutoFillImageProperties { get; set; } = DefaultImagingAutoFillUploadField;

Check warning on line 37 in src/Umbraco.Core/Configuration/Models/ContentImagingSettings.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

XML comment has badly formed XML -- 'Expected an end tag for element 'value'.'
}
8 changes: 4 additions & 4 deletions src/Umbraco.Core/Configuration/Models/ContentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ @keyframes umbraco-preview-badge--effect {{
/// <summary>
/// Gets or sets a value for the collection of error pages.
/// </summary>
public ContentErrorPage[] Error404Collection { get; set; } = Array.Empty<ContentErrorPage>();
public ISet<ContentErrorPage> Error404Collection { get; set; } = new HashSet<ContentErrorPage>();

/// <summary>
/// Gets or sets a value for the preview badge mark-up.
Expand Down Expand Up @@ -246,18 +246,18 @@ @keyframes umbraco-preview-badge--effect {{
/// <summary>
/// Gets or sets a value for the collection of file extensions that are allowed for upload.
/// </summary>
public string[] AllowedUploadedFileExtensions { get; set; } = Array.Empty<string>();
public ISet<string> AllowedUploadedFileExtensions { get; set; } = new HashSet<string>();

/// <summary>
/// Gets or sets a value for the collection of file extensions that are disallowed for upload.
/// </summary>
[DefaultValue(StaticDisallowedUploadFiles)]
public string[] DisallowedUploadedFileExtensions { get; set; } = StaticDisallowedUploadFiles.Split(',');
public ISet<string> DisallowedUploadedFileExtensions { get; set; } = new HashSet<string>(StaticDisallowedUploadFiles.Split(Constants.CharArrays.Comma));

/// <summary>
/// Gets or sets the allowed external host for media. If empty only relative paths are allowed.
/// </summary>
public string[] AllowedMediaHosts { get; set; } = Array.Empty<string>();
public ISet<string> AllowedMediaHosts { get; set; } = new HashSet<string>();

/// <summary>
/// Gets or sets a value indicating whether to show domain warnings.
Expand Down
22 changes: 15 additions & 7 deletions src/Umbraco.Core/Configuration/Models/DeliveryApiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public class DeliveryApiSettings
/// Gets or sets the aliases of the content types that may never be exposed through the Delivery API. Content of these
/// types will never be returned from any Delivery API endpoint, nor added to the query index.
/// </summary>
/// <value>The content type aliases that are not to be exposed.</value>
public string[] DisallowedContentTypeAliases { get; set; } = Array.Empty<string>();
/// <value>
/// The content type aliases that are not to be exposed.
/// </value>
public ISet<string> DisallowedContentTypeAliases { get; set; } = new HashSet<string>();

/// <summary>
/// Gets or sets a value indicating whether the Delivery API should output rich text values as JSON instead of HTML.
Expand Down Expand Up @@ -133,15 +135,21 @@ public class AuthorizationCodeFlowSettings
/// <summary>
/// Gets or sets the URLs allowed to use as redirect targets after a successful login (session authorization).
/// </summary>
/// <value>The URLs allowed as redirect targets.</value>
public Uri[] LoginRedirectUrls { get; set; } = Array.Empty<Uri>();
/// <value>
/// The URLs allowed as redirect targets.
/// </value>
public ISet<Uri> LoginRedirectUrls { get; set; } = new HashSet<Uri>();

/// <summary>
/// Gets or sets the URLs allowed to use as redirect targets after a successful logout (session termination).
/// </summary>
/// <value>The URLs allowed as redirect targets.</value>
/// <remarks>These are only required if logout is to be used.</remarks>
public Uri[] LogoutRedirectUrls { get; set; } = Array.Empty<Uri>();
/// <value>
/// The URLs allowed as redirect targets.
/// </value>
/// <remarks>
/// These are only required if logout is to be used.
/// </remarks>
public ISet<Uri> LogoutRedirectUrls { get; set; } = new HashSet<Uri>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public class HealthChecksNotificationSettings
/// <summary>
/// Gets or sets a value for the collection of health check notification methods.
/// </summary>
public IDictionary<string, HealthChecksNotificationMethodSettings> NotificationMethods { get; set; } =
new Dictionary<string, HealthChecksNotificationMethodSettings>();
public IDictionary<string, HealthChecksNotificationMethodSettings> NotificationMethods { get; set; } = new Dictionary<string, HealthChecksNotificationMethodSettings>();

/// <summary>
/// Gets or sets a value for the collection of health checks that are disabled for notifications.
/// </summary>
public List<DisabledHealthCheckSettings> DisabledChecks { get; set; } =
new List<DisabledHealthCheckSettings>();
public IList<DisabledHealthCheckSettings> DisabledChecks { get; set; } = new List<DisabledHealthCheckSettings>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class HealthChecksSettings
/// <summary>
/// Gets or sets a value for the collection of healthchecks that are disabled.
/// </summary>
public List<DisabledHealthCheckSettings> DisabledChecks { get; set; } =
new List<DisabledHealthCheckSettings>();
public IList<DisabledHealthCheckSettings> DisabledChecks { get; set; } = new List<DisabledHealthCheckSettings>();

/// <summary>
/// Gets or sets a value for the healthcheck notification settings.
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Configuration/Models/HelpPageSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class HelpPageSettings
/// <summary>
/// Gets or sets the allowed addresses to retrieve data for the content dashboard.
/// </summary>
public string[]? HelpPageUrlAllowList { get; set; }
public ISet<string> HelpPageUrlAllowList { get; set; } = new HashSet<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ public class InstallDefaultDataSettings
/// https://github.com/umbraco/Umbraco-CMS/blob/v9/dev/src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs.
/// </para>
/// </remarks>
public IList<string> Values { get; set; } = new List<string>();
public ISet<string> Values { get; set; } = new HashSet<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public class MarketplaceSettings
/// <summary>
/// Gets or sets the additional parameters that are sent to the Marketplace.
/// </summary>
public Dictionary<string, string> AdditionalParameters { get; set; } = new ();
public IDictionary<string, string> AdditionalParameters { get; set; } = new Dictionary<string, string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See LICENSE for more details.

using System.ComponentModel;
using Umbraco.Cms.Core.Configuration.UmbracoSettings;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Configuration.Models;
Expand Down Expand Up @@ -82,5 +81,5 @@ public class RequestHandlerSettings
/// <summary>
/// Add additional character replacements, or override defaults
/// </summary>
public IEnumerable<CharItem>? UserDefinedCharCollection { get; set; }
public ISet<CharItem> UserDefinedCharCollection { get; set; } = new HashSet<CharItem>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class RichTextEditorSettings

internal const string StaticInvalidElements = "font";

private static readonly string[] Default_plugins =
private static readonly ISet<string> Default_plugins = new HashSet<string>()
{
"anchor", "charmap", "table", "lists", "advlist", "autolink", "directionality", "searchreplace",
};

private static readonly RichTextEditorCommand[] Default_commands =
private static readonly ISet<RichTextEditorCommand> Default_commands = new HashSet<RichTextEditorCommand>()

Check warning on line 20 in src/Umbraco.Core/Configuration/Models/RichTextEditorSettings.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

❌ New issue: Large Method

HashSet has 96 lines, threshold = 70. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
{
new RichTextEditorCommand
{
Expand Down Expand Up @@ -121,12 +121,12 @@ public class RichTextEditorSettings
/// HTML RichText Editor TinyMCE Commands.
/// </summary>
/// WB-TODO Custom Array of objects
public RichTextEditorCommand[] Commands { get; set; } = Default_commands;
public ISet<RichTextEditorCommand> Commands { get; set; } = Default_commands;

/// <summary>
/// HTML RichText Editor TinyMCE Plugins.
/// </summary>
public string[] Plugins { get; set; } = Default_plugins;
public ISet<string> Plugins { get; set; } = Default_plugins;

/// <summary>
/// HTML RichText Editor TinyMCE Custom Config.
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.Core/Configuration/Models/TypeFinderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class TypeFinderSettings
/// By default the entry assemblies for scanning plugin types is the Umbraco DLLs. If you require
/// scanning for plugins based on different root referenced assemblies you can add the assembly name to this list.
/// </summary>
public IEnumerable<string>? AdditionalEntryAssemblies { get; set; }
public ISet<string> AdditionalEntryAssemblies { get; set; } = new HashSet<string>();

/// <summary>
/// Gets or sets a value for the assemblies that will be excluded from scanning.
/// </summary>
public string[] AdditionalAssemblyExclusionEntries { get; set; } = Array.Empty<string>();
public ISet<string> AdditionalAssemblyExclusionEntries { get; set; } = new HashSet<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static TypeLoader AddTypeLoader(
var typeFinder = new TypeFinder(
loggerFactory.CreateLogger<TypeFinder>(),
assemblyProvider,
typeFinderSettings.AdditionalAssemblyExclusionEntries,
typeFinderSettings.AdditionalAssemblyExclusionEntries.ToArray(),
typeFinderConfig);

var typeLoader = new TypeLoader(typeFinder, loggerFactory.CreateLogger<TypeLoader>());
Expand Down
Loading

0 comments on commit e18c6c8

Please sign in to comment.