@@ -181,23 +181,85 @@ function getGroupMembers(connection, groupId, callback) {
181
181
} ) ;
182
182
}
183
183
184
+
185
+ /**
186
+ * Get groups that the current user can access.
187
+ *
188
+ * @param {Object } connection - the connection object provided by ActionHero
189
+ * @param {Function<err, groupIds> } callback - the callback. Receives either an error
190
+ * or the list of group's users an array of numeric IDs
191
+ */
192
+ function getMemberGroups ( connection , callback ) {
193
+ getToken ( connection , function ( err , token ) {
194
+ if ( err ) {
195
+ callback ( err ) ;
196
+ return ;
197
+ }
198
+
199
+ var userId = ( connection . caller . userId || 0 ) ;
200
+
201
+ // calls
202
+ callService ( {
203
+ url : v3url + 'groups?membershipType=user&memberId=' + userId ,
204
+ method : 'GET' ,
205
+ headers : {
206
+ 'Authorization' : 'Bearer ' + token
207
+ }
208
+ } , function ( err , body ) {
209
+ if ( err ) {
210
+ callback ( err ) ;
211
+ } else {
212
+ var groupIds = body . result . content . map ( function ( item ) {
213
+ return item . id ;
214
+ } ) ;
215
+
216
+ var result = [ ] ;
217
+
218
+ groupIds . forEach ( function ( groupId ) {
219
+ result . push ( groupId ) ;
220
+ callService ( {
221
+ url : v3url + 'groups/' + groupId + '/getParentGroup?oneLevel=false' ,
222
+ method : 'GET' ,
223
+ headers : {
224
+ 'Authorization' : 'Bearer ' + token
225
+ }
226
+ } , function ( err , body ) {
227
+ if ( err ) {
228
+ callback ( err ) ;
229
+ } else {
230
+ var groupResponse = body . result . content ;
231
+ while ( groupResponse ) {
232
+ result . push ( groupResponse . id ) ;
233
+ groupResponse = groupResponse . parentGroup ;
234
+ }
235
+ }
236
+ } )
237
+ } ) ;
238
+
239
+ callback ( null , result ) ;
240
+ }
241
+ } ) ;
242
+ } ) ;
243
+ }
244
+
245
+
246
+
184
247
exports . v3client = function ( api , next ) {
185
248
api . v3client = {
186
249
/**
187
250
* Check if the user belongs to the group
188
251
*
189
252
* @param {Object } connection - the connection object provided by ActionHero
190
- * @param {Number } userId - the user ID
191
253
* @param {Number } groupId - the group ID
192
254
* @param {Function<err, isIn> } callback - the callback. The second parameter
193
255
* is boolean vwhich is true if the user is found in the group.
194
256
*/
195
- isUserInGroup : function ( connection , userId , groupId , callback ) {
196
- getGroupMembers ( connection , groupId , function ( err , members ) {
257
+ isUserInGroup : function ( connection , groupId , callback ) {
258
+ getMemberGroups ( connection , groupId , function ( err , groupIds ) {
197
259
if ( err ) {
198
260
callback ( err ) ;
199
261
} else {
200
- callback ( null , members . indexOf ( userId ) >= 0 ) ;
262
+ callback ( null , groupIds . indexOf ( groupId ) >= 0 ) ;
201
263
}
202
264
} ) ;
203
265
}
0 commit comments