add blueprint child interceptor which prevents deletion of children

This commit is contained in:
2021-11-17 14:22:40 +01:00
parent 8d9d52a08a
commit e45b709673
7 changed files with 65 additions and 16 deletions
+7 -11
View File
@@ -15,18 +15,14 @@ namespace zero.Core.Blueprints
{
public List<BlueprintField<T>> UnlockedFields { get; private set; } = new();
protected static string[] DefaultFields { get; set; } = new[] { "name", "alias", "key", "sort", "isActive", "hash", "languageId", "createdById", "createdDate", "lastModifiedById", "lastModifiedDate", "blueprint" };
/// <summary>
/// These fields are not copied by the ObjectCloner
/// as they are either locked or copied manually by ApplyDefaults()
/// </summary>
protected static string[] DefaultUncopyFields { get; set; } = new[] { "id", "url", "name", "alias", "key", "sort", "isActive", "hash", "languageId", "createdById", "createdDate", "lastModifiedById", "lastModifiedDate", "blueprint" };
public Blueprint() : base(typeof(T))
{
//Sync(x => x.Name);
//Sync(x => x.Alias);
//Sync(x => x.Sort);
//Sync(x => x.IsActive);
//Sync(x => x.LanguageId);
//Sync(x => x.Key);
}
public Blueprint() : base(typeof(T)) { }
/// <summary>
@@ -43,7 +39,7 @@ namespace zero.Core.Blueprints
// copy all properties which are synced from the blueprint to the model
ApplyDefaults(blueprint, model);
ObjectCloner.CopyProperties(blueprint, model, DefaultFields.Union(model.Blueprint.Desync).ToArray());
ObjectCloner.CopyProperties(blueprint, model, DefaultUncopyFields.Union(model.Blueprint.Desync).ToArray());
return model;
}
@@ -0,0 +1,50 @@
using Microsoft.Extensions.Logging;
using Raven.Client.Documents.Session;
using System.Threading.Tasks;
using zero.Core.Collections;
using zero.Core.Database;
using zero.Core.Entities;
namespace zero.Core.Blueprints
{
public class BlueprintChildInterceptor : CollectionInterceptor
{
protected IZeroContext Context { get; set; }
protected IZeroStore Store { get; set; }
protected ILogger<BlueprintChildInterceptor> Logger { get; set; }
protected IBlueprintService BlueprintService { get; set; }
public BlueprintChildInterceptor(IZeroContext context, IZeroStore store, ILogger<BlueprintChildInterceptor> logger, IBlueprintService blueprintService)
{
Context = context;
Store = store;
Logger = logger;
BlueprintService = blueprintService;
}
/// <inheritdoc />
public override bool CanRun(Parameters args)
{
// this interceptor does only work for child entities
return (args.Session as AsyncDocumentSession)?.DatabaseName != Context.Options.Raven.Database;
}
/// <inheritdoc />
public override Task<InterceptorResult<ZeroEntity>> Deleting(DeleteParameters args)
{
if (args.Model.Blueprint != null)
{
InterceptorResult<ZeroEntity> result = new();
result.Result = EntityResult<ZeroEntity>.Fail("@blueprint.errors.cannotDeleteChild");
return Task.FromResult(result);
}
return base.Deleting(args);
}
}
}
+1 -2
View File
@@ -3,13 +3,12 @@ using Raven.Client.Documents;
using Raven.Client.Documents.Session;
using System.Collections.Generic;
using System.Threading.Tasks;
using zero.Core.Blueprints;
using zero.Core.Collections;
using zero.Core.Database;
using zero.Core.Entities;
using zero.Core.Extensions;
namespace zero.Core.Routing
namespace zero.Core.Blueprints
{
public class BlueprintInterceptor : CollectionInterceptor
{
-1
View File
@@ -46,7 +46,6 @@ namespace zero.Core.Utils
if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(STRING_TYPE) || propertyValue == null || propertyValue is IEnumerable)
{
Console.WriteLine("copy: " + propertyNameWithPrefix);
property.SetValue(target, propertyValue, null);
}
else if (property.PropertyType.IsClass)
+2 -2
View File
@@ -21,7 +21,7 @@ function canUnlock(config, model, field)
{
if (!config || !model || !model.blueprint)
{
return true;
return false;
}
return config.unlocked.indexOf(field.path) > -1
@@ -49,7 +49,7 @@ async function lock(config, model, field)
}
async function unlock(config, model, field)
function unlock(config, model, field)
{
model.blueprint.desync.push(field.path);
}
@@ -53,6 +53,7 @@ namespace zero.Web.Defaults
zero.Interceptors.Add<ZeroEntityRouteInterceptor>(gravity: 100);
zero.Interceptors.Add<BlueprintInterceptor>(gravity: -1);
zero.Interceptors.Add<BlueprintChildInterceptor>(gravity: -1);
}
@@ -138,6 +139,7 @@ namespace zero.Web.Defaults
services.AddScoped<IBlueprintService, BlueprintService>();
services.AddScoped<BlueprintInterceptor>();
services.AddScoped<BlueprintChildInterceptor>();
services.AddScoped<ZeroEntityRouteInterceptor>();
}
@@ -711,6 +711,9 @@
"goToChild": "View child",
"settingsButton": "Settings",
"xUnlocked": "{count} unlocked"
},
"errors": {
"cannotDeleteChild": "Entities which are synchronized with a blueprint cannot be deleted"
}
},