} else { $groupid = $dbgroup->id; $cli->cli_print('group exists ' . $group); $ldapusers = $instance->get_users_having_attribute_value($group); } // now it does exist see what members should be added/removed $members = array('1' => 'admin'); //must be set otherwise fatal error group_update_members: no group admins listed for group foreach ($ldapusers as $username) { if (isset($currentmembers[$username])) { $id = $currentmembers[$username]; $members[$id] = 'member'; } } if ($CFG->debug_ldap_groupes) { moodle_print_object('new members list : ' . count($members) . ' ', $members); } unset($ldapusers); //try to save memory before memory consuming call to API $result = $dryrun ? false : group_update_members($groupid, $members); if ($result) { $cli->cli_print(" -> added : {$result['added']} removed : {$result['removed']} updated : {$result['updated']}"); } else { $cli->cli_print('-> no change for ' . $group); } unset($members); //break; } } $USER->logout(); // important
function get_users_having_attribute_value($attributevalue) { global $CFG, $DB; //build a filter $filter = $this->config['group_synching_ldap_attribute_attribute'] . '=' . $this->filter_addslashes($attributevalue); // call Moodle ldap_get_userlist that return it as an array with user attributes names $matchings = $this->ldap_get_users($filter); // return the FIRST entry found if (empty($matchings)) { if ($CFG->debug_ldap_groupes) { moodle_print_object('not found', ''); } return array(); } if ($CFG->debug_ldap_groupes) { moodle_print_object('found ', count($matchings) . ' matching users in LDAP'); } return $matchings; }
$cli->cli_print('creating user ' . $ldapusername); // Retrieve information of user from LDAP $ldapdetails = $instance->get_user_info($ldapusername, $ldapattributes); $ldapdetails->username = $ldapusername; //not returned by LDAP $ldapdetails->authinstance = $auth->id; if ($CFG->debug_ldap_groupes) { moodle_print_object("creation de ", $ldapdetails); } //check for used email if (($d1 = get_record('usr', 'email', $ldapdetails->email)) || ($d2 = get_record('artefact_internal_profile_email', 'email', $ldapdetails->email))) { if (empty($d1)) { $d1 = get_record('usr', 'id', $d2->owner); } if ($CFG->debug_ldap_groupes) { moodle_print_object("collision email ", $d1); } $cli->cli_print(get_string('emailalreadytaken', 'auth.internal') . ' ' . $d1->username . ' ' . $ldapdetails->email); $nberrors++; } else { // consumes also a lot of memory not returned to poll if (!$dryrun) { create_user($ldapdetails, array(), $institutionname); } $nbcreated++; } unset($ldapdetails); } else { $cli->cli_print('ignoring LDAP user not in Mahara ' . $ldapusername); } // if ($nbcreated > 500) break;