Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support capturing groups for test users #747

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package biz.netcentric.cq.tools.actool.configreader;

/*-
Expand All @@ -18,6 +17,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -71,10 +72,14 @@ void createTestUserConfigs(AcConfiguration acConfiguration, InstallationLogger l
continue;
}
String groupId = groupAuthConfigBean.getAuthorizableId();
if (groupId.matches(autoCreateTestUsersConf.getCreateForGroupNamesRegEx())) {
Pattern pattern = Pattern.compile(autoCreateTestUsersConf.getCreateForGroupNamesRegEx());
Matcher matcher = pattern.matcher(groupId);
if (matcher.matches()) {

Map<String, Object> vars = getVarsForAuthConfigBean(groupAuthConfigBean);

// also add all captured groups from the matcher as variables
vars.putAll(getVarsForCapturedGroups(matcher));

AuthorizableConfigBean testUserConfigBean = new AuthorizableConfigBean();
testUserConfigBean.setIsGroup(false);
String testUserAuthId = autoCreateTestUsersConf.getPrefix() + groupId;
Expand Down Expand Up @@ -119,6 +124,14 @@ void createTestUserConfigs(AcConfiguration acConfiguration, InstallationLogger l

}

Map<String, Object> getVarsForCapturedGroups(Matcher matcher) {
Map<String,Object> vars = new HashMap<>();
for (int i=0; i <= matcher.groupCount(); i++) {
vars.put("cg"+i, matcher.group(i));
}
return vars;
}

Map<String, Object> getVarsForAuthConfigBean(AuthorizableConfigBean groupAuthConfigBean) {
Map<String,Object> vars = new HashMap<>();
Map<String,String> groupVar = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
* #L%
*/
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -55,23 +59,38 @@ public void testELFunctions() {
assertEquals("testfolder/subfolder", testUserConfigsCreator.processValue("%{split(group.path,'/')[2]}/%{split(group.path,'/')[3]}", testVars));
assertEquals("group-id", testUserConfigsCreator.processValue("%{substringAfter(group.id,'-')}", testVars));
}


@Test
public void testCapturingGroups() {
Matcher matcher = Pattern.compile("prefix-(.*)").matcher("prefix-group");
assertTrue(matcher.matches());
assertEquals("group", testUserConfigsCreator.processValue("%{cg1}", getTestVars(TEST_GROUP_NAME, matcher)));
assertEquals("prefix-group", testUserConfigsCreator.processValue("%{cg0}", getTestVars(TEST_GROUP_NAME, matcher)));
}

@Test
public void testMultipleReplacements() {
assertEquals("Name "+TEST_GROUP_NAME+" Path "+TEST_GROUP_PATH, testUserConfigsCreator.processValue("Name %{group.name} Path %{group.path}", getTestVars()));
}


private Map<String, Object> getTestVars() {
return getTestVars(TEST_GROUP_NAME);
}

private Map<String, Object> getTestVars(String name) {
Matcher matcher = Pattern.compile("").matcher("");
matcher.matches();
return getTestVars(name, matcher);
}

private Map<String, Object> getTestVars(String name, Matcher matcher) {
AuthorizableConfigBean groupAuthConfigBean = new AuthorizableConfigBean();
groupAuthConfigBean.setAuthorizableId(TEST_GROUP_ID);
groupAuthConfigBean.setName(name);
groupAuthConfigBean.setPath(TEST_GROUP_PATH);
return testUserConfigsCreator.getVarsForAuthConfigBean(groupAuthConfigBean);
Map<String, Object> vars = new HashMap<>(testUserConfigsCreator.getVarsForAuthConfigBean(groupAuthConfigBean));
vars.putAll(testUserConfigsCreator.getVarsForCapturedGroups(matcher));
return vars;
}

}
4 changes: 3 additions & 1 deletion docs/AdvancedFeatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ It is possible to automatically create test users (since v2.1.0) for groups give

property | comment | required
--- | --- | ---
`createForGroupNamesRegEx` | A regex (matched against authorizableId of groups) to select the groups, test users should be created for | required
`createForGroupNamesRegEx` | A regex (matched against authorizableId of groups) to select the groups, test users should be created for. The regular expression may contain [capturing groups](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#cg). | required
`prefix` | The prefix for the authorizable id, for instance if prefix "tu-" is given, a user "tu-myproject-editors" will be created for group "myproject-editors" | required
`name` | The name as configured in user's profile, allows for interpolation with EL *) | optional, defaults to "Test User %{group.name}"
`description` | The description as configured in user's profile, allows for interpolation with EL *) | optional, not set by default
Expand All @@ -295,6 +295,8 @@ property | comment | required

*) Interpolation of group properties can be used with EL, however as `$` is evaluated at an earlier stage, `%{}` is used here. Available is `%{group.id}`, `%{group.name}`, `%{group.path}` or expressions like `%{split(group.path,'/')[2]}`.

The special variables `%{cg<capturingGroupIndex>}` may be used to reference a capturing group from the regular expression given in `createForGroupNamesRegEx` matched against the group id. The variable `%{cg0}` stands for the complete group id (since version 3.2.0).

Example:

```
Expand Down
Loading