21
21
import java .io .InputStream ;
22
22
import java .io .InputStreamReader ;
23
23
import java .io .OutputStream ;
24
- import java .io .UnsupportedEncodingException ;
25
24
import java .math .BigDecimal ;
26
25
import java .math .BigInteger ;
27
26
import java .nio .charset .StandardCharsets ;
@@ -95,18 +94,33 @@ public void addAll(String... collections) {
95
94
}
96
95
}
97
96
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 commaSeparatedRolesAndCapabilities comma-delimited string of the pattern: role1,capability1,role2,capability2,etc.
119
+ * @since 6.3.0
120
+ */
121
+ void addFromString (String commaSeparatedRolesAndCapabilities );
122
+ }
123
+
110
124
@ SuppressWarnings ("serial" )
111
125
static private class PermissionsImpl extends HashMap <String ,Set <Capability >> implements DocumentPermissions {
112
126
@ Override
@@ -130,7 +144,42 @@ public void add(String role, Capability capability) {
130
144
put (role , caps );
131
145
}
132
146
}
147
+
148
+ /**
149
+ *
150
+ * @param commaSeparatedRolesAndCapabilities comma-delimited string of the pattern: role1,capability1,role2,capability2,etc.
151
+ * @since 6.3.0
152
+ */
153
+ @ Override
154
+ public void addFromString (String commaSeparatedRolesAndCapabilities ) {
155
+ if (commaSeparatedRolesAndCapabilities != null && commaSeparatedRolesAndCapabilities .trim ().length () > 0 ) {
156
+ String [] tokens = commaSeparatedRolesAndCapabilities .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-separated " +
162
+ "list of role names and capabilities - i.e. role1,read,role2,update,role3,execute; string: %s" ,
163
+ commaSeparatedRolesAndCapabilities ));
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
+ commaSeparatedRolesAndCapabilities , 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
+ }
133
181
}
182
+
134
183
/**
135
184
* A document operation restricted to users with a role.
136
185
*/
0 commit comments