MDL-16073 add test for connection to external database for authentication and enrolments

This commit is contained in:
Petr Škoda 2013-09-30 22:39:21 +02:00
parent 7d19bc1353
commit 6cf2091571
8 changed files with 362 additions and 8 deletions

View File

@ -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);

View File

@ -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:<br />'.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();
}
}

78
auth/test_settings.php Normal file
View File

@ -0,0 +1,78 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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();

View File

@ -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:<br />'.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:<br />'.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();
}
}

78
enrol/test_settings.php Normal file
View File

@ -0,0 +1,78 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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();

View File

@ -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';

View File

@ -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';

View File

@ -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 = "<a href=\"auth_config.php?auth=manual\">{$txt->settings}</a>";
//$settings = "<a href=\"settings.php?section=authsettingmanual\">{$txt->settings}</a>";
$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 = "<a href=\"auth_config.php?auth=nologin\">{$txt->settings}</a>";
$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;
}