Skip to content

Commit 566e977

Browse files
authored
Merge pull request #395 from wttech/custom-restrictions
added custom restrictions
2 parents 3bb322e + a1bec69 commit 566e977

File tree

6 files changed

+79
-44
lines changed

6 files changed

+79
-44
lines changed

app/aem/actions.main/src/main/java/com/cognifide/apm/main/actions/allow/Allow.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cognifide.apm.main.utils.PathUtils;
3232
import java.util.Collections;
3333
import java.util.List;
34+
import java.util.Map;
3435
import javax.jcr.PathNotFoundException;
3536
import javax.jcr.RepositoryException;
3637
import org.apache.commons.lang3.StringUtils;
@@ -51,25 +52,25 @@ public class Allow implements Action {
5152
private final boolean ignoreNonExistingPaths;
5253

5354
public Allow(String path, List<String> permissions,
54-
String glob, List<String> ntNames,
55-
List<String> itemNames, boolean ignoreNonExistingPaths) {
55+
String glob, List<String> ntNames, List<String> itemNames, Map<String, Object> customRestrictions,
56+
boolean ignoreNonExistingPaths) {
5657
this.path = path;
5758
this.permissions = permissions;
58-
this.restrictions = new Restrictions(glob, ntNames, itemNames);
59+
this.restrictions = new Restrictions(glob, ntNames, itemNames, customRestrictions);
5960
this.ignoreNonExistingPaths = ignoreNonExistingPaths;
6061
}
6162

6263
@Override
63-
public ActionResult simulate(final Context context) {
64+
public ActionResult simulate(Context context) {
6465
return process(context, true);
6566
}
6667

6768
@Override
68-
public ActionResult execute(final Context context) {
69+
public ActionResult execute(Context context) {
6970
return process(context, false);
7071
}
7172

72-
private ActionResult process(final Context context, boolean simulate) {
73+
private ActionResult process(Context context, boolean simulate) {
7374
ActionResult actionResult = context.createActionResult();
7475
try {
7576
Authorizable authorizable = context.getCurrentAuthorizable();
@@ -78,7 +79,7 @@ private ActionResult process(final Context context, boolean simulate) {
7879
actionResult.changeStatus(Status.SKIPPED, "Skipped adding allow privilege for " + authorizable.getID() + " on " + path);
7980
} else {
8081
context.getSession().getNode(path);
81-
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
82+
PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
8283
context.getValueFactory(), path, permissions, restrictions);
8384
LOGGER.info(String.format("Adding permissions %s for authorizable with id = %s for path = %s %s",
8485
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
@@ -92,11 +93,12 @@ private ActionResult process(final Context context, boolean simulate) {
9293
String preparedGlob = recalculateGlob(restrictions.getGlob());
9394
new Allow(path, Collections.singletonList("MODIFY_PAGE"),
9495
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
96+
restrictions.getCustomRestrictions(),
9597
ignoreNonExistingPaths
9698
).process(context, simulate);
9799
}
98100
}
99-
} catch (final PathNotFoundException e) {
101+
} catch (PathNotFoundException e) {
100102
if (ignoreNonExistingPaths) {
101103
actionResult.logWarning("Path " + path + " not found");
102104
} else {

app/aem/actions.main/src/main/java/com/cognifide/apm/main/actions/allow/AllowMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.cognifide.apm.api.actions.annotations.Required;
3030
import com.cognifide.apm.main.actions.ActionGroup;
3131
import java.util.List;
32+
import java.util.Map;
3233

3334
@Mapper(value = "ALLOW", group = ActionGroup.CORE)
3435
public class AllowMapper {
@@ -51,9 +52,10 @@ public Action create(
5152
@Required(value = "permissions", description = "e.g.: [READ, 'jcr:all']") List<String> permissions,
5253
@Named(value = "glob", description = "regular expression to narrow set of paths") String glob,
5354
@Named(value = "types", description = "list of jcr types which will be affected") List<String> types,
54-
@Named(value = "properties", description = "list of properties which will be affected ") List<String> items,
55+
@Named(value = "properties", description = "list of properties which will be affected") List<String> items,
56+
@Named(value = "restrictions", description = "map of custom restrictions") Map<String, Object> restrictions,
5557
@Flag(value = IF_EXISTS, description = "script doesn't fail if path doesn't exist") boolean ifExists) {
56-
return new Allow(path, permissions, glob, types, items, ifExists);
58+
return new Allow(path, permissions, glob, types, items, restrictions, ifExists);
5759
}
5860

5961
@Mapping(
@@ -71,8 +73,9 @@ public Action create(
7173
@Required(value = "path", description = "e.g.: '/content/dam'") String path,
7274
@Named(value = "glob", description = "regular expression to narrow set of paths") String glob,
7375
@Named(value = "types", description = "list of jcr types which will be affected") List<String> types,
74-
@Named(value = "properties", description = "list of properties which will be affected ") List<String> items,
76+
@Named(value = "properties", description = "list of properties which will be affected") List<String> items,
77+
@Named(value = "restrictions", description = "map of custom restrictions") Map<String, Object> restrictions,
7578
@Flag(value = IF_EXISTS, description = "script doesn't fail if path doesn't exist") boolean ifExists) {
76-
return new Allow(path, permissions, glob, types, items, ifExists);
79+
return new Allow(path, permissions, glob, types, items, restrictions, ifExists);
7780
}
7881
}

app/aem/actions.main/src/main/java/com/cognifide/apm/main/actions/deny/Deny.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cognifide.apm.main.utils.PathUtils;
3232
import java.util.ArrayList;
3333
import java.util.List;
34+
import java.util.Map;
3435
import javax.jcr.PathNotFoundException;
3536
import javax.jcr.RepositoryException;
3637
import org.apache.commons.lang3.StringUtils;
@@ -50,26 +51,26 @@ public class Deny implements Action {
5051

5152
private final boolean ignoreNonExistingPaths;
5253

53-
public Deny(final String path, final List<String> permissions,
54-
final String glob, List<String> ntNames, final List<String> itemNames,
55-
final boolean ignoreNonExistingPaths) {
54+
public Deny(String path, List<String> permissions,
55+
String glob, List<String> ntNames, List<String> itemNames, Map<String, Object> customRestrictions,
56+
boolean ignoreNonExistingPaths) {
5657
this.path = path;
5758
this.permissions = permissions;
58-
this.restrictions = new Restrictions(glob, ntNames, itemNames);
59+
this.restrictions = new Restrictions(glob, ntNames, itemNames, customRestrictions);
5960
this.ignoreNonExistingPaths = ignoreNonExistingPaths;
6061
}
6162

6263
@Override
63-
public ActionResult simulate(final Context context) {
64+
public ActionResult simulate(Context context) {
6465
return process(context, true);
6566
}
6667

6768
@Override
68-
public ActionResult execute(final Context context) {
69+
public ActionResult execute(Context context) {
6970
return process(context, false);
7071
}
7172

72-
private ActionResult process(final Context context, boolean simulate) {
73+
private ActionResult process(Context context, boolean simulate) {
7374
ActionResult actionResult = context.createActionResult();
7475
try {
7576
Authorizable authorizable = context.getCurrentAuthorizable();
@@ -78,7 +79,7 @@ private ActionResult process(final Context context, boolean simulate) {
7879
actionResult.changeStatus(Status.SKIPPED, "Skipped adding deny privilege for " + authorizable.getID() + " on " + path);
7980
} else {
8081
context.getSession().getNode(path);
81-
final PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
82+
PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
8283
context.getValueFactory(), path, permissions, restrictions);
8384
LOGGER.info(String.format("Denying permissions %s for authorizable with id = %s for path = %s %s",
8485
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
@@ -93,18 +94,18 @@ private ActionResult process(final Context context, boolean simulate) {
9394
globModifyPermission.add("MODIFY_PAGE");
9495
String preparedGlob = recalculateGlob(restrictions.getGlob());
9596
new Deny(path, globModifyPermission,
96-
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(),
97+
preparedGlob + "*/jcr:content*", restrictions.getNtNames(), restrictions.getItemNames(), restrictions.getCustomRestrictions(),
9798
ignoreNonExistingPaths)
9899
.process(context, simulate);
99100
}
100101
}
101-
} catch (final PathNotFoundException e) {
102+
} catch (PathNotFoundException e) {
102103
if (ignoreNonExistingPaths) {
103104
actionResult.logWarning("Path " + path + " not found");
104105
} else {
105106
actionResult.logError("Path " + path + " not found");
106107
}
107-
} catch (final RepositoryException | PermissionException | ActionExecutionException e) {
108+
} catch (RepositoryException | PermissionException | ActionExecutionException e) {
108109
actionResult.logError(MessagingUtils.createMessage(e));
109110
}
110111
return actionResult;

app/aem/actions.main/src/main/java/com/cognifide/apm/main/actions/deny/DenyMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.cognifide.apm.api.actions.annotations.Required;
3030
import com.cognifide.apm.main.actions.ActionGroup;
3131
import java.util.List;
32+
import java.util.Map;
3233

3334
@Mapper(value = "DENY", group = ActionGroup.CORE)
3435
public class DenyMapper {
@@ -51,9 +52,10 @@ public Action create(
5152
@Required(value = "permissions", description = "e.g.: [READ, 'jcr:all']") List<String> permissions,
5253
@Named(value = "glob", description = "regular expression to narrow set of paths") String glob,
5354
@Named(value = "types", description = "list of jcr types which will be affected") List<String> types,
54-
@Named(value = "properties", description = "list of properties which will be affected ") List<String> items,
55+
@Named(value = "properties", description = "list of properties which will be affected") List<String> items,
56+
@Named(value = "restrictions", description = "map of custom restrictions") Map<String, Object> restrictions,
5557
@Flag(value = IF_EXISTS, description = "script doesn't fail if path doesn't exist") boolean ifExists) {
56-
return new Deny(path, permissions, glob, types, items, ifExists);
58+
return new Deny(path, permissions, glob, types, items, restrictions, ifExists);
5759
}
5860

5961
@Mapping(
@@ -71,8 +73,9 @@ public Action create(
7173
@Required(value = "path", description = "e.g.: '/content/dam'") String path,
7274
@Named(value = "glob", description = "regular expression to narrow set of paths") String glob,
7375
@Named(value = "types", description = "list of jcr types which will be affected") List<String> types,
74-
@Named(value = "properties", description = "list of properties which will be affected ") List<String> items,
76+
@Named(value = "properties", description = "list of properties which will be affected") List<String> items,
77+
@Named(value = "restrictions", description = "map of custom restrictions") Map<String, Object> restrictions,
7578
@Flag(value = IF_EXISTS, description = "script doesn't fail if path doesn't exist") boolean ifExists) {
76-
return new Deny(path, permissions, glob, types, items, ifExists);
79+
return new Deny(path, permissions, glob, types, items, restrictions, ifExists);
7780
}
7881
}

app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/Restrictions.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.cognifide.apm.main.permissions;
2222

2323
import com.google.common.collect.ImmutableList;
24+
import com.google.common.collect.ImmutableMap;
2425
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.List;
@@ -40,54 +41,75 @@ public class Restrictions {
4041
private static final String STRICT = "STRICT";
4142

4243
private final String glob;
44+
4345
private final List<String> ntNames;
46+
4447
private final List<String> itemNames;
4548

46-
public Restrictions(String glob, List<String> ntNames, List<String> itemNames) {
49+
private final Map<String, Object> customRestrictions;
50+
51+
public Restrictions(String glob, List<String> ntNames, List<String> itemNames, Map<String, Object> customRestrictions) {
4752
this.glob = glob;
4853
this.ntNames = notNullCopy(ntNames);
4954
this.itemNames = notNullCopy(itemNames);
55+
this.customRestrictions = notNullCopy(customRestrictions);
5056
}
5157

5258
private List<String> notNullCopy(List<String> strings) {
5359
return strings != null ? ImmutableList.copyOf(strings) : Collections.emptyList();
5460
}
5561

56-
public Map<String, Value> getSingleValueRestrictions(ValueFactory valueFactory) {
62+
private Map<String, Object> notNullCopy(Map<String, Object> items) {
63+
return items != null ? ImmutableMap.copyOf(items) : Collections.emptyMap();
64+
}
65+
66+
public Map<String, Value> getSingleValueRestrictions(ValueFactory valueFactory) throws ValueFormatException {
5767
Map<String, Value> result = new HashMap<>();
58-
if (StringUtils.isNotBlank(glob)) {
59-
result.put("rep:glob", normalizeGlob(valueFactory));
68+
addRestriction(valueFactory, result, "rep:glob", glob);
69+
for (Map.Entry<String, Object> entry : customRestrictions.entrySet()) {
70+
if (entry.getValue() instanceof String) {
71+
addRestriction(valueFactory, result, entry.getKey(), (String) entry.getValue());
72+
}
6073
}
6174
return result;
6275
}
6376

77+
private void addRestriction(ValueFactory valueFactory, Map<String, Value> result, String key, String value) throws ValueFormatException {
78+
if (StringUtils.isNotBlank(value)) {
79+
if (key.equals("rep:glob")) {
80+
result.put(key, normalizeGlob(valueFactory));
81+
} else {
82+
result.put(key, valueFactory.createValue(value, PropertyType.NAME));
83+
}
84+
}
85+
}
86+
6487
private Value normalizeGlob(ValueFactory valueFactory) {
6588
if (STRICT.equalsIgnoreCase(glob)) {
6689
return valueFactory.createValue(StringUtils.EMPTY);
6790
}
6891
return valueFactory.createValue(glob);
6992
}
7093

71-
public Map<String, Value[]> getMultiValueRestrictions(ValueFactory valueFactory)
72-
throws ValueFormatException {
73-
94+
public Map<String, Value[]> getMultiValueRestrictions(ValueFactory valueFactory) throws ValueFormatException {
7495
Map<String, Value[]> result = new HashMap<>();
7596
addRestrictions(valueFactory, result, "rep:ntNames", ntNames);
7697
addRestrictions(valueFactory, result, "rep:itemNames", itemNames);
98+
for (Map.Entry<String, Object> entry : customRestrictions.entrySet()) {
99+
if (entry.getValue() instanceof List) {
100+
addRestrictions(valueFactory, result, entry.getKey(), (List<String>) entry.getValue());
101+
}
102+
}
77103
return result;
78104
}
79105

80-
private void addRestrictions(ValueFactory valueFactory, Map<String, Value[]> result, String key, List<String> names)
81-
throws ValueFormatException {
82-
106+
private void addRestrictions(ValueFactory valueFactory, Map<String, Value[]> result, String key, List<String> names) throws ValueFormatException {
83107
if (names != null && !names.isEmpty()) {
84108
result.put(key, createRestrictions(valueFactory, names));
85109
}
86110
}
87111

88-
private Value[] createRestrictions(ValueFactory valueFactory, List<String> names)
89-
throws ValueFormatException {
90-
112+
private Value[] createRestrictions(ValueFactory valueFactory, List<String> names) throws ValueFormatException {
91113
Value[] values = new Value[names.size()];
92114
for (int index = 0; index < names.size(); index++) {
93115
values[index] = valueFactory.createValue(names.get(index), PropertyType.NAME);

app/aem/core/src/main/java/com/cognifide/apm/core/actions/MapperDescriptorFactory.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.cognifide.apm.core.actions.ParameterDescriptor.RequiredParameterDescriptor;
3535
import com.cognifide.apm.core.grammar.ApmInteger;
3636
import com.cognifide.apm.core.grammar.ApmList;
37+
import com.cognifide.apm.core.grammar.ApmMap;
3738
import com.cognifide.apm.core.grammar.ApmString;
3839
import com.cognifide.apm.core.grammar.ApmType;
3940
import com.google.common.collect.ImmutableList;
@@ -43,6 +44,7 @@
4344
import java.lang.reflect.ParameterizedType;
4445
import java.lang.reflect.Type;
4546
import java.util.List;
47+
import java.util.Map;
4648
import java.util.Optional;
4749

4850
public class MapperDescriptorFactory {
@@ -53,10 +55,10 @@ public MapperDescriptor create(Class<?> mapperClass) {
5355
throw new InvalidActionMapperException("Mapper must be annotated with " + Mapper.class.getName());
5456
}
5557

56-
final Object mapper = createInstance(mapperClass);
57-
final String name = mapperAnnotation.value();
58-
final String group = mapperAnnotation.group();
59-
final List<MappingDescriptor> mappingDescriptors = Lists.newArrayList();
58+
Object mapper = createInstance(mapperClass);
59+
String name = mapperAnnotation.value();
60+
String group = mapperAnnotation.group();
61+
List<MappingDescriptor> mappingDescriptors = Lists.newArrayList();
6062
for (Method method : mapperClass.getDeclaredMethods()) {
6163
create(mapperAnnotation, method).ifPresent(mappingDescriptors::add);
6264
}
@@ -138,6 +140,8 @@ private Class<? extends ApmType> getApmType(Type type) {
138140
Class rawType = (Class) parameterizedType.getRawType();
139141
if (List.class.equals(rawType)) {
140142
return ApmList.class;
143+
} else if (Map.class.equals(rawType)) {
144+
return ApmMap.class;
141145
}
142146
}
143147
return null;

0 commit comments

Comments
 (0)