Skip to content

Commit e7d8615

Browse files
committed
Adds setting ACL permissions by mode string
Signed-off-by: Patrick Reinhart <patrick@reini.net>
1 parent b4621c7 commit e7d8615

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/main/java/org/xmldb/api/security/AclEntry.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ public Builder setPermissions(AclEntryPermission... perms) {
185185
return this;
186186
}
187187

188+
/**
189+
* Sets the permissions component of this builder. On return, the permissions component of this
190+
* builder is a copy of the permissions in the given array.
191+
*
192+
* @param modeStr the permissions mode string
193+
* @return this builder
194+
*/
195+
public Builder setPermissions(String modeStr) {
196+
Set<AclEntryPermission> set = EnumSet.noneOf(AclEntryPermission.class);
197+
if (modeStr == null || modeStr.isEmpty() || modeStr.length() > 3) {
198+
throw new IllegalArgumentException("Invalid mode string '" + modeStr + "'");
199+
}
200+
for (int index = 0; index < 3; index++) {
201+
char chr = modeStr.charAt(index);
202+
switch (chr) {
203+
case 'r':
204+
set.add(AclEntryPermission.READ);
205+
break;
206+
case 'w':
207+
set.add(AclEntryPermission.WRITE);
208+
break;
209+
case 'x':
210+
set.add(AclEntryPermission.EXECUTE);
211+
break;
212+
case '-':
213+
break;
214+
default:
215+
throw new IllegalArgumentException(
216+
"Unknown char '" + chr + "' in mode string '" + modeStr + "'");
217+
}
218+
}
219+
this.permissions = set;
220+
return this;
221+
}
222+
188223
/**
189224
* Sets the flags component of this builder. On return, the flags component of this builder is a
190225
* copy of the given set.

src/test/java/org/xmldb/api/security/AclEntryTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ void testBuildWithPermissionsVarargs() {
125125
});
126126
}
127127

128+
@Test
129+
void testBuildWithPermissionsModeString() {
130+
assertThatExceptionOfType(IllegalArgumentException.class)
131+
.isThrownBy(() -> builder.setPermissions((String) null))
132+
.withMessage("Invalid mode string 'null'");
133+
assertThatExceptionOfType(IllegalArgumentException.class)
134+
.isThrownBy(() -> builder.setPermissions("")).withMessage("Invalid mode string ''");
135+
assertThatExceptionOfType(IllegalArgumentException.class)
136+
.isThrownBy(() -> builder.setPermissions("xxxx")).withMessage("Invalid mode string 'xxxx'");
137+
assertThatExceptionOfType(IllegalArgumentException.class)
138+
.isThrownBy(() -> builder.setPermissions("a"))
139+
.withMessage("Unknown char 'a' in mode string 'a'");
140+
builder.setType(ALLOW).setPrincipal(principal).setPermissions("rw-");
141+
assertThat(builder.build()).satisfies(aclEntry -> {
142+
assertThat(aclEntry.type()).isEqualTo(ALLOW);
143+
assertThat(aclEntry.permissions()).containsExactlyInAnyOrder(READ, WRITE)
144+
.isInstanceOf(EnumSet.class);
145+
});
146+
builder.setType(ALLOW).setPrincipal(principal).setPermissions("r-x");
147+
assertThat(builder.build()).satisfies(aclEntry -> {
148+
assertThat(aclEntry.type()).isEqualTo(ALLOW);
149+
assertThat(aclEntry.permissions()).containsExactlyInAnyOrder(READ, EXECUTE)
150+
.isInstanceOf(EnumSet.class);
151+
});
152+
}
153+
128154
@Test
129155
void testBuildWithEmpptyFlagsSet() {
130156
builder.setType(DENY).setPrincipal(principal);

0 commit comments

Comments
 (0)