Skip to content

Commit b844571

Browse files
authored
Merge pull request catalyst#23 from andrewhancox/master_33
Master 33
2 parents bf33d00 + 4afc205 commit b844571

File tree

4 files changed

+62
-301
lines changed

4 files changed

+62
-301
lines changed

auth.php

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -191,87 +191,6 @@ public function can_change_password() {
191191
return false;
192192
}
193193

194-
/**
195-
* Prints a form for configuring this authentication plugin.
196-
*
197-
* This function is called from admin/auth.php, and outputs a full page with
198-
* a form for configuring this plugin.
199-
*
200-
* @param object $config
201-
* @param object $err
202-
* @param array $userfields
203-
*/
204-
public function config_form($config, $err, $userfields) {
205-
global $CFG, $OUTPUT;
206-
207-
$config = (object) array_merge($this->defaults, (array) $config);
208-
include("settings.html");
209-
}
210-
211-
/**
212-
* A chance to validate form data, and last chance to
213-
* do stuff before it is inserted in config_plugin
214-
*
215-
* @param object $form with submitted configuration settings (without system magic quotes)
216-
* @param array $err array of error messages
217-
*
218-
* @return array of any errors
219-
*/
220-
public function validate_form($form, &$err) {
221-
if ((int)$form->keylifetime == 0) {
222-
$err['keylifetime'] = get_string('incorrectkeylifetime', 'auth_userkey');
223-
}
224-
225-
if (!$this->is_valid_url($form->redirecturl)) {
226-
$err['redirecturl'] = get_string('incorrectredirecturl', 'auth_userkey');
227-
}
228-
229-
if (!$this->is_valid_url($form->ssourl)) {
230-
$err['ssourl'] = get_string('incorrectssourl', 'auth_userkey');
231-
}
232-
233-
}
234-
235-
/**
236-
* Check if provided url is correct.
237-
*
238-
* @param string $url URL to check.
239-
*
240-
* @return bool
241-
*/
242-
protected function is_valid_url($url) {
243-
if (empty($url)) {
244-
return true;
245-
}
246-
247-
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
248-
return false;
249-
}
250-
251-
if (!preg_match("/^(http|https):/", $url)) {
252-
return false;
253-
}
254-
255-
return true;
256-
}
257-
258-
/**
259-
* Process and stores configuration data for this authentication plugin.
260-
*
261-
* @param object $config Config object from the form.
262-
*
263-
* @return bool
264-
*/
265-
public function process_config($config) {
266-
foreach ($this->defaults as $key => $value) {
267-
if (!isset($this->config->$key) || $config->$key != $this->config->$key) {
268-
set_config($key, $config->$key, 'auth_userkey');
269-
}
270-
}
271-
272-
return true;
273-
}
274-
275194
/**
276195
* Set userkey manager.
277196
*

settings.html

Lines changed: 0 additions & 88 deletions
This file was deleted.

settings.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Admin settings and defaults
19+
*
20+
* @package auth_userkey
21+
* @copyright 2017 Stephen Bourget
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
defined('MOODLE_INTERNAL') || die;
26+
27+
if ($ADMIN->fulltree) {
28+
$yesno = array(get_string('no'), get_string('yes'));
29+
$fields = get_auth_plugin('userkey')->get_allowed_mapping_fields();
30+
31+
$settings->add(new admin_setting_configselect('auth_userkey/mappingfield',
32+
new lang_string('mappingfield', 'auth_userkey'),
33+
new lang_string('mappingfield_desc', 'auth_userkey'), 0, $fields));
34+
35+
$settings->add(new admin_setting_configtext('auth_userkey/keylifetime', get_string('keylifetime', 'auth_userkey'),
36+
get_string('keylifetime_desc', 'auth_userkey', 'auth'),
37+
'60', PARAM_INT));
38+
39+
$settings->add(new admin_setting_configselect('auth_userkey/iprestriction',
40+
new lang_string('iprestriction', 'auth_userkey'),
41+
new lang_string('iprestriction_desc', 'auth_userkey'), 0, $yesno));
42+
43+
$settings->add(new admin_setting_configtext('auth_userkey/ipwhitelist', get_string('ipwhitelist', 'auth_userkey'),
44+
get_string('ipwhitelist_desc', 'auth_userkey', 'auth'),
45+
'', PARAM_TEXT));
46+
47+
$settings->add(new admin_setting_configtext('auth_userkey/redirecturl', get_string('redirecturl', 'auth_userkey'),
48+
get_string('redirecturl_desc', 'auth_userkey', 'auth'),
49+
'', PARAM_URL));
50+
51+
$settings->add(new admin_setting_configtext('auth_userkey/ssourl', get_string('ssourl', 'auth_userkey'),
52+
get_string('ssourl_desc', 'auth_userkey', 'auth'),
53+
'', PARAM_URL));
54+
55+
$settings->add(new admin_setting_configselect('auth_userkey/createuser',
56+
new lang_string('createuser', 'auth_userkey'),
57+
new lang_string('createuser_desc', 'auth_userkey'), 0, $yesno));
58+
59+
$settings->add(new admin_setting_configselect('auth_userkey/updateuser',
60+
new lang_string('updateuser', 'auth_userkey'),
61+
new lang_string('updateuser_desc', 'auth_userkey'), 0, $yesno));
62+
}

tests/auth_plugin_test.php

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -633,46 +633,6 @@ public function test_get_request_login_url_user_parameters_based_on_plugin_confi
633633
set_config('updateuser', false, 'auth_userkey');
634634
}
635635

636-
/**
637-
* Test that we can validate keylifetime for config form correctly.
638-
*/
639-
public function test_validate_keylifetime_for_config_form() {
640-
$form = new stdClass();
641-
642-
$form->redirecturl = '';
643-
$form->ssourl = '';
644-
645-
$form->keylifetime = '';
646-
$err = array();
647-
$this->auth->validate_form($form, $err);
648-
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
649-
650-
$form->keylifetime = '0';
651-
$err = array();
652-
$this->auth->validate_form($form, $err);
653-
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
654-
655-
$form->keylifetime = '1';
656-
$err = array();
657-
$this->auth->validate_form($form, $err);
658-
$this->assertFalse(array_key_exists('keylifetime', $err));
659-
660-
$form->keylifetime = 0;
661-
$err = array();
662-
$this->auth->validate_form($form, $err);
663-
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
664-
665-
$form->keylifetime = 1;
666-
$err = array();
667-
$this->auth->validate_form($form, $err);
668-
$this->assertFalse(array_key_exists('keylifetime', $err));
669-
670-
$form->keylifetime = 'rkjflj';
671-
$err = array();
672-
$this->auth->validate_form($form, $err);
673-
$this->assertEquals('User key life time should be a number', $err['keylifetime']);
674-
}
675-
676636
/**
677637
* Data provider for testing URL validation functions.
678638
*
@@ -693,98 +653,6 @@ public function url_data_provider() {
693653
);
694654
}
695655

696-
/**
697-
* Test that we can validate redirecturl for config form correctly.
698-
*
699-
* @dataProvider url_data_provider
700-
*/
701-
702-
/**
703-
* Test that we can validate redirecturl for config form correctly.
704-
*
705-
* @dataProvider url_data_provider
706-
*
707-
* @param string $url URL to test.
708-
* @param string $errortext Expected error text.
709-
*/
710-
public function test_validate_redirecturl_for_config_form($url, $errortext) {
711-
$form = new stdClass();
712-
713-
$form->keylifetime = 10;
714-
$form->ssourl = '';
715-
716-
$form->redirecturl = $url;
717-
$err = array();
718-
$this->auth->validate_form($form, $err);
719-
720-
if (empty($errortext)) {
721-
$this->assertFalse(array_key_exists('redirecturl', $err));
722-
} else {
723-
$this->assertArrayHasKey('redirecturl', $err);
724-
$this->assertEquals($errortext, $err['redirecturl']);
725-
}
726-
}
727-
728-
/**
729-
* Test that we can validate ssourl for config form correctly.
730-
*
731-
* @dataProvider url_data_provider
732-
*
733-
* @param string $url URL to test.
734-
* @param string $errortext Expected error text.
735-
*/
736-
public function test_validate_ssourl_for_config_form($url, $errortext) {
737-
$form = new stdClass();
738-
739-
$form->keylifetime = 10;
740-
$form->redirecturl = '';
741-
$form->ssourl = '';
742-
743-
$form->ssourl = $url;
744-
$err = array();
745-
$this->auth->validate_form($form, $err);
746-
747-
if (empty($errortext)) {
748-
$this->assertFalse(array_key_exists('ssourl', $err));
749-
} else {
750-
$this->assertArrayHasKey('ssourl', $err);
751-
$this->assertEquals($errortext, $err['ssourl']);
752-
}
753-
}
754-
755-
/**
756-
* Test that we can process config form.
757-
*/
758-
public function test_process_config_form() {
759-
$config = get_config('auth_userkey');
760-
761-
$this->assertObjectNotHasAttribute('mappingfield', $config);
762-
$this->assertObjectNotHasAttribute('keylifetime', $config);
763-
$this->assertObjectNotHasAttribute('iprestriction', $config);
764-
765-
$formconfig = new stdClass();
766-
$formconfig->mappingfield = 'email';
767-
$formconfig->keylifetime = 100;
768-
$formconfig->iprestriction = 0;
769-
$formconfig->ipwhitelist = '';
770-
$formconfig->redirecturl = 'http://google.com/';
771-
$formconfig->ssourl = 'http://google.com/';
772-
$formconfig->createuser = false;
773-
$formconfig->updateuser = false;
774-
775-
$this->auth->process_config($formconfig);
776-
777-
$config = get_config('auth_userkey');
778-
$this->assertObjectHasAttribute('mappingfield', $config);
779-
$this->assertObjectHasAttribute('keylifetime', $config);
780-
$this->assertObjectHasAttribute('iprestriction', $config);
781-
782-
$this->assertEquals('email', $config->mappingfield);
783-
$this->assertEquals(100, $config->keylifetime);
784-
$this->assertEquals(0, $config->iprestriction);
785-
$this->assertEquals('http://google.com/', $config->redirecturl);
786-
}
787-
788656
/**
789657
* Test required parameter exception gets thrown id try to login, but key is not set.
790658
*

0 commit comments

Comments
 (0)