/** * cron synchronization script * * @param int $do_updates true to update existing accounts * * @return int */ function sync_users($do_updates = false) { global $CFG, $DB; // process users in Moodle that no longer exist in Drupal $remote_user = $this->config->remote_user; $remote_pw = $this->config->remote_pw; $base_url = $this->config->host_uri; $apiObj = new RemoteAPI($base_url); // Required for authentication, and all other operations: $ret = $apiObj->Login($remote_user, $remote_pw, true); if ($ret->info['http_code'] == 404) { die("ERROR: Login service unreachable!\n"); } if ($ret->info['http_code'] == 401) { die("ERROR: Login failed - check username and password!\n"); } elseif ($ret->info['http_code'] !== 200) { $error = "ERROR: Login to drupal failed with http code " . $ret->info['http_code']; if (!empty($ret->error)) { $error .= PHP_EOL . $ret->error . PHP_EOL; } die($error); } // list external users since last update $vid = isset($this->config->last_vid) ? $this->config->last_vid : 0; $pagesize = $this->config->pagesize; $page = 0; $drupal_users = $apiObj->Index('user', "?vid={$vid},page={$page},pagesize={$pagesize}"); if (is_null($drupal_users) || empty($drupal_users)) { die("ERROR: Problems trying to get index of users!\n"); } // sync users in Drupal with users in Moodle (adding users if needed) print_string('auth_drupalservicesuserstoupdate', 'auth_drupalservices', count($drupal_users)); foreach ($drupal_users as $drupal_user_info) { // get the full user object rather than the prototype from the index service // merge the listing and the full value because if the user is blocked, a full user will not be retrieved $drupal_user = (array) $drupal_user_info + (array) $apiObj->Index("user/{$drupal_user_info->uid}"); // recast drupaluser as an object $drupal_user = (object) $drupal_user; // the drupal services module strips off the mail attribute if the user requested is not // either the user requesting, or a user with administer users permission. // luckily the updates service has the value, so we have to copy it over. $drupal_user->mail = $drupal_user_info->mail; if ($drupal_user_info->uid < 1) { //No anon print "Skipping anon user - uid {$drupal_user->uid}\n"; continue; } print_string('auth_drupalservicesupdateuser', 'auth_drupalservices', array($drupal_user->name . '(' . $drupal_user->uid . ')')); $user = $this->create_update_user($drupal_user); if (empty($user)) { // Something went wrong while creating the user print_error('auth_drupalservicescreateaccount', 'auth_drupalservices', array($drupal_user->name)); continue; //Next user } } // now that all the latest updates have been imported, store the revision point we are at. set_config('last_vid', $drupal_user->vid, 'auth_drupalservices'); // Now do cohorts if ($this->config->cohorts != 0) { $cohort_view = $this->config->cohort_view; print "Updating cohorts using services view - {$cohort_view}\n"; $context = context_system::instance(); //$processed_cohorts_list = array(); $drupal_cohorts = $apiObj->Index($cohort_view); if (is_null($drupal_cohorts)) { print "ERROR: Error retreiving cohorts!\n"; } else { // OK First lets create any Moodle cohorts that are in drupal. foreach ($drupal_cohorts as $drupal_cohort) { if ($drupal_cohort->cohort_name == '') { continue; // We don't want an empty cohort name } $drupal_cohort_list[] = $drupal_cohort->cohort_name; if (!$this->cohort_exists($drupal_cohort->cohort_name)) { $newcohort = new stdClass(); $newcohort->name = $drupal_cohort->cohort_name; $newcohort->idnumber = $drupal_cohort->cohort_id; $newcohort->description = $drupal_cohort->cohort_description; $newcohort->contextid = $context->id; $newcohort->component = 'auth_drupalservices'; $cid = cohort_add_cohort($newcohort); print "Cohort {$drupal_cohort->cohort_name} ({$cid}) created!\n"; } } // Next lets delete any Moodle cohorts that are not in drupal. // Now create a unique array $drupal_cohort_list = array_unique($drupal_cohort_list); //print_r($drupal_cohort_list); $moodle_cohorts = $this->moodle_cohorts(); //print_r($moodle_cohorts); foreach ($moodle_cohorts as $moodle_cohort) { if (array_search($moodle_cohort->name, $drupal_cohort_list) === false) { print "{$moodle_cohort->name} not in drupal - deleteing\n"; cohort_delete_cohort($moodle_cohort); } $moodle_cohorts_list[$moodle_cohort->id] = $moodle_cohort->name; } // Cool. Now lets go through each user and add them to cohorts. // arrays to use? $userlist - list of uids. // $drupal_cohorts - view. $drupal_cohorts_list. Moodle lists. foreach ($userlist as $uid) { $drupal_user_cohort_list = array(); //print "$uid\n"; $user = $DB->get_record('user', array('idnumber' => $uid, 'mnethostid' => $CFG->mnet_localhost_id)); // Get array of cohort names this user belongs to. $drupal_user_cohorts = $this->drupal_user_cohorts($uid, $drupal_cohorts); foreach ($drupal_user_cohorts as $drupal_user_cohort) { //get the cohort id frm the moodle list. $cid = array_search($drupal_user_cohort->cohort_name, $moodle_cohorts_list); //print "$cid\n"; if (!$DB->record_exists('cohort_members', array('cohortid' => $cid, 'userid' => $user->id))) { cohort_add_member($cid, $user->id); print "Added {$user->username} ({$user->id}) to cohort {$drupal_user_cohort->cohort_name}\n"; } // Create a list of enrolled cohorts to use later. $drupal_user_cohort_list[] = $cid; } // Cool. now get this users list of moodle cohorts and compare // with drupal. remove from moodle if needed. $moodle_user_cohorts = $this->moodle_user_cohorts($user); //print_r($moodle_user_cohorts); foreach ($moodle_user_cohorts as $moodle_user_cohort) { if (array_search($moodle_user_cohort->cid, $drupal_user_cohort_list) === false) { cohort_remove_member($moodle_user_cohort->cid, $user->id); print "Removed {$user->username} ({$user->id}) from cohort {$moodle_user_cohort->name}\n"; } } } } } // End of cohorts //LOGOUT if (get_config('auth_drupalservices', 'call_logout_service')) { $ret = $apiObj->Logout(); if (is_null($ret)) { print "ERROR logging out!\n"; } else { print "Logged out from drupal services\n"; } } }
$fulluser_keys = array(); if ($config->cookiedomain) { $drupalsession = $drupalauth->get_drupal_session($config); //now that the cookie domain is discovered, try to reach out to the endpoint to test SSO $apiObj = new RemoteAPI($config->host_uri, 1, $drupalsession); // Connect to Drupal with this session if ($loggedin_user = $apiObj->Connect()) { if ($loggedin_user->user->uid !== false) { debugging("<pre>Service were reached, here's the logged in user:"******"</pre>", DEBUG_DEVELOPER); $endpoint_reachable = true; $tests['session'] = array('success' => true, 'message' => "system/connect: User session data reachable and you are logged in!"); } else { $tests['session'] = array('success' => false, 'message' => "system/connect: User session data reachable but you aren't logged in!"); } //this data should be cached - its possible that a non-admin user $fulluser = (array) $apiObj->Index("user/" . $loggedin_user->user->uid); debugging("<pre>here's the complete user:"******"</pre>", DEBUG_DEVELOPER); // turn the fulluser fields into key/value options $fulluser_keys = array_combine(array_keys($fulluser), array_keys($fulluser)); } else { debugging("could not reach the logged in user " . print_r($loggedin_user, true), DEBUG_DEVELOPER); $tests['session'] = array('success' => false, 'message' => "system/connect: User session data unreachable. Ensure that the server is reachable"); } } //$settings comes from the calling page $drupalssosettings =& $settings; // build an endpoint status item here: $drupalssosettings->add(new admin_setting_heading('drupalsso_status', new lang_string('servicestatus_header', 'auth_drupalservices'), new lang_string('servicestatus_header_info', 'auth_drupalservices'))); //todo: these should be in a fieldset related to sso config. a heading will do for now $drupalssosettings->add(new admin_setting_heading('drupalsso_settings', new lang_string('servicesettings_header', 'auth_drupalservices'), new lang_string('servicesettings_header_info', 'auth_drupalservices'))); $drupalssosettings->add(new admin_setting_configtext('auth_drupalservices/host_uri', new lang_string('auth_drupalservices_host_uri_key', 'auth_drupalservices'), new lang_string('auth_drupalservices_host_uri', 'auth_drupalservices'), $defaults->host_uri, PARAM_TEXT));