Skip to content

Commit

Permalink
MDL-16073 add test for connection to external database for authentica…
Browse files Browse the repository at this point in the history
…tion and enrolments
  • Loading branch information
skodak committed Oct 8, 2013
1 parent 7d19bc1 commit 6cf2091
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 8 deletions.
7 changes: 7 additions & 0 deletions admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
$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);
Expand All @@ -97,6 +100,10 @@
$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);
Expand Down
75 changes: 75 additions & 0 deletions auth/db/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function user_login($username, $password) {
}
}

/**
* Connect to external database.
*
* @return ADOConnection
*/
function db_init() {
// Connect to the external database (forcing new connection).
$authdb = ADONewConnection($this->config->type);
Expand Down Expand Up @@ -781,6 +786,76 @@ function ext_addslashes($text) {
}
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 changes: 78 additions & 0 deletions auth/test_settings.php
Original file line number Diff line number Diff line change
@@ -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();
95 changes: 95 additions & 0 deletions enrol/database/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,99 @@ public function restore_role_assignment($instance, $roleid, $userid, $contextid)
}
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 changes: 78 additions & 0 deletions enrol/test_settings.php
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 2 additions & 0 deletions lang/en/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
$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';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
$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';
Expand Down
Loading

0 comments on commit 6cf2091

Please sign in to comment.