diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 0865b75e779..6265aaccae1 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -87,6 +87,9 @@ if ($hassiteconfig) { $temp->add(new admin_setting_configtext('recaptchaprivatekey', new lang_string('recaptchaprivatekey', 'admin'), new lang_string('configrecaptchaprivatekey', 'admin'), '', PARAM_NOTAGS)); $ADMIN->add('authsettings', $temp); + $temp = new admin_externalpage('authtestsettings', get_string('testsettings', 'core_auth'), new moodle_url("/auth/test_settings.php"), 'moodle/site:config', true); + $ADMIN->add('authsettings', $temp); + foreach (core_plugin_manager::instance()->get_plugins_of_type('auth') as $plugin) { /** @var \core\plugininfo\auth $plugin */ $plugin->load_settings($ADMIN, 'authsettings', $hassiteconfig); @@ -97,6 +100,10 @@ if ($hassiteconfig) { $temp = new admin_settingpage('manageenrols', new lang_string('manageenrols', 'enrol')); $temp->add(new admin_setting_manageenrols()); $ADMIN->add('enrolments', $temp); + + $temp = new admin_externalpage('enroltestsettings', get_string('testsettings', 'core_enrol'), new moodle_url("/enrol/test_settings.php"), 'moodle/site:config', true); + $ADMIN->add('enrolments', $temp); + foreach(core_plugin_manager::instance()->get_plugins_of_type('enrol') as $plugin) { /** @var \core\plugininfo\enrol $plugin */ $plugin->load_settings($ADMIN, 'enrolments', $hassiteconfig); diff --git a/auth/db/auth.php b/auth/db/auth.php index a01fc6e9801..333142af3b2 100644 --- a/auth/db/auth.php +++ b/auth/db/auth.php @@ -134,6 +134,11 @@ class auth_plugin_db extends auth_plugin_base { } } + /** + * Connect to external database. + * + * @return ADOConnection + */ function db_init() { // Connect to the external database (forcing new connection). $authdb = ADONewConnection($this->config->type); @@ -781,6 +786,76 @@ class auth_plugin_db extends auth_plugin_base { } return $text; } + + /** + * Test if settings are ok, print info to output. + * @private + */ + public function test_settings() { + global $CFG, $OUTPUT; + + // NOTE: this is not localised intentionally, admins are supposed to understand English at least a bit... + + raise_memory_limit(MEMORY_HUGE); + + if (empty($this->config->table)) { + echo $OUTPUT->notification('External table not specified.', 'notifyproblem'); + return; + } + + if (empty($this->config->fielduser)) { + echo $OUTPUT->notification('External user field not specified.', 'notifyproblem'); + return; + } + + $olddebug = $CFG->debug; + $olddisplay = ini_get('display_errors'); + ini_set('display_errors', '1'); + $CFG->debug = DEBUG_DEVELOPER; + $olddebugauthdb = $this->config->debugauthdb; + $this->config->debugauthdb = 1; + error_reporting($CFG->debug); + + $adodb = $this->db_init(); + + if (!$adodb or !$adodb->IsConnected()) { + $this->config->debugauthdb = $olddebugauthdb; + $CFG->debug = $olddebug; + ini_set('display_errors', $olddisplay); + error_reporting($CFG->debug); + ob_end_flush(); + + echo $OUTPUT->notification('Cannot connect the database.', 'notifyproblem'); + return; + } + + $rs = $adodb->Execute("SELECT * + FROM {$this->config->table} + WHERE {$this->config->fielduser} <> 'random_unlikely_username'"); // Any unlikely name is ok here. + + if (!$rs) { + echo $OUTPUT->notification('Can not read external table.', 'notifyproblem'); + + } else if ($rs->EOF) { + echo $OUTPUT->notification('External table is empty.', 'notifyproblem'); + $rs->close(); + + } else { + $fields_obj = $rs->FetchObj(); + $columns = array_keys((array)$fields_obj); + + echo $OUTPUT->notification('External table contains following columns:
'.implode(', ', $columns), 'notifysuccess'); + $rs->close(); + } + + $adodb->Close(); + + $this->config->debugauthdb = $olddebugauthdb; + $CFG->debug = $olddebug; + ini_set('display_errors', $olddisplay); + error_reporting($CFG->debug); + ob_end_flush(); + } } diff --git a/auth/test_settings.php b/auth/test_settings.php new file mode 100644 index 00000000000..a0ff227d42f --- /dev/null +++ b/auth/test_settings.php @@ -0,0 +1,78 @@ +. + +/** + * Test auth settings. + * + * @package core_auth + * @copyright 2013 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require(__DIR__.'../../config.php'); +require_once("$CFG->libdir/adminlib.php"); + +$auth = optional_param('auth', '', PARAM_RAW); +if (!core_component::is_valid_plugin_name('auth', $auth)) { + $auth = ''; +} else if (!file_exists("$CFG->dirroot/auth/$auth/auth.php")) { + $auth = ''; +} + +require_login(); +require_capability('moodle/site:config', context_system::instance()); + +navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section'=>'manageauths'))); +admin_externalpage_setup('authtestsettings'); + +$returnurl = new moodle_url('/admin/settings.php', array('section'=>'manageauths')); + +echo $OUTPUT->header(); + +if (!$auth) { + $options = array(); + $plugins = core_component::get_plugin_list('auth'); + foreach ($plugins as $name => $fulldir) { + $plugin = get_auth_plugin($name); + if (!$plugin or !method_exists($plugin, 'test_settings')) { + continue; + } + $options[$name] = get_string('pluginname', 'auth_'.$name); + } + + if (!$options) { + redirect($returnurl); + } + + echo $OUTPUT->heading(get_string('testsettings', 'core_auth')); + + $url = new moodle_url('/auth/test_settings.php', array('sesskey'=>sesskey())); + echo $OUTPUT->single_select($url, 'auth', $options); + + echo $OUTPUT->footer(); +} + +$plugin = get_auth_plugin($auth); +if (!$plugin or !method_exists($plugin, 'test_settings')) { + redirect($returnurl); +} + +echo $OUTPUT->heading(get_string('testsettingsheading', 'core_auth', get_string('pluginname', 'auth_'.$auth))); + +$plugin->test_settings(); + +echo $OUTPUT->continue_button($returnurl); +echo $OUTPUT->footer(); diff --git a/enrol/database/lib.php b/enrol/database/lib.php index 052458a1e05..cde6d625213 100644 --- a/enrol/database/lib.php +++ b/enrol/database/lib.php @@ -938,4 +938,99 @@ class enrol_database_plugin extends enrol_plugin { } role_assign($roleid, $userid, $contextid, 'enrol_'.$this->get_name(), $instance->id); } + + /** + * Test plugin settings, print info to output. + */ + public function test_settings() { + global $CFG, $OUTPUT; + + // NOTE: this is not localised intentionally, admins are supposed to understand English at least a bit... + + raise_memory_limit(MEMORY_HUGE); + + $this->load_config(); + + $enroltable = $this->get_config('remoteenroltable'); + $coursetable = $this->get_config('newcoursetable'); + + if (empty($enroltable)) { + echo $OUTPUT->notification('External enrolment table not specified.', 'notifyproblem'); + } + + if (empty($coursetable)) { + echo $OUTPUT->notification('External course table not specified.', 'notifyproblem'); + } + + if (empty($coursetable) and empty($enroltable)) { + return; + } + + $olddebug = $CFG->debug; + $olddisplay = ini_get('display_errors'); + ini_set('display_errors', '1'); + $CFG->debug = DEBUG_DEVELOPER; + $olddebugdb = $this->config->debugdb; + $this->config->debugdb = 1; + error_reporting($CFG->debug); + + $adodb = $this->db_init(); + + if (!$adodb or !$adodb->IsConnected()) { + $this->config->debugdb = $olddebugdb; + $CFG->debug = $olddebug; + ini_set('display_errors', $olddisplay); + error_reporting($CFG->debug); + ob_end_flush(); + + echo $OUTPUT->notification('Cannot connect the database.', 'notifyproblem'); + return; + } + + if (!empty($enroltable)) { + $rs = $adodb->Execute("SELECT * + FROM $enroltable"); + if (!$rs) { + echo $OUTPUT->notification('Can not read external enrol table.', 'notifyproblem'); + + } else if ($rs->EOF) { + echo $OUTPUT->notification('External enrol table is empty.', 'notifyproblem'); + $rs->Close(); + + } else { + $fields_obj = $rs->FetchObj(); + $columns = array_keys((array)$fields_obj); + + echo $OUTPUT->notification('External enrolment table contains following columns:
'.implode(', ', $columns), 'notifysuccess'); + $rs->Close(); + } + } + + if (!empty($coursetable)) { + $rs = $adodb->Execute("SELECT * + FROM $coursetable"); + if (!$rs) { + echo $OUTPUT->notification('Can not read external course table.', 'notifyproblem'); + + } else if ($rs->EOF) { + echo $OUTPUT->notification('External course table is empty.', 'notifyproblem'); + $rs->Close(); + + } else { + $fields_obj = $rs->FetchObj(); + $columns = array_keys((array)$fields_obj); + + echo $OUTPUT->notification('External course table contains following columns:
'.implode(', ', $columns), 'notifysuccess'); + $rs->Close(); + } + } + + $adodb->Close(); + + $this->config->debugdb = $olddebugdb; + $CFG->debug = $olddebug; + ini_set('display_errors', $olddisplay); + error_reporting($CFG->debug); + ob_end_flush(); + } } diff --git a/enrol/test_settings.php b/enrol/test_settings.php new file mode 100644 index 00000000000..b8ae94a9a42 --- /dev/null +++ b/enrol/test_settings.php @@ -0,0 +1,78 @@ +. + +/** + * Test enrol plugin settings. + * + * @package core_enrol + * @copyright 2013 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require(__DIR__.'../../config.php'); +require_once("$CFG->libdir/adminlib.php"); + +$enrol = optional_param('enrol', '', PARAM_RAW); +if (!core_component::is_valid_plugin_name('enrol', $enrol)) { + $enrol = ''; +} else if (!file_exists("$CFG->dirroot/enrol/$enrol/lib.php")) { + $enrol = ''; +} + +require_login(); +require_capability('moodle/site:config', context_system::instance()); + +navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section'=>'manageenrols'))); +admin_externalpage_setup('enroltestsettings'); + +$returnurl = new moodle_url('/admin/settings.php', array('section'=>'manageenrols')); + +echo $OUTPUT->header(); + +if (!$enrol) { + $options = array(); + $plugins = core_component::get_plugin_list('enrol'); + foreach ($plugins as $name => $fulldir) { + $plugin = enrol_get_plugin($name); + if (!$plugin or !method_exists($plugin, 'test_settings')) { + continue; + } + $options[$name] = get_string('pluginname', 'enrol_'.$name); + } + + if (!$options) { + redirect($returnurl); + } + + echo $OUTPUT->heading(get_string('testsettings', 'core_enrol')); + + $url = new moodle_url('/enrol/test_settings.php', array('sesskey'=>sesskey())); + echo $OUTPUT->single_select($url, 'enrol', $options); + + echo $OUTPUT->footer(); +} + +$plugin = enrol_get_plugin($enrol); +if (!$plugin or !method_exists($plugin, 'test_settings')) { + redirect($returnurl); +} + +echo $OUTPUT->heading(get_string('testsettingsheading', 'core_enrol', get_string('pluginname', 'enrol_'.$enrol))); + +$plugin->test_settings(); + +echo $OUTPUT->continue_button($returnurl); +echo $OUTPUT->footer(); diff --git a/lang/en/auth.php b/lang/en/auth.php index ac2377099b0..6e8a439115a 100644 --- a/lang/en/auth.php +++ b/lang/en/auth.php @@ -133,6 +133,8 @@ $string['stdchangepassword_expl'] = 'If the external authentication system allow $string['stdchangepassword_explldap'] = 'NOTE: It is recommended that you use LDAP over an SSL encrypted tunnel (ldaps://) if the LDAP server is remote.'; $string['suspended'] = 'Suspended account'; $string['suspended_help'] = 'Suspended user accounts cannot log in or use web services, and any outgoing messages are discarded.'; +$string['testsettings'] = 'Test settings'; +$string['testsettingsheading'] = 'Test authentication settings - {$a}'; $string['unlocked'] = 'Unlocked'; $string['unlockedifempty'] = 'Unlocked if empty'; $string['update_never'] = 'Never'; diff --git a/lang/en/enrol.php b/lang/en/enrol.php index ac01907560b..cbae109d031 100644 --- a/lang/en/enrol.php +++ b/lang/en/enrol.php @@ -110,6 +110,8 @@ $string['rolefromcategory'] = '{$a->role} (Inherited from course category)'; $string['rolefromsystem'] = '{$a->role} (Assigned at site level)'; $string['startdatetoday'] = 'Today'; $string['synced'] = 'Synced'; +$string['testsettings'] = 'Test settings'; +$string['testsettingsheading'] = 'Test enrol settings - {$a}'; $string['totalenrolledusers'] = '{$a} enrolled users'; $string['totalotherusers'] = '{$a} other users'; $string['unassignnotpermitted'] = 'You do not have permission to unassign roles in this course'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 94d600138b6..380e0c765c0 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -5045,6 +5045,7 @@ class admin_setting_manageenrols extends admin_setting { $struninstall = get_string('uninstallplugin', 'core_admin'); $strusage = get_string('enrolusage', 'enrol'); $strversion = get_string('version'); + $strtest = get_string('testsettings', 'core_enrol'); $pluginmanager = core_plugin_manager::instance(); @@ -5070,8 +5071,8 @@ class admin_setting_manageenrols extends admin_setting { $return .= $OUTPUT->box_start('generalbox enrolsui'); $table = new html_table(); - $table->head = array(get_string('name'), $strusage, $strversion, $strenable, $strup.'/'.$strdown, $strsettings, $struninstall); - $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign'); + $table->head = array(get_string('name'), $strusage, $strversion, $strenable, $strup.'/'.$strdown, $strsettings, $strtest, $struninstall); + $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign'); $table->id = 'courseenrolmentplugins'; $table->attributes['class'] = 'admintable generaltable'; $table->data = array(); @@ -5159,8 +5160,14 @@ class admin_setting_manageenrols extends admin_setting { $uninstall = html_writer::link($uninstallurl, $struninstall); } + $test = ''; + if (!empty($enrols_available[$enrol]) and method_exists($enrols_available[$enrol], 'test_settings')) { + $url = new moodle_url('/enrol/test_settings.php', array('enrol'=>$enrol, 'sesskey'=>sesskey())); + $test = html_writer::link($url, $strtest); + } + // Add a row to the table. - $row = new html_table_row(array($icon.$displayname, $usage, $version, $hideshow, $updown, $settings, $uninstall)); + $row = new html_table_row(array($icon.$displayname, $usage, $version, $hideshow, $updown, $settings, $test, $uninstall)); if ($class) { $row->attributes['class'] = $class; } @@ -5582,6 +5589,7 @@ class admin_setting_manageauths extends admin_setting { 'up', 'down', 'none', 'users')); $txt->updown = "$txt->up/$txt->down"; $txt->uninstall = get_string('uninstallplugin', 'core_admin'); + $txt->testsettings = get_string('testsettings', 'core_auth'); $authsavailable = core_component::get_plugin_list('auth'); get_enabled_auth_plugins(true); // fix the list of enabled auths @@ -5595,8 +5603,10 @@ class admin_setting_manageauths extends admin_setting { $displayauths = array(); $registrationauths = array(); $registrationauths[''] = $txt->disable; + $authplugins = array(); foreach ($authsenabled as $auth) { $authplugin = get_auth_plugin($auth); + $authplugins[$auth] = $authplugin; /// Get the auth title (from core or own auth lang files) $authtitle = $authplugin->get_title(); /// Apply titles @@ -5611,6 +5621,7 @@ class admin_setting_manageauths extends admin_setting { continue; //already in the list } $authplugin = get_auth_plugin($auth); + $authplugins[$auth] = $authplugin; /// Get the auth title (from core or own auth lang files) $authtitle = $authplugin->get_title(); /// Apply titles @@ -5624,8 +5635,8 @@ class admin_setting_manageauths extends admin_setting { $return .= $OUTPUT->box_start('generalbox authsui'); $table = new html_table(); - $table->head = array($txt->name, $txt->users, $txt->enable, $txt->updown, $txt->settings, $txt->uninstall); - $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign'); + $table->head = array($txt->name, $txt->users, $txt->enable, $txt->updown, $txt->settings, $txt->testsettings, $txt->uninstall); + $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign'); $table->data = array(); $table->attributes['class'] = 'admintable generaltable'; $table->id = 'manageauthtable'; @@ -5635,11 +5646,11 @@ class admin_setting_manageauths extends admin_setting { $settings = "{$txt->settings}"; //$settings = "{$txt->settings}"; $usercount = $DB->count_records('user', array('auth'=>'manual', 'deleted'=>0)); - $table->data[] = array($displayname, $usercount, '', '', $settings, ''); + $table->data[] = array($displayname, $usercount, '', '', $settings, '', ''); $displayname = $displayauths['nologin']; $settings = "{$txt->settings}"; $usercount = $DB->count_records('user', array('auth'=>'nologin', 'deleted'=>0)); - $table->data[] = array($displayname, $usercount, '', '', $settings, ''); + $table->data[] = array($displayname, $usercount, '', '', $settings, '', ''); // iterate through auth plugins and add to the display table @@ -5703,8 +5714,14 @@ class admin_setting_manageauths extends admin_setting { $uninstall = html_writer::link($uninstallurl, $txt->uninstall); } + $test = ''; + if (!empty($authplugins[$auth]) and method_exists($authplugins[$auth], 'test_settings')) { + $url = new moodle_url('/auth/test_settings.php', array('auth'=>$auth, 'sesskey'=>sesskey())); + $test = html_writer::link($url, $txt->testsettings); + } + // Add a row to the table. - $row = new html_table_row(array($displayname, $usercount, $hideshow, $updown, $settings, $uninstall)); + $row = new html_table_row(array($displayname, $usercount, $hideshow, $updown, $settings, $test, $uninstall)); if ($class) { $row->attributes['class'] = $class; }