Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.ServerEvents;
using Umbraco.Cms.Core.Services.Filters;
using Umbraco.Cms.Core.Snippets;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Webhooks;
Expand Down Expand Up @@ -92,6 +93,7 @@ internal static void AddAllCoreCollectionBuilders(this IUmbracoBuilder builder)
builder.SortHandlers().Add(() => builder.TypeLoader.GetTypes<ISortHandler>());
builder.ContentIndexHandlers().Add(() => builder.TypeLoader.GetTypes<IContentIndexHandler>());
builder.WebhookEvents().AddCms(true);
builder.ContentTypeFilters();
}

/// <summary>
Expand Down Expand Up @@ -246,4 +248,11 @@ public static SortHandlerCollectionBuilder SortHandlers(this IUmbracoBuilder bui
/// </summary>
public static ContentIndexHandlerCollectionBuilder ContentIndexHandlers(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentIndexHandlerCollectionBuilder>();

/// <summary>
/// Gets the content type filters collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static ContentTypeFilterCollectionBuilder ContentTypeFilters(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<ContentTypeFilterCollectionBuilder>();
}
2 changes: 1 addition & 1 deletion src/Umbraco.Core/DependencyInjection/UmbracoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;
using Umbraco.Cms.Core.Services.Filters;

namespace Umbraco.Cms.Core.DependencyInjection
{
Expand Down Expand Up @@ -444,7 +445,6 @@ private void AddCoreServices()
// Routing
Services.AddUnique<IDocumentUrlService, DocumentUrlService>();
Services.AddNotificationAsyncHandler<UmbracoApplicationStartingNotification, DocumentUrlServiceInitializerNotificationHandler>();

}
}
}
35 changes: 31 additions & 4 deletions src/Umbraco.Core/Services/ContentTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Filters;
using Umbraco.Cms.Core.Services.Locking;
using Umbraco.Cms.Core.Services.OperationStatus;

namespace Umbraco.Cms.Core.Services;

Expand All @@ -28,17 +27,19 @@
IDocumentTypeContainerRepository entityContainerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
IUserIdKeyResolver userIdKeyResolver,
ContentTypeFilterCollection contentTypeFilters)
: base(
provider,
loggerFactory,
eventMessagesFactory,
repository,
auditRepository,
entityContainerRepository,
entityRepository,
eventAggregator,
userIdKeyResolver) =>
userIdKeyResolver,
contentTypeFilters) =>

Check notice on line 42 in src/Umbraco.Core/Services/ContentTypeService.cs

View check run for this annotation

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

ℹ Getting worse: Constructor Over-Injection

ContentTypeService increases from 10 to 11 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
ContentService = contentService;

[Obsolete("Use the ctor specifying all dependencies instead")]
Expand All @@ -65,6 +66,32 @@
StaticServiceProvider.Instance.GetRequiredService<IUserIdKeyResolver>())
{ }

[Obsolete("Use the ctor specifying all dependencies instead")]
public ContentTypeService(
ICoreScopeProvider provider,
ILoggerFactory loggerFactory,
IEventMessagesFactory eventMessagesFactory,
IContentService contentService,
IContentTypeRepository repository,
IAuditRepository auditRepository,
IDocumentTypeContainerRepository entityContainerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
: this(
provider,
loggerFactory,
eventMessagesFactory,
contentService,
repository,
auditRepository,
entityContainerRepository,
entityRepository,
eventAggregator,
userIdKeyResolver,
StaticServiceProvider.Instance.GetRequiredService<ContentTypeFilterCollection>())
{ }

Check notice on line 93 in src/Umbraco.Core/Services/ContentTypeService.cs

View check run for this annotation

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

ℹ New issue: Constructor Over-Injection

ContentTypeService has 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

protected override int[] ReadLockIds => ContentTypeLocks.ReadLockIds;

protected override int[] WriteLockIds => ContentTypeLocks.WriteLockIds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;

Check warning on line 1 in src/Umbraco.Core/Services/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

View check run for this annotation

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

❌ New issue: Lines of Code in a Single File

This module has 1003 lines of code, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in src/Umbraco.Core/Services/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

View check run for this annotation

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

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 57.55% to 52.59%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.DependencyInjection;
Expand All @@ -11,6 +11,7 @@
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Filters;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Extensions;

Expand All @@ -25,6 +26,7 @@
private readonly IEntityRepository _entityRepository;
private readonly IEventAggregator _eventAggregator;
private readonly IUserIdKeyResolver _userIdKeyResolver;
private readonly ContentTypeFilterCollection _contentTypeFilters;

protected ContentTypeServiceBase(
ICoreScopeProvider provider,
Expand All @@ -35,15 +37,17 @@
IEntityContainerRepository containerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
IUserIdKeyResolver userIdKeyResolver,
ContentTypeFilterCollection contentTypeFilters)
: base(provider, loggerFactory, eventMessagesFactory)
{
Repository = repository;
_auditRepository = auditRepository;
_containerRepository = containerRepository;
_entityRepository = entityRepository;
_eventAggregator = eventAggregator;
_userIdKeyResolver = userIdKeyResolver;
_contentTypeFilters = contentTypeFilters;

Check notice on line 50 in src/Umbraco.Core/Services/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

View check run for this annotation

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

ℹ Getting worse: Constructor Over-Injection

ContentTypeServiceBase increases from 9 to 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
}

[Obsolete("Use the ctor specifying all dependencies instead")]
Expand All @@ -69,6 +73,31 @@
{
}

[Obsolete("Use the ctor specifying all dependencies instead")]
protected ContentTypeServiceBase(
ICoreScopeProvider provider,
ILoggerFactory loggerFactory,
IEventMessagesFactory eventMessagesFactory,
TRepository repository,
IAuditRepository auditRepository,
IEntityContainerRepository containerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
: this(
provider,
loggerFactory,
eventMessagesFactory,
repository,
auditRepository,
containerRepository,
entityRepository,
eventAggregator,
userIdKeyResolver,
StaticServiceProvider.Instance.GetRequiredService<ContentTypeFilterCollection>())
{
}

Check warning on line 99 in src/Umbraco.Core/Services/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

View check run for this annotation

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

❌ New issue: Code Duplication

The module contains 6 functions with similar structure: ContentTypeServiceBase,ContentTypeServiceBase,CopyAsync,GetMany and 2 more functions. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

Check notice on line 99 in src/Umbraco.Core/Services/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

View check run for this annotation

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

ℹ New issue: Constructor Over-Injection

ContentTypeServiceBase has 9 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

protected TRepository Repository { get; }
protected abstract int[] WriteLockIds { get; }
protected abstract int[] ReadLockIds { get; }
Expand Down Expand Up @@ -1129,7 +1158,7 @@
#region Allowed types

/// <inheritdoc />
public Task<PagedModel<TItem>> GetAllAllowedAsRootAsync(int skip, int take)
public async Task<PagedModel<TItem>> GetAllAllowedAsRootAsync(int skip, int take)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);

Expand All @@ -1139,28 +1168,39 @@
IQuery<TItem> query = ScopeProvider.CreateQuery<TItem>().Where(x => x.AllowedAsRoot);
IEnumerable<TItem> contentTypes = Repository.Get(query).ToArray();

foreach (IContentTypeFilter filter in _contentTypeFilters)
{
contentTypes = await filter.FilterAllowedAtRootAsync(contentTypes);
}

var pagedModel = new PagedModel<TItem>
{
Total = contentTypes.Count(),
Items = contentTypes.Skip(skip).Take(take)
};

return Task.FromResult(pagedModel);
return pagedModel;
}

/// <inheritdoc />
public Task<Attempt<PagedModel<TItem>?, ContentTypeOperationStatus>> GetAllowedChildrenAsync(Guid key, int skip, int take)
public async Task<Attempt<PagedModel<TItem>?, ContentTypeOperationStatus>> GetAllowedChildrenAsync(Guid key, int skip, int take)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
TItem? parent = Get(key);

if (parent?.AllowedContentTypes is null)
{
return Task.FromResult(Attempt.FailWithStatus<PagedModel<TItem>?, ContentTypeOperationStatus>(ContentTypeOperationStatus.NotFound, null));
return Attempt.FailWithStatus<PagedModel<TItem>?, ContentTypeOperationStatus>(ContentTypeOperationStatus.NotFound, null);
}

IEnumerable<ContentTypeSort> allowedContentTypes = parent.AllowedContentTypes;
foreach (IContentTypeFilter filter in _contentTypeFilters)
{
allowedContentTypes = await filter.FilterAllowedChildrenAsync(allowedContentTypes, key);
}

PagedModel<TItem> result;
if (parent.AllowedContentTypes.Any() is false)
if (allowedContentTypes.Any() is false)
{
// no content types allowed under parent
result = new PagedModel<TItem>
Expand All @@ -1173,7 +1213,7 @@
{
// Get the sorted keys. Whilst we can't guarantee the order that comes back from GetMany, we can use
// this to sort the resulting list of allowed children.
Guid[] sortedKeys = parent.AllowedContentTypes.OrderBy(x => x.SortOrder).Select(x => x.Key).ToArray();
Guid[] sortedKeys = allowedContentTypes.OrderBy(x => x.SortOrder).Select(x => x.Key).ToArray();

TItem[] allowedChildren = GetMany(sortedKeys).ToArray();
result = new PagedModel<TItem>
Expand All @@ -1183,7 +1223,7 @@
};
}

return Task.FromResult(Attempt.SucceedWithStatus<PagedModel<TItem>?, ContentTypeOperationStatus>(ContentTypeOperationStatus.Success, result));
return Attempt.SucceedWithStatus<PagedModel<TItem>?, ContentTypeOperationStatus>(ContentTypeOperationStatus.Success, result);
}

#endregion
Expand Down
18 changes: 18 additions & 0 deletions src/Umbraco.Core/Services/Filters/ContentTypeFilterCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Umbraco.Cms.Core.Composing;

namespace Umbraco.Cms.Core.Services.Filters;

/// <summary>
/// Defines an ordered collection of <see cref="IContentTypeFilter"/>.
/// </summary>
public class ContentTypeFilterCollection : BuilderCollectionBase<IContentTypeFilter>
{
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeFilterCollection"/> class.
/// </summary>
/// <param name="items">The collection items.</param>
public ContentTypeFilterCollection(Func<IEnumerable<IContentTypeFilter>> items)
: base(items)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Umbraco.Cms.Core.Composing;

namespace Umbraco.Cms.Core.Services.Filters;

/// <summary>
/// Builds an ordered collection of <see cref="IContentTypeFilter"/>.
/// </summary>
public class ContentTypeFilterCollectionBuilder : OrderedCollectionBuilderBase<ContentTypeFilterCollectionBuilder, ContentTypeFilterCollection, IContentTypeFilter>
{
/// <inheritdoc/>
protected override ContentTypeFilterCollectionBuilder This => this;
}
25 changes: 25 additions & 0 deletions src/Umbraco.Core/Services/Filters/IContentTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Umbraco.Cms.Core.Models;

namespace Umbraco.Cms.Core.Services.Filters;

/// <summary>
/// Defines methods for filtering content types after retrieval from the database.
/// </summary>
public interface IContentTypeFilter
{
/// <summary>
/// Filters the content types retrieved for being allowed at the root.
/// </summary>
/// <param name="contentTypes">Retrieved collection of content types.</param>
/// <returns>Filtered collection of content types.</returns>
Task<IEnumerable<TItem>> FilterAllowedAtRootAsync<TItem>(IEnumerable<TItem> contentTypes)
where TItem : IContentTypeComposition;

/// <summary>
/// Filters the content types retrieved for being allowed as children of a parent content type.
/// </summary>
/// <param name="contentTypes">Retrieved collection of content types.</param>
/// <param name="parentKey">The parent content type key.</param>
/// <returns>Filtered collection of content types.</returns>
Task<IEnumerable<ContentTypeSort>> FilterAllowedChildrenAsync(IEnumerable<ContentTypeSort> contentTypes, Guid parentKey);
}
33 changes: 31 additions & 2 deletions src/Umbraco.Core/Services/MediaTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Filters;
using Umbraco.Cms.Core.Services.Locking;
using Umbraco.Extensions;

Expand All @@ -24,17 +25,19 @@
IMediaTypeContainerRepository entityContainerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
IUserIdKeyResolver userIdKeyResolver,
ContentTypeFilterCollection contentTypeFilters)
: base(
provider,
loggerFactory,
eventMessagesFactory,
mediaTypeRepository,
auditRepository,
entityContainerRepository,
entityRepository,
eventAggregator,
userIdKeyResolver) => MediaService = mediaService;
userIdKeyResolver,
contentTypeFilters) => MediaService = mediaService;

Check notice on line 40 in src/Umbraco.Core/Services/MediaTypeService.cs

View check run for this annotation

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

ℹ Getting worse: Constructor Over-Injection

MediaTypeService increases from 10 to 11 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

[Obsolete("Use the constructor with all dependencies instead")]
public MediaTypeService(
Expand All @@ -61,6 +64,32 @@
{
}

[Obsolete("Use the constructor with all dependencies instead")]
public MediaTypeService(
ICoreScopeProvider provider,
ILoggerFactory loggerFactory,
IEventMessagesFactory eventMessagesFactory,
IMediaService mediaService,
IMediaTypeRepository mediaTypeRepository,
IAuditRepository auditRepository,
IMediaTypeContainerRepository entityContainerRepository,
IEntityRepository entityRepository,
IEventAggregator eventAggregator,
IUserIdKeyResolver userIdKeyResolver)
: this(
provider,
loggerFactory,
eventMessagesFactory,
mediaService,
mediaTypeRepository,
auditRepository,
entityContainerRepository,
entityRepository,
eventAggregator,
userIdKeyResolver,
StaticServiceProvider.Instance.GetRequiredService<ContentTypeFilterCollection>())
{
}

Check notice on line 92 in src/Umbraco.Core/Services/MediaTypeService.cs

View check run for this annotation

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

ℹ New issue: Constructor Over-Injection

MediaTypeService has 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

protected override int[] ReadLockIds => MediaTypeLocks.ReadLockIds;

Expand Down
Loading
Loading