diff --git a/local/o365/classes/feature/usersync/main.php b/local/o365/classes/feature/usersync/main.php index 9baadda43..c5206020b 100644 --- a/local/o365/classes/feature/usersync/main.php +++ b/local/o365/classes/feature/usersync/main.php @@ -314,9 +314,9 @@ protected function check_usercreationrestriction($aaddata) { \local_o365\utils::debug('Could not find group (1)', 'check_usercreationrestriction', $group); return false; } - $members = $apiclient->get_group_members($group['id']); - foreach ($members['value'] as $member) { - if ($member['id'] === $aaddata['id']) { + $usersgroups = $apiclient->get_users_groups($group['id'],$aaddata['id']); + foreach ($usersgroups['value'] as $usergroup) { + if ($group['id'] === $usergroup) { return true; } } diff --git a/local/o365/classes/rest/unified.php b/local/o365/classes/rest/unified.php index b6b48ea04..2d7cd45ef 100644 --- a/local/o365/classes/rest/unified.php +++ b/local/o365/classes/rest/unified.php @@ -412,6 +412,21 @@ public function restore_deleted_group($objectid) { return $response; } + /** + * Get a list of all groups a user is member of. + * + * @param string $groupobjectid The object ID of the group. + * @param string $userobjecttid The user ID. + * @return array Array of groups user is member of. + */ + public function get_users_groups($groupobjectid, $userobjectid) { + $endpoint = 'users/'.$userobjectid.'/getMemberGroups'; + $postdata = '{ "securityEnabledOnly": false }'; + $response = $this->apicall('post', $endpoint, $postdata); + $expectedparams = ['value' => null]; + return $this->process_apicall_response($response, $expectedparams); + } + /** * Get a list of group members. *