Skip to content

Commit

Permalink
Fix layer rules import step (OrchardCMS#9068)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored Apr 20, 2021
1 parent ca2ae05 commit d6edac9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrchardCore.Deployment;
using OrchardCore.Entities;
Expand All @@ -10,7 +11,12 @@
namespace OrchardCore.Layers.Deployment
{
public class AllLayersDeploymentSource : IDeploymentSource
{
{
private readonly static JsonSerializer JsonSerializer = new JsonSerializer()
{
TypeNameHandling = TypeNameHandling.Auto
};

private readonly ILayerService _layerService;
private readonly ISiteService _siteService;

Expand All @@ -33,7 +39,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

result.Steps.Add(new JObject(
new JProperty("name", "Layers"),
new JProperty("Layers", layers.Layers.Select(JObject.FromObject))
new JProperty("Layers", layers.Layers.Select(layer => JObject.FromObject(layer, JsonSerializer)))
));

var siteSettings = await _siteService.GetSiteSettingsAsync();
Expand Down
37 changes: 30 additions & 7 deletions src/OrchardCore.Modules/OrchardCore.Layers/Recipes/LayerStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrchardCore.Layers.Models;
using OrchardCore.Layers.Services;
Expand All @@ -16,6 +17,11 @@ namespace OrchardCore.Layers.Recipes
/// </summary>
public class LayerStep : IRecipeStepHandler
{
private readonly static JsonSerializer JsonSerializer = new JsonSerializer()
{
TypeNameHandling = TypeNameHandling.Auto
};

private readonly ILayerService _layerService;
private readonly IRuleMigrator _ruleMigrator;
private readonly IConditionIdGenerator _conditionIdGenerator;
Expand All @@ -40,7 +46,7 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
return;
}

var model = context.Step.ToObject<LayerStepModel>();
var model = context.Step.ToObject<LayersStepModel>();

var allLayers = await _layerService.LoadLayersAsync();

Expand Down Expand Up @@ -83,18 +89,18 @@ public async Task ExecuteAsync(RecipeExecutionContext context)

// The conditions list is cleared, because we cannot logically merge conditions.
layer.LayerRule.Conditions.Clear();
foreach (var condition in layerStep.LayerRule.Conditions)
foreach (var jCondition in layerStep.LayerRule.Conditions)
{
if (factories.TryGetValue(condition.Name, out var factory))
var name = jCondition["Name"].ToString();
if (factories.TryGetValue(name, out var factory))
{
var jCondition = JObject.FromObject(condition);
var factoryCondition = (Condition)jCondition.ToObject(factory.Create().GetType());
var factoryCondition = (Condition)jCondition.ToObject(factory.Create().GetType(), JsonSerializer);

layer.LayerRule.Conditions.Add(factoryCondition);
}
else
{
unknownTypes.Add(condition.Name);
unknownTypes.Add(name);
}
}
}
Expand Down Expand Up @@ -126,8 +132,25 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
}
}

public class LayersStepModel
{
public LayerStepModel[] Layers { get; set; }
}

public class LayerStepModel
{
public Layer[] Layers { get; set; }
public string Name { get; set; }

public string Rule { get; set; }
public string Description { get; set; }

public RuleStepModel LayerRule { get; set; }
}

public class RuleStepModel
{
public string Name { get; set; }
public string ConditionId { get; set; }
public JArray Conditions { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@
"ConditionId": "[js: uuid()]",
"Conditions": [
{
"$type": "OrchardCore.Rules.Models.BooleanCondition, OrchardCore.Rules",
"Name": "BooleanCondition",
"Value": true,
"ConditionId": "[js: uuid()]"
Expand All @@ -870,6 +871,7 @@
"ConditionId": "[js: uuid()]",
"Conditions": [
{
"$type": "OrchardCore.Rules.Models.HomepageCondition, OrchardCore.Rules",
"Name": "HomepageCondition",
"Value": true,
"ConditionId": "[js: uuid()]"
Expand Down
2 changes: 2 additions & 0 deletions src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@
"ConditionId":"[js: uuid()]",
"Conditions": [
{
"$type": "OrchardCore.Rules.Models.BooleanCondition, OrchardCore.Rules",
"Name": "BooleanCondition",
"Value": true,
"ConditionId": "[js: uuid()]"
Expand All @@ -1189,6 +1190,7 @@
"ConditionId": "[js: uuid()]",
"Conditions": [
{
"$type": "OrchardCore.Rules.Models.HomepageCondition, OrchardCore.Rules",
"Name": "HomepageCondition",
"Value": true,
"ConditionId": "[js: uuid()]"
Expand Down

0 comments on commit d6edac9

Please sign in to comment.