Skip to content

Commit

Permalink
ctservice.js: Avoid global group membership endpoint
Browse files Browse the repository at this point in the history
By fetching the group members per group instead of globally we can
avoid the endpoint that requires "administer persons" permission and
access the CT API with reduced permission scope.
  • Loading branch information
fschrempf committed Oct 20, 2023
1 parent c412b8c commit 6c7954a
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/ctservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,38 @@ exports.getPersonsInGroups = async (site) => {
};

exports.getGroupMemberships = async (groupIds, site) => {
const result = await getGroupsPaginated(
groupIds,
c.GROUPMEMBERS_AP,
[{ key: 'with_deleted', value: 'false' }],
site,
);
const members = [];
result.forEach((el) => {
members.push({
personId: el.personId,
groupId: el.groupId,
groupTypeRoleId: el.groupTypeRoleId,

if (!groupIds) {
const result = await getGroupsPaginated(
null,
c.GROUPMEMBERS_AP,
[{ key: 'with_deleted', value: 'false' }],
site,
);
result.forEach((el) => {
members.push({
personId: el.personId,
groupId: el.groupId,
groupTypeRoleId: el.groupTypeRoleId,
});
});
return members;
}

groupIds.forEach(async (groupId) => {
const result = await getGroupsPaginated(
groupIds,
`${c.GROUPS_AP}/${groupId}/members`,
[{ key: 'with_deleted', value: 'false' }],
site,
);
result.forEach((el) => {
members.push({
personId: el.personId,
groupId: el.groupId,
groupTypeRoleId: el.groupTypeRoleId,
});
});
});
return members;
Expand Down Expand Up @@ -168,7 +188,8 @@ exports.getChurchToolsData = async (site) => {
log.info('Get Person Details from ChurchTools');
const ctPersons = await this.getPersons(ctPersonIds, site);
log.info('Get Group Memberships from ChurchTools');
const ctGroupMembership = await this.getGroupMemberships(allGroupsIds, site);
const groupIds = ctGroups.map((group) => group.id);
const ctGroupMembership = await this.getGroupMemberships(groupIds, site);

return {
groups: ctGroups,
Expand Down

0 comments on commit 6c7954a

Please sign in to comment.