2121import java .io .InputStream ;
2222import java .io .InputStreamReader ;
2323import java .io .OutputStream ;
24- import java .io .UnsupportedEncodingException ;
2524import java .math .BigDecimal ;
2625import java .math .BigInteger ;
2726import java .nio .charset .StandardCharsets ;
@@ -95,18 +94,33 @@ public void addAll(String... collections) {
9594 }
9695 }
9796
98- /**
99- * Represents the permissions for a database document.
100- */
101- public interface DocumentPermissions extends Map <String ,Set <Capability >> {
102- /**
103- * Adds a role with one or more capabilities to the metadata that can be written
104- * for the document.
105- * @param role the role for users permitted to access the document
106- * @param capabilities the permissions to be granted to users with the role
107- */
108- public void add (String role , Capability ... capabilities );
109- }
97+ /**
98+ * Represents the permissions for a database document.
99+ */
100+ public interface DocumentPermissions extends Map <String , Set <Capability >> {
101+ /**
102+ * Adds a role with one or more capabilities to the metadata that can be written
103+ * for the document.
104+ *
105+ * @param role the role for users permitted to access the document
106+ * @param capabilities the permissions to be granted to users with the role
107+ */
108+ void add (String role , Capability ... capabilities );
109+
110+ /**
111+ * Adds one or more permissions based on the given comma-delimited string. Each capability value is
112+ * case-insensitive; you do not need to worry about providing the correct case. Similar to {@code add}, this
113+ * method adds permissions and can thus add capabilities to roles already present in this object.
114+ *
115+ * For example, the following string would add two permissions - a "read" permission for "rest-reader" and an
116+ * "update" permission for "rest-writer": rest-reader,read,rest-writer,update.
117+ *
118+ * @param commaDelimitedRolesAndCapabilities comma-delimited string of the pattern: role1,capability1,role2,capability2,etc.
119+ * @since 6.3.0
120+ */
121+ void addFromDelimitedString (String commaDelimitedRolesAndCapabilities );
122+ }
123+
110124 @ SuppressWarnings ("serial" )
111125 static private class PermissionsImpl extends HashMap <String ,Set <Capability >> implements DocumentPermissions {
112126 @ Override
@@ -130,7 +144,42 @@ public void add(String role, Capability capability) {
130144 put (role , caps );
131145 }
132146 }
147+
148+ /**
149+ *
150+ * @param commaDelimitedRolesAndCapabilities comma-delimited string of the pattern: role1,capability1,role2,capability2,etc.
151+ * @since 6.3.0
152+ */
153+ @ Override
154+ public void addFromDelimitedString (String commaDelimitedRolesAndCapabilities ) {
155+ if (commaDelimitedRolesAndCapabilities != null && commaDelimitedRolesAndCapabilities .trim ().length () > 0 ) {
156+ String [] tokens = commaDelimitedRolesAndCapabilities .trim ().split ("," );
157+ for (int i = 0 ; i < tokens .length ; i += 2 ) {
158+ String role = tokens [i ];
159+ if (i + 1 >= tokens .length ) {
160+ throw new IllegalArgumentException (String .format (
161+ "Unable to parse permissions string, which must be a comma-delimited " +
162+ "list of role names and capabilities - i.e. role1,read,role2,update,role3,execute; string: %s" ,
163+ commaDelimitedRolesAndCapabilities ));
164+ }
165+ Capability c ;
166+ try {
167+ c = Capability .getValueOf (tokens [i + 1 ]);
168+ } catch (Exception e ) {
169+ throw new IllegalArgumentException (String .format (
170+ "Unable to parse permissions string: %s; cause: %s" ,
171+ commaDelimitedRolesAndCapabilities , e .getMessage ()));
172+ }
173+ if (this .containsKey (role )) {
174+ this .get (role ).add (c );
175+ } else {
176+ this .add (role , c );
177+ }
178+ }
179+ }
180+ }
133181 }
182+
134183 /**
135184 * A document operation restricted to users with a role.
136185 */
0 commit comments