Move settings into settings.php

This commit is contained in:
Andrew Hancox 2018-01-23 15:26:54 +00:00
parent bb426a5adb
commit 4afc2055be
4 changed files with 62 additions and 301 deletions

View file

@ -191,87 +191,6 @@ class auth_plugin_userkey extends auth_plugin_base {
return false;
}
/**
* Prints a form for configuring this authentication plugin.
*
* This function is called from admin/auth.php, and outputs a full page with
* a form for configuring this plugin.
*
* @param object $config
* @param object $err
* @param array $userfields
*/
public function config_form($config, $err, $userfields) {
global $CFG, $OUTPUT;
$config = (object) array_merge($this->defaults, (array) $config);
include("settings.html");
}
/**
* A chance to validate form data, and last chance to
* do stuff before it is inserted in config_plugin
*
* @param object $form with submitted configuration settings (without system magic quotes)
* @param array $err array of error messages
*
* @return array of any errors
*/
public function validate_form($form, &$err) {
if ((int)$form->keylifetime == 0) {
$err['keylifetime'] = get_string('incorrectkeylifetime', 'auth_userkey');
}
if (!$this->is_valid_url($form->redirecturl)) {
$err['redirecturl'] = get_string('incorrectredirecturl', 'auth_userkey');
}
if (!$this->is_valid_url($form->ssourl)) {
$err['ssourl'] = get_string('incorrectssourl', 'auth_userkey');
}
}
/**
* Check if provided url is correct.
*
* @param string $url URL to check.
*
* @return bool
*/
protected function is_valid_url($url) {
if (empty($url)) {
return true;
}
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
}
if (!preg_match("/^(http|https):/", $url)) {
return false;
}
return true;
}
/**
* Process and stores configuration data for this authentication plugin.
*
* @param object $config Config object from the form.
*
* @return bool
*/
public function process_config($config) {
foreach ($this->defaults as $key => $value) {
if (!isset($this->config->$key) || $config->$key != $this->config->$key) {
set_config($key, $config->$key, 'auth_userkey');
}
}
return true;
}
/**
* Set userkey manager.
*

View file

@ -1,88 +0,0 @@
<?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/>.
/**
* Settings for the plugin.
*
* @package auth_userkey
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$yesno = array(get_string('no'), get_string('yes'));
$fields = get_auth_plugin('userkey')->get_allowed_mapping_fields();
?>
<table cellspacing="0" cellpadding="5" border="0" class="generaltable">
<tr valign="top">
<?php $field = 'mappingfield' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td>
<?php echo html_writer::select($fields, $field, $config->$field, false) ?>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey') ?></td>
</tr>
<tr valign="top">
<?php $field = 'keylifetime' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><input type="text" size="4" name="<?php echo $field ?>" value="<?php print $config->$field ?>" placeholder="60"><br>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey') ?></td>
</tr>
<tr valign="top">
<?php $field = 'iprestriction' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><?php echo html_writer::select($yesno, $field, $config->$field, false) ?>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey')?></td>
</tr>
<tr valign="top">
<?php $field = 'ipwhitelist' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><input type="text" size="60" name="<?php echo $field ?>" value="<?php print $config->$field ?>" placeholder=""><br>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey') ?></td>
</tr>
<tr valign="top">
<?php $field = 'redirecturl' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><input type="text" size="60" name="<?php echo $field ?>" value="<?php print $config->$field ?>" placeholder=""><br>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey') ?></td>
</tr>
<tr valign="top">
<?php $field = 'ssourl' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><input type="text" size="60" name="<?php echo $field ?>" value="<?php print $config->$field ?>" placeholder=""><br>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey') ?></td>
</tr>
<tr valign="top">
<?php $field = 'createuser' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><?php echo html_writer::select($yesno, $field, $config->$field, false) ?>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey')?></td>
</tr>
<tr valign="top">
<?php $field = 'updateuser' ?>
<td align="right"><label for="<?php echo $field ?>"><?php print_string($field, 'auth_userkey') ?></label></td>
<td><?php echo html_writer::select($yesno, $field, $config->$field, false) ?>
<?php if (isset($err[$field])) { echo $OUTPUT->notification($err[$field], 'notifyfailure'); } ?>
<?php print_string($field.'_desc', 'auth_userkey')?></td>
</tr>
</table>

62
settings.php Normal file
View file

@ -0,0 +1,62 @@
<?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/>.
/**
* Admin settings and defaults
*
* @package auth_userkey
* @copyright 2017 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
if ($ADMIN->fulltree) {
$yesno = array(get_string('no'), get_string('yes'));
$fields = get_auth_plugin('userkey')->get_allowed_mapping_fields();
$settings->add(new admin_setting_configselect('auth_userkey/mappingfield',
new lang_string('mappingfield', 'auth_userkey'),
new lang_string('mappingfield_desc', 'auth_userkey'), 0, $fields));
$settings->add(new admin_setting_configtext('auth_userkey/keylifetime', get_string('keylifetime', 'auth_userkey'),
get_string('keylifetime_desc', 'auth_userkey', 'auth'),
'60', PARAM_INT));
$settings->add(new admin_setting_configselect('auth_userkey/iprestriction',
new lang_string('iprestriction', 'auth_userkey'),
new lang_string('iprestriction_desc', 'auth_userkey'), 0, $yesno));
$settings->add(new admin_setting_configtext('auth_userkey/ipwhitelist', get_string('ipwhitelist', 'auth_userkey'),
get_string('ipwhitelist_desc', 'auth_userkey', 'auth'),
'', PARAM_TEXT));
$settings->add(new admin_setting_configtext('auth_userkey/redirecturl', get_string('redirecturl', 'auth_userkey'),
get_string('redirecturl_desc', 'auth_userkey', 'auth'),
'', PARAM_URL));
$settings->add(new admin_setting_configtext('auth_userkey/ssourl', get_string('ssourl', 'auth_userkey'),
get_string('ssourl_desc', 'auth_userkey', 'auth'),
'', PARAM_URL));
$settings->add(new admin_setting_configselect('auth_userkey/createuser',
new lang_string('createuser', 'auth_userkey'),
new lang_string('createuser_desc', 'auth_userkey'), 0, $yesno));
$settings->add(new admin_setting_configselect('auth_userkey/updateuser',
new lang_string('updateuser', 'auth_userkey'),
new lang_string('updateuser_desc', 'auth_userkey'), 0, $yesno));
}

View file

@ -633,46 +633,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
set_config('updateuser', false, 'auth_userkey');
}
/**
* Test that we can validate keylifetime for config form correctly.
*/
public function test_validate_keylifetime_for_config_form() {
$form = new stdClass();
$form->redirecturl = '';
$form->ssourl = '';
$form->keylifetime = '';
$err = array();
$this->auth->validate_form($form, $err);
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
$form->keylifetime = '0';
$err = array();
$this->auth->validate_form($form, $err);
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
$form->keylifetime = '1';
$err = array();
$this->auth->validate_form($form, $err);
$this->assertFalse(array_key_exists('keylifetime', $err));
$form->keylifetime = 0;
$err = array();
$this->auth->validate_form($form, $err);
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
$form->keylifetime = 1;
$err = array();
$this->auth->validate_form($form, $err);
$this->assertFalse(array_key_exists('keylifetime', $err));
$form->keylifetime = 'rkjflj';
$err = array();
$this->auth->validate_form($form, $err);
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
}
/**
* Data provider for testing URL validation functions.
*
@ -693,98 +653,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
);
}
/**
* Test that we can validate redirecturl for config form correctly.
*
* @dataProvider url_data_provider
*/
/**
* Test that we can validate redirecturl for config form correctly.
*
* @dataProvider url_data_provider
*
* @param string $url URL to test.
* @param string $errortext Expected error text.
*/
public function test_validate_redirecturl_for_config_form($url, $errortext) {
$form = new stdClass();
$form->keylifetime = 10;
$form->ssourl = '';
$form->redirecturl = $url;
$err = array();
$this->auth->validate_form($form, $err);
if (empty($errortext)) {
$this->assertFalse(array_key_exists('redirecturl', $err));
} else {
$this->assertArrayHasKey('redirecturl', $err);
$this->assertEquals($errortext, $err['redirecturl']);
}
}
/**
* Test that we can validate ssourl for config form correctly.
*
* @dataProvider url_data_provider
*
* @param string $url URL to test.
* @param string $errortext Expected error text.
*/
public function test_validate_ssourl_for_config_form($url, $errortext) {
$form = new stdClass();
$form->keylifetime = 10;
$form->redirecturl = '';
$form->ssourl = '';
$form->ssourl = $url;
$err = array();
$this->auth->validate_form($form, $err);
if (empty($errortext)) {
$this->assertFalse(array_key_exists('ssourl', $err));
} else {
$this->assertArrayHasKey('ssourl', $err);
$this->assertEquals($errortext, $err['ssourl']);
}
}
/**
* Test that we can process config form.
*/
public function test_process_config_form() {
$config = get_config('auth_userkey');
$this->assertObjectNotHasAttribute('mappingfield', $config);
$this->assertObjectNotHasAttribute('keylifetime', $config);
$this->assertObjectNotHasAttribute('iprestriction', $config);
$formconfig = new stdClass();
$formconfig->mappingfield = 'email';
$formconfig->keylifetime = 100;
$formconfig->iprestriction = 0;
$formconfig->ipwhitelist = '';
$formconfig->redirecturl = 'http://google.com/';
$formconfig->ssourl = 'http://google.com/';
$formconfig->createuser = false;
$formconfig->updateuser = false;
$this->auth->process_config($formconfig);
$config = get_config('auth_userkey');
$this->assertObjectHasAttribute('mappingfield', $config);
$this->assertObjectHasAttribute('keylifetime', $config);
$this->assertObjectHasAttribute('iprestriction', $config);
$this->assertEquals('email', $config->mappingfield);
$this->assertEquals(100, $config->keylifetime);
$this->assertEquals(0, $config->iprestriction);
$this->assertEquals('http://google.com/', $config->redirecturl);
}
/**
* Test required parameter exception gets thrown id try to login, but key is not set.
*