From 4afc2055be033575753247bdb036880b12940de7 Mon Sep 17 00:00:00 2001 From: Andrew Hancox Date: Tue, 23 Jan 2018 15:26:54 +0000 Subject: [PATCH] Move settings into settings.php --- auth.php | 81 ----------------------- settings.html | 88 ------------------------- settings.php | 62 +++++++++++++++++ tests/auth_plugin_test.php | 132 ------------------------------------- 4 files changed, 62 insertions(+), 301 deletions(-) delete mode 100644 settings.html create mode 100644 settings.php diff --git a/auth.php b/auth.php index 05570c0..b196694 100644 --- a/auth.php +++ b/auth.php @@ -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. * diff --git a/settings.html b/settings.html deleted file mode 100644 index d82c526..0000000 --- a/settings.html +++ /dev/null @@ -1,88 +0,0 @@ -. - -/** -* 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(); - -?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- $field, false) ?> - notification($err[$field], 'notifyfailure'); } ?> -

- notification($err[$field], 'notifyfailure'); } ?> -
$field, false) ?> - notification($err[$field], 'notifyfailure'); } ?> -

- notification($err[$field], 'notifyfailure'); } ?> -

- notification($err[$field], 'notifyfailure'); } ?> -

- notification($err[$field], 'notifyfailure'); } ?> -
$field, false) ?> - notification($err[$field], 'notifyfailure'); } ?> -
$field, false) ?> - notification($err[$field], 'notifyfailure'); } ?> -
- diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..10c3738 --- /dev/null +++ b/settings.php @@ -0,0 +1,62 @@ +. + +/** + * 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)); +} diff --git a/tests/auth_plugin_test.php b/tests/auth_plugin_test.php index abec749..ccd81dc 100644 --- a/tests/auth_plugin_test.php +++ b/tests/auth_plugin_test.php @@ -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. *