Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 1ed0b21

Browse files
authored
Merge pull request #512 from appirio-tech/revise_user_challenge_eligibility_logic
revise user challenge eligibility check
2 parents 01e459c + 42a7eca commit 1ed0b21

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

initializers/v3client.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,85 @@ function getGroupMembers(connection, groupId, callback) {
181181
});
182182
}
183183

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+
184247
exports.v3client = function (api, next) {
185248
api.v3client = {
186249
/**
187250
* Check if the user belongs to the group
188251
*
189252
* @param {Object} connection - the connection object provided by ActionHero
190-
* @param {Number} userId - the user ID
191253
* @param {Number} groupId - the group ID
192254
* @param {Function<err, isIn>} callback - the callback. The second parameter
193255
* is boolean vwhich is true if the user is found in the group.
194256
*/
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) {
197259
if (err) {
198260
callback(err);
199261
} else {
200-
callback(null, members.indexOf(userId) >= 0);
262+
callback(null, groupIds.indexOf(groupId) >= 0);
201263
}
202264
});
203265
}

0 commit comments

Comments
 (0)