From cdbad7795e4a13d16309d7566bece13c8ddaa908 Mon Sep 17 00:00:00 2001 From: Ryan Wawrzaszek Date: Wed, 27 Mar 2024 10:36:17 -0400 Subject: [PATCH 1/3] Adjust content type condition to evaluate to true when no content is displayed and the operation for the given condition is a negation. --- .../Drivers/ContentTypeConditionEvaluatorDriver.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs index 59fe11dd09e..79902a05a94 100644 --- a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using OrchardCore.ContentManagement; using OrchardCore.ContentManagement.Display.ContentDisplay; @@ -43,6 +44,14 @@ public ValueTask EvaluateAsync(Condition condition) private ValueTask EvaluateAsync(ContentTypeCondition condition) { var operatorComparer = _operatorResolver.GetOperatorComparer(condition.Operation); + + // If no content types are considered and the operation is an INegateOperator, return true, + // since displaying no content type should match any negative comparision on any content type + if (!_contentTypes.Any() && condition.Operation is INegateOperator) + { + return ValueTask.FromResult(true); + } + foreach (var contentType in _contentTypes) { if (operatorComparer.Compare(condition.Operation, contentType, condition.Value)) From ba17907c324c37119f10c4b42fc16058a0bea6d4 Mon Sep 17 00:00:00 2001 From: Ryan Wawrzaszek Date: Wed, 27 Mar 2024 12:45:01 -0400 Subject: [PATCH 2/3] Adjust logic to compare condition value to empty string rather than assuming all implementations of INegateOperation should return true when no content types are considered. --- .../Drivers/ContentTypeConditionEvaluatorDriver.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs index 79902a05a94..f713809c120 100644 --- a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs @@ -45,11 +45,11 @@ private ValueTask EvaluateAsync(ContentTypeCondition condition) { var operatorComparer = _operatorResolver.GetOperatorComparer(condition.Operation); - // If no content types are considered and the operation is an INegateOperator, return true, - // since displaying no content type should match any negative comparision on any content type - if (!_contentTypes.Any() && condition.Operation is INegateOperator) + // If no content types are considered, use the empty string as the value to compare against, + // since we still want comparisons such as "Does Not Equal", "Does Not Start With", etc. to evaluate to true in this case. + if (!_contentTypes.Any()) { - return ValueTask.FromResult(true); + return ValueTask.FromResult(operatorComparer.Compare(condition.Operation, string.Empty, condition.Value)); } foreach (var contentType in _contentTypes) From f82a6abd9a2cbc88e95670fa7372712a166f5620 Mon Sep 17 00:00:00 2001 From: rwawr <153218770+rwawr@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:05:42 -0400 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Mike Alhayek --- .../Drivers/ContentTypeConditionEvaluatorDriver.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs index f713809c120..daed06de4a9 100644 --- a/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Rules/Drivers/ContentTypeConditionEvaluatorDriver.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using OrchardCore.ContentManagement; using OrchardCore.ContentManagement.Display.ContentDisplay; @@ -47,7 +46,7 @@ private ValueTask EvaluateAsync(ContentTypeCondition condition) // If no content types are considered, use the empty string as the value to compare against, // since we still want comparisons such as "Does Not Equal", "Does Not Start With", etc. to evaluate to true in this case. - if (!_contentTypes.Any()) + if (_contentTypes.Count == 0) { return ValueTask.FromResult(operatorComparer.Compare(condition.Operation, string.Empty, condition.Value)); }