/** * Return one array containing all the tasks that have been included * in the restore process. Note that these tasks aren't built (they * haven't steps nor ids data available) */ public static function get_included_tasks($restoreid) { $rc = restore_controller_dbops::load_controller($restoreid); $tasks = $rc->get_plan()->get_tasks(); $includedtasks = array(); foreach ($tasks as $key => $task) { // Calculate if the task is being included $included = false; // blocks, based in blocks setting and parent activity/course if ($task instanceof restore_block_task) { if (!$task->get_setting_value('blocks')) { // Blocks not included, continue continue; } $parent = basename(dirname(dirname($task->get_taskbasepath()))); if ($parent == 'course') { // Parent is course, always included if present $included = true; } else { // Look for activity_included setting $included = $task->get_setting_value($parent . '_included'); } // ativities, based on included setting } else { if ($task instanceof restore_activity_task) { $included = $task->get_setting_value('included'); // sections, based on included setting } else { if ($task instanceof restore_section_task) { $included = $task->get_setting_value('included'); // course always included if present } else { if ($task instanceof restore_course_task) { $included = true; } } } } // If included, add it if ($included) { $includedtasks[] = $task; } } return $includedtasks; }
public static function load_controller($restoreid) { // Load controller from persistent storage // TODO: flag the controller as available. Operations on it can continue $controller = restore_controller_dbops::load_controller($restoreid); $controller->log('loading controller from db', backup::LOG_DEBUG); return $controller; }
/** * Check all the included users, deciding the action to perform * for each one (mapping / creation) and returning one array * of problems in case something is wrong (lack of permissions, * conficts) * * @param string $restoreid Restore id * @param int $courseid Course id * @param int $userid User id * @param bool $samesite True if restore is to same site * @param \core\progress\base $progress Progress reporter */ public static function precheck_included_users($restoreid, $courseid, $userid, $samesite, \core\progress\base $progress) { global $CFG, $DB; // To return any problem found $problems = array(); // We are going to map mnethostid, so load all the available ones $mnethosts = $DB->get_records('mnet_host', array(), 'wwwroot', 'wwwroot, id'); // Calculate the context we are going to use for capability checking $context = context_course::instance($courseid); // When conflicting users are detected we may need original site info. $restoreinfo = restore_controller_dbops::load_controller($restoreid)->get_info(); // Calculate if we have perms to create users, by checking: // to 'moodle/restore:createuser' and 'moodle/restore:userinfo' // and also observe $CFG->disableusercreationonrestore $cancreateuser = false; if (has_capability('moodle/restore:createuser', $context, $userid) and has_capability('moodle/restore:userinfo', $context, $userid) and empty($CFG->disableusercreationonrestore)) { // Can create users $cancreateuser = true; } // Prepare for reporting progress. $conditions = array('backupid' => $restoreid, 'itemname' => 'user'); $max = $DB->count_records('backup_ids_temp', $conditions); $done = 0; $progress->start_progress('Checking users', $max); // Iterate over all the included users $rs = $DB->get_recordset('backup_ids_temp', $conditions, '', 'itemid, info'); foreach ($rs as $recuser) { $user = (object) backup_controller_dbops::decode_backup_temp_info($recuser->info); // Find the correct mnethostid for user before performing any further check if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) { $user->mnethostid = $CFG->mnet_localhost_id; } else { // fast url-to-id lookups if (isset($mnethosts[$user->mnethosturl])) { $user->mnethostid = $mnethosts[$user->mnethosturl]->id; } else { $user->mnethostid = $CFG->mnet_localhost_id; } } // Now, precheck that user and, based on returned results, annotate action/problem $usercheck = self::precheck_user($user, $samesite, $restoreinfo->original_site_identifier_hash); if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to // Annotate it, for later process. Set newitemid to mapping user->id self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $usercheck->id); } else { if ($usercheck === false) { // Found conflict, report it as problem if (!get_config('backup', 'import_general_duplicate_admin_allowed')) { $problems[] = get_string('restoreuserconflict', '', $user->username); } else { if ($user->username == 'admin') { if (!$cancreateuser) { $problems[] = get_string('restorecannotcreateuser', '', $user->username); } if ($user->mnethostid != $CFG->mnet_localhost_id) { $problems[] = get_string('restoremnethostidmismatch', '', $user->username); } if (!$problems) { // Duplicate admin allowed, append original site idenfitier to username. $user->username .= '_' . $restoreinfo->original_site_identifier_hash; self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, 0, null, (array) $user); } } } } else { if ($usercheck === true) { // User needs to be created, check if we are able if ($cancreateuser) { // Can create user, set newitemid to 0 so will be created later self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, 0, null, (array) $user); } else { // Cannot create user, report it as problem $problems[] = get_string('restorecannotcreateuser', '', $user->username); } } else { // Shouldn't arrive here ever, something is for sure wrong. Exception throw new restore_dbops_exception('restore_error_processing_user', $user->username); } } } $done++; $progress->progress($done); } $rs->close(); $progress->end_progress(); return $problems; }