Add settings

This commit is contained in:
Dmitrii Metelkin 2016-08-18 14:43:57 +10:00
parent 5a5bfc9d23
commit 29d61cad41
4 changed files with 201 additions and 7 deletions

View file

@ -45,7 +45,19 @@ class auth_plugin_userkey extends auth_plugin_base {
* *
* @var userkey_manager_interface * @var userkey_manager_interface
*/ */
private $userkeymanager; protected $userkeymanager;
/**
* Defaults for config form.
*
* @var array
*/
protected $defaults = array(
'mappingfield' => self::DEFAULT_MAPPING_FIELD,
'keylifetime' => 60,
'iprestriction' => 0,
//'createuser' => 0, // TODO: use this field when implementing user creation.
);
/** /**
* Constructor. * Constructor.
@ -94,6 +106,53 @@ class auth_plugin_userkey extends auth_plugin_base {
return false; 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
*/
public function config_form($config, $err, $user_fields) {
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'] = 'User key life time should be a number.';
}
}
/**
* 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. * Set userkey manager.
* *
@ -190,9 +249,9 @@ class auth_plugin_userkey extends auth_plugin_base {
*/ */
public function get_allowed_mapping_fields() { public function get_allowed_mapping_fields() {
return array( return array(
'username' => 'username', 'username' => get_string('username'),
'email' => 'email', 'email' => get_string('email'),
'idnumber' => 'idnumber', 'idnumber' => get_string('idnumber'),
); );
} }

View file

@ -23,3 +23,12 @@
*/ */
$string['pluginname'] = 'User key authentication'; $string['pluginname'] = 'User key authentication';
$string['auth_userkeydescription'] = ''; // TODO: create descripton.
$string['mappingfield'] = 'Mapping field';
$string['mappingfield_desc'] = 'This user field will be used to find relevant user in the LMS.';
$string['iprestriction'] = 'IP restriction';
$string['iprestriction_desc'] = 'If enabled, a user should use the same remote ip to get login URL and logging to to LMS.';
$string['keylifetime'] = 'User key life time';
$string['keylifetime_desc'] = 'Life time in seconds of the each user login key.';
$string['createuser'] = 'Crete user?';
$string['createuser_desc'] = 'If enabled, a new user will be created if fail to find one in LMS.';

61
settings.html Normal file
View file

@ -0,0 +1,61 @@
<?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="40" 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>
<!--UNCOMMENT FOLLOWING WHEN IMPLEMENT USER CREATION.-->
<!--<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>-->
</table>

View file

@ -240,9 +240,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
*/ */
public function test_get_allowed_mapping_fields_list() { public function test_get_allowed_mapping_fields_list() {
$expected = array( $expected = array(
'username' => 'username', 'username' => 'Username',
'email' => 'email', 'email' => 'Email address',
'idnumber' => 'idnumber', 'idnumber' => 'ID number',
); );
$actual = $this->auth->get_allowed_mapping_fields(); $actual = $this->auth->get_allowed_mapping_fields();
@ -303,4 +303,69 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/**
* Test that we can validate config form correctly.
*/
public function test_validate_config_form() {
$form = new stdClass();
$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']);
}
/**
* 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;
$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);
}
} }