Skip to content

Commit

Permalink
Merge pull request #23 from andrewhancox/master_33
Browse files Browse the repository at this point in the history
Master 33
  • Loading branch information
dmitriim authored Jan 29, 2018
2 parents bf33d00 + 4afc205 commit b844571
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 301 deletions.
81 changes: 0 additions & 81 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,87 +191,6 @@ public function can_change_password() {
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.
*
Expand Down
88 changes: 0 additions & 88 deletions settings.html

This file was deleted.

62 changes: 62 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
@@ -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));
}
132 changes: 0 additions & 132 deletions tests/auth_plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,46 +633,6 @@ public function test_get_request_login_url_user_parameters_based_on_plugin_confi
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.
*
Expand All @@ -693,98 +653,6 @@ public function url_data_provider() {
);
}

/**
* 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.
*
Expand Down

0 comments on commit b844571

Please sign in to comment.