Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 04dcbc5

Browse files
committed
#117 Throwing a friendly error message when permissions string is invalid
1 parent a5673fa commit 04dcbc5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/main/java/com/marklogic/client/ext/util/DefaultDocumentPermissionsParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public void parsePermissions(String str, DocumentPermissions permissions) {
1212
String[] tokens = str.split(",");
1313
for (int i = 0; i < tokens.length; i += 2) {
1414
String role = tokens[i];
15+
if (i + 1 >= tokens.length) {
16+
throw new IllegalArgumentException("Unable to parse permissions string, which must be a comma-separated " +
17+
"list of role names and capabilities - i.e. role1,read,role2,update,role3,execute; string: " + str);
18+
}
1519
String capability = tokens[i + 1];
1620
Capability c = null;
1721
if (capability.equals("execute")) {

src/test/java/com/marklogic/client/ext/util/ParseDocumentPermissionsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ public void test() {
1919
assertEquals(2, perms.get("rest-admin").size());
2020
assertEquals(1, perms.get("rest-extension-user").size());
2121
}
22+
23+
@Test
24+
public void badInput() {
25+
String str = "rest-admin,read,rest-admin";
26+
try {
27+
parser.parsePermissions(str, new DocumentMetadataHandle().getPermissions());
28+
fail("An IllegalArgumentException should be thrown because the permissions string is missing a second capability " +
29+
"to go with the second role");
30+
} catch (IllegalArgumentException ex) {
31+
assertTrue(ex.getMessage().startsWith("Unable to parse permissions string"));
32+
}
33+
}
2234
}

0 commit comments

Comments
 (0)