Expose and manage system media types (#16343)

* Expose and manage system media types

* Removed license :D
This commit is contained in:
Kenn Jacobsen
2024-05-24 08:59:47 +02:00
committed by GitHub
parent 8383532fd5
commit 07711aac28
11 changed files with 111 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Extensions;
namespace Umbraco.Cms.Api.Management.Controllers.MediaType.Tree;
@@ -38,6 +39,7 @@ public class MediaTypeTreeControllerBase : FolderTreeControllerBase<MediaTypeTre
if (mediaTypes.TryGetValue(entity.Id, out IMediaType? mediaType))
{
responseModel.Icon = mediaType.Icon ?? responseModel.Icon;
responseModel.IsDeletable = mediaType.IsSystemMediaType() is false;
}
return responseModel;

View File

@@ -48,6 +48,8 @@ public class MediaTypeMapDefinition : ContentTypeMapDefinition<IMediaType, Media
MediaType = referenceByIdModel,
CompositionType = compositionType,
});
target.IsDeletable = source.IsSystemMediaType() is false;
target.AliasCanBeChanged = source.IsSystemMediaType() is false;
}
// Umbraco.Code.MapAll

View File

@@ -38642,12 +38642,14 @@
"MediaTypeResponseModel": {
"required": [
"alias",
"aliasCanBeChanged",
"allowedAsRoot",
"allowedMediaTypes",
"compositions",
"containers",
"icon",
"id",
"isDeletable",
"isElement",
"name",
"properties",
@@ -38735,6 +38737,12 @@
}
]
}
},
"isDeletable": {
"type": "boolean"
},
"aliasCanBeChanged": {
"type": "boolean"
}
},
"additionalProperties": false
@@ -38765,6 +38773,7 @@
"hasChildren",
"icon",
"id",
"isDeletable",
"isFolder",
"name"
],
@@ -38793,6 +38802,9 @@
},
"icon": {
"type": "string"
},
"isDeletable": {
"type": "boolean"
}
},
"additionalProperties": false

View File

@@ -7,4 +7,8 @@ public class MediaTypeResponseModel : ContentTypeResponseModelBase<MediaTypeProp
public IEnumerable<MediaTypeSort> AllowedMediaTypes { get; set; } = Enumerable.Empty<MediaTypeSort>();
public IEnumerable<MediaTypeComposition> Compositions { get; set; } = Enumerable.Empty<MediaTypeComposition>();
public bool IsDeletable { get; set; }
public bool AliasCanBeChanged { get; set; }
}

View File

@@ -3,4 +3,6 @@
public class MediaTypeTreeItemResponseModel : FolderTreeItemResponseModel
{
public string Icon { get; set; } = string.Empty;
public bool IsDeletable { get; set; }
}

View File

@@ -1,5 +1,6 @@
using System.Runtime.Serialization;
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Models;
@@ -45,6 +46,21 @@ public class MediaType : ContentTypeCompositionBase, IMediaType
/// <inheritdoc />
public override ISimpleContentType ToSimple() => new SimpleContentType(this);
/// <inheritdoc />
public override string Alias
{
get => base.Alias;
set
{
if (this.IsSystemMediaType() && value != Alias)
{
throw new InvalidOperationException("Cannot change the alias of a system media type");
}
base.Alias = value;
}
}
/// <inheritdoc />
IMediaType IMediaType.DeepCloneWithResetIdentities(string newAlias) =>
(IMediaType)DeepCloneWithResetIdentities(newAlias);

View File

@@ -42,6 +42,11 @@ internal sealed class MediaTypeEditingService : ContentTypeEditingServiceBase<IM
public async Task<Attempt<IMediaType?, ContentTypeOperationStatus>> UpdateAsync(IMediaType mediaType, MediaTypeUpdateModel model, Guid userKey)
{
if (mediaType.IsSystemMediaType() && mediaType.Alias != model.Alias)
{
return Attempt.FailWithStatus<IMediaType?, ContentTypeOperationStatus>(ContentTypeOperationStatus.NotAllowed, null);
}
Attempt<IMediaType?, ContentTypeOperationStatus> result = await ValidateAndMapForUpdateAsync(mediaType, model);
if (result.Success)
{

View File

@@ -620,6 +620,11 @@ public abstract class ContentTypeServiceBase<TRepository, TItem> : ContentTypeSe
return ContentTypeOperationStatus.NotFound;
}
if (CanDelete(item) is false)
{
return ContentTypeOperationStatus.NotAllowed;
}
Delete(item, performingUserId);
scope.Complete();
@@ -628,6 +633,11 @@ public abstract class ContentTypeServiceBase<TRepository, TItem> : ContentTypeSe
public void Delete(TItem item, int userId = Constants.Security.SuperUserId)
{
if (CanDelete(item) is false)
{
throw new InvalidOperationException("The item was not allowed to be deleted");
}
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
EventMessages eventMessages = EventMessagesFactory.Get();
@@ -695,6 +705,10 @@ public abstract class ContentTypeServiceBase<TRepository, TItem> : ContentTypeSe
public void Delete(IEnumerable<TItem> items, int userId = Constants.Security.SuperUserId)
{
TItem[] itemsA = items.ToArray();
if (itemsA.All(CanDelete) is false)
{
throw new InvalidOperationException("One or more items were not allowed to be deleted");
}
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
@@ -750,6 +764,8 @@ public abstract class ContentTypeServiceBase<TRepository, TItem> : ContentTypeSe
protected abstract void DeleteItemsOfTypes(IEnumerable<int> typeIds);
protected virtual bool CanDelete(TItem item) => true;
#endregion
#region Copy

View File

@@ -8,6 +8,7 @@ using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Locking;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Services;
@@ -77,6 +78,9 @@ public class MediaTypeService : ContentTypeServiceBase<IMediaTypeRepository, IMe
}
}
protected override bool CanDelete(IMediaType item)
=> item.IsSystemMediaType() is false;
#region Notifications
protected override SavingNotification<IMediaType> GetSavingNotification(