From 0ce89a4cc42aaf25013d6eb48c32ea98614f911e Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Fri, 22 Mar 2024 01:47:48 +0100 Subject: [PATCH 1/5] fixed multi value restrictions --- .../apm/main/permissions/Restrictions.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java index b0e08d4c..703cd3d7 100644 --- a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java +++ b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java @@ -94,7 +94,7 @@ private void addRestriction(ValueFactory valueFactory, Map result if (REP_GLOB_PROPERTY.equals(key)) { result.put(key, normalizeGlob(valueFactory)); } else { - result.put(key, valueFactory.createValue(value, PropertyType.NAME)); + result.put(key, valueFactory.createValue(value, determinePropertyType(key))); } } } @@ -126,18 +126,26 @@ public Map getMultiValueRestrictions(ValueFactory valueFactory) private void addRestrictions(ValueFactory valueFactory, Map result, String key, List names) throws ValueFormatException { if (names != null && !names.isEmpty()) { - result.put(key, createRestrictions(valueFactory, names)); + result.put(key, createRestrictions(valueFactory, names, determinePropertyType(key))); } } - private Value[] createRestrictions(ValueFactory valueFactory, List names) throws ValueFormatException { + private Value[] createRestrictions(ValueFactory valueFactory, List names, int propertyType) throws ValueFormatException { Value[] values = new Value[names.size()]; for (int index = 0; index < names.size(); index++) { - values[index] = valueFactory.createValue(names.get(index), PropertyType.NAME); + values[index] = valueFactory.createValue(names.get(index), propertyType); } return values; } + private int determinePropertyType(String key) { + if (StringUtils.equalsAny(key, REP_NT_NAMES_PROPERTY, REP_ITEM_NAMES_PROPERTY)) { + return PropertyType.NAME; + } else { + return PropertyType.STRING; + } + } + private boolean isMultivalue(Map.Entry entry) { boolean result; if (REP_GLOB_PROPERTY.equals(entry.getKey())) { From b5339753d6ce0cb4bdfb00a65ab3e1a11f61ed25 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Fri, 22 Mar 2024 10:30:51 +0100 Subject: [PATCH 2/5] typo --- .../apm/main/permissions/Restrictions.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java index 703cd3d7..4f3b3d10 100644 --- a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java +++ b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java @@ -40,12 +40,12 @@ public class Restrictions { private static final String REP_GLOB_PROPERTY = "rep:glob"; - private static final String REP_NTNAMES_PROPERTY = "rep:ntNames"; + private static final String REP_NT_NAMES_PROPERTY = "rep:ntNames"; - private static final String REP_ITEMNAMES_PROPERTY = "rep:itemNames"; + private static final String REP_ITEM_NAMES_PROPERTY = "rep:itemNames"; - private static final Set MULTIVALUE_REP_PROPERTIES = ImmutableSet.of( - REP_NTNAMES_PROPERTY, REP_ITEMNAMES_PROPERTY, "rep:prefixes", "rep:current", "rep:globs", + private static final Set MULTI_VALUE_REP_PROPERTIES = ImmutableSet.of( + REP_NT_NAMES_PROPERTY, REP_ITEM_NAMES_PROPERTY, "rep:prefixes", "rep:current", "rep:globs", "rep:subtrees", "sling:resourceTypes", "sling:resourceTypesWithDescendants" ); @@ -76,7 +76,7 @@ public Map getSingleValueRestrictions(ValueFactory valueFactory) Map result = new HashMap<>(); addRestriction(valueFactory, result, REP_GLOB_PROPERTY, glob); for (Map.Entry entry : customRestrictions.entrySet()) { - if (!isMultivalue(entry)) { + if (!isMultiValue(entry)) { String value; if (entry.getValue() instanceof String) { value = (String) entry.getValue(); @@ -108,10 +108,10 @@ private Value normalizeGlob(ValueFactory valueFactory) { public Map getMultiValueRestrictions(ValueFactory valueFactory) throws ValueFormatException { Map result = new HashMap<>(); - addRestrictions(valueFactory, result, REP_NTNAMES_PROPERTY, ntNames); - addRestrictions(valueFactory, result, REP_ITEMNAMES_PROPERTY, itemNames); + addRestrictions(valueFactory, result, REP_NT_NAMES_PROPERTY, ntNames); + addRestrictions(valueFactory, result, REP_ITEM_NAMES_PROPERTY, itemNames); for (Map.Entry entry : customRestrictions.entrySet()) { - if (isMultivalue(entry)) { + if (isMultiValue(entry)) { List values; if (entry.getValue() instanceof String) { values = Collections.singletonList((String) entry.getValue()); @@ -146,11 +146,11 @@ private int determinePropertyType(String key) { } } - private boolean isMultivalue(Map.Entry entry) { + private boolean isMultiValue(Map.Entry entry) { boolean result; if (REP_GLOB_PROPERTY.equals(entry.getKey())) { result = false; - } else if (MULTIVALUE_REP_PROPERTIES.contains(entry.getKey())) { + } else if (MULTI_VALUE_REP_PROPERTIES.contains(entry.getKey())) { result = true; } else { result = entry.getValue() instanceof List; From 53a1ca8ea1028b25864cac02d4af65feddebe8a0 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Fri, 22 Mar 2024 10:36:25 +0100 Subject: [PATCH 3/5] refactor --- .../apm/main/permissions/Restrictions.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java index 4f3b3d10..63e15733 100644 --- a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java +++ b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java @@ -32,6 +32,7 @@ import javax.jcr.Value; import javax.jcr.ValueFactory; import javax.jcr.ValueFormatException; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; public class Restrictions { @@ -40,12 +41,14 @@ public class Restrictions { private static final String REP_GLOB_PROPERTY = "rep:glob"; + private static final String REP_GLOBS_PROPERTY = "rep:globs"; + private static final String REP_NT_NAMES_PROPERTY = "rep:ntNames"; private static final String REP_ITEM_NAMES_PROPERTY = "rep:itemNames"; private static final Set MULTI_VALUE_REP_PROPERTIES = ImmutableSet.of( - REP_NT_NAMES_PROPERTY, REP_ITEM_NAMES_PROPERTY, "rep:prefixes", "rep:current", "rep:globs", + REP_NT_NAMES_PROPERTY, REP_ITEM_NAMES_PROPERTY, REP_GLOBS_PROPERTY, "rep:prefixes", "rep:current", "rep:subtrees", "sling:resourceTypes", "sling:resourceTypesWithDescendants" ); @@ -91,11 +94,15 @@ public Map getSingleValueRestrictions(ValueFactory valueFactory) private void addRestriction(ValueFactory valueFactory, Map result, String key, String value) throws ValueFormatException { if (StringUtils.isNotBlank(value)) { - if (REP_GLOB_PROPERTY.equals(key)) { - result.put(key, normalizeGlob(valueFactory)); - } else { - result.put(key, valueFactory.createValue(value, determinePropertyType(key))); - } + result.put(key, createValue(valueFactory, key, value)); + } + } + + private Value createValue(ValueFactory valueFactory, String key, String value) throws ValueFormatException { + if (StringUtils.equalsAny(key, REP_GLOB_PROPERTY, REP_GLOBS_PROPERTY)) { + return normalizeGlob(valueFactory); + } else { + return valueFactory.createValue(value, determinePropertyType(key)); } } @@ -125,15 +132,15 @@ public Map getMultiValueRestrictions(ValueFactory valueFactory) } private void addRestrictions(ValueFactory valueFactory, Map result, String key, List names) throws ValueFormatException { - if (names != null && !names.isEmpty()) { - result.put(key, createRestrictions(valueFactory, names, determinePropertyType(key))); + if (!CollectionUtils.isEmpty(names)) { + result.put(key, createRestrictions(valueFactory, key, names)); } } - private Value[] createRestrictions(ValueFactory valueFactory, List names, int propertyType) throws ValueFormatException { + private Value[] createRestrictions(ValueFactory valueFactory, String key, List names) throws ValueFormatException { Value[] values = new Value[names.size()]; for (int index = 0; index < names.size(); index++) { - values[index] = valueFactory.createValue(names.get(index), propertyType); + values[index] = createValue(valueFactory, key, names.get(index)); } return values; } From 0c7c9f46c5e2cbc5f0e4cb228067c3125fb15af9 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Fri, 22 Mar 2024 14:53:10 +0100 Subject: [PATCH 4/5] refactor --- .../apm/main/permissions/Restrictions.java | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java index 63e15733..f5c820c1 100644 --- a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java +++ b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java @@ -20,19 +20,17 @@ package com.cognifide.apm.main.permissions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import javax.jcr.PropertyType; import javax.jcr.Value; import javax.jcr.ValueFactory; import javax.jcr.ValueFormatException; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; public class Restrictions { @@ -62,17 +60,10 @@ public class Restrictions { public Restrictions(String glob, List ntNames, List itemNames, Map customRestrictions) { this.glob = glob; - this.ntNames = notNullCopy(ntNames); - this.itemNames = notNullCopy(itemNames); - this.customRestrictions = notNullCopy(customRestrictions); - } - - private List notNullCopy(List strings) { - return strings != null ? ImmutableList.copyOf(strings) : Collections.emptyList(); - } - - private Map notNullCopy(Map items) { - return items != null ? ImmutableMap.copyOf(items) : Collections.emptyMap(); + this.ntNames = ntNames; + this.itemNames = itemNames; + this.customRestrictions = Optional.ofNullable(customRestrictions) + .orElse(Collections.emptyMap()); } public Map getSingleValueRestrictions(ValueFactory valueFactory) throws ValueFormatException { @@ -93,7 +84,7 @@ public Map getSingleValueRestrictions(ValueFactory valueFactory) } private void addRestriction(ValueFactory valueFactory, Map result, String key, String value) throws ValueFormatException { - if (StringUtils.isNotBlank(value)) { + if (value != null) { result.put(key, createValue(valueFactory, key, value)); } } @@ -132,7 +123,7 @@ public Map getMultiValueRestrictions(ValueFactory valueFactory) } private void addRestrictions(ValueFactory valueFactory, Map result, String key, List names) throws ValueFormatException { - if (!CollectionUtils.isEmpty(names)) { + if (names != null) { result.put(key, createRestrictions(valueFactory, key, names)); } } From 4e3ea9d53d728c1633743e27adf76d8871434f61 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Sat, 23 Mar 2024 20:44:05 +0100 Subject: [PATCH 5/5] minor --- .../com/cognifide/apm/main/permissions/Restrictions.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java index f5c820c1..1197e6e2 100644 --- a/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java +++ b/app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java @@ -75,7 +75,8 @@ public Map getSingleValueRestrictions(ValueFactory valueFactory) if (entry.getValue() instanceof String) { value = (String) entry.getValue(); } else { - value = ((List) entry.getValue()).get(0); + List values = (List) entry.getValue(); + value = values.isEmpty() ? "" : values.get(0); } addRestriction(valueFactory, result, entry.getKey(), value); } @@ -112,7 +113,8 @@ public Map getMultiValueRestrictions(ValueFactory valueFactory) if (isMultiValue(entry)) { List values; if (entry.getValue() instanceof String) { - values = Collections.singletonList((String) entry.getValue()); + String value = (String) entry.getValue(); + values = value.isEmpty() ? Collections.emptyList() : Collections.singletonList(value); } else { values = (List) entry.getValue(); }