Refactor generation of request_login_url_parameters

This commit is contained in:
Dmitrii Metelkin 2016-08-17 16:43:35 +10:00
parent dfae36ad51
commit e698304f80
3 changed files with 143 additions and 34 deletions

View file

@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die();
use auth_userkey\core_userkey_manager; use auth_userkey\core_userkey_manager;
use auth_userkey\userkey_manager_interface; use auth_userkey\userkey_manager_interface;
require_once($CFG->libdir . "/externallib.php");
require_once($CFG->libdir.'/authlib.php'); require_once($CFG->libdir.'/authlib.php');
/** /**
@ -182,4 +183,81 @@ class auth_plugin_userkey extends auth_plugin_base {
return $CFG->wwwroot . '/auth/userkey/login.php?key=' . $userkey; return $CFG->wwwroot . '/auth/userkey/login.php?key=' . $userkey;
} }
/**
* Return a list of mapping fields.
*
* @return array
*/
public function get_allowed_mapping_fields() {
return array(
'username' => 'username',
'email' => 'email',
'idnumber' => 'idnumber',
);
}
/**
* Return a mapping parameter for request_login_url_parameters().
*
* @return array
*/
protected function get_mapping_parameter() {
$mappingfield = $this->get_mapping_field();
switch ($mappingfield) {
case 'username':
$parameter = array(
'username' => new external_value(
PARAM_USERNAME,
'Username'
),
);
break;
case 'email':
$parameter = array(
'email' => new external_value(
PARAM_EMAIL,
'A valid email address'
),
);
break;
case 'idnumber':
$parameter = array(
'idnumber' => new external_value(
PARAM_RAW,
'An arbitrary ID code number perhaps from the institution'
),
);
break;
default:
$parameter = array();
break;
}
return $parameter;
}
/**
* Return user fields parameters for request_login_url_parameters().
*
* @return array
*/
protected function get_user_fields_parameters() {
// TODO: add more fields here when we implement user creation.
return array();
}
/**
* Return parameters for request_login_url_parameters().
*
* @return array
*/
public function get_request_login_url_user_parameters() {
$parameters = array_merge($this->get_mapping_parameter(), $this->get_user_fields_parameters());
return $parameters;
}
} }

View file

@ -28,49 +28,16 @@ require_once($CFG->dirroot . "/auth/userkey/auth.php");
class auth_userkey_external extends external_api { class auth_userkey_external extends external_api {
public function request_login_url_parameters() { public function request_login_url_parameters() {
// TODO based on settings set required parameter for one of the following: username, email, idnumber.
return new external_function_parameters( return new external_function_parameters(
array( array(
'user' => new external_single_structure( 'user' => new external_single_structure(
array( get_auth_plugin('auth_userkey')->get_request_login_url_user_parameters()
'username' => new external_value(
PARAM_USERNAME,
'Username policy is defined in Moodle security config.'
),
'email' => new external_value(
PARAM_EMAIL,
'A valid and unique email address'
),
'idnumber' => new external_value(
PARAM_RAW,
'An arbitrary ID code number perhaps from the institution'
),
'firstname' => new external_value(
PARAM_NOTAGS,
'The first name(s) of the user',
VALUE_OPTIONAL
),
'lastname' => new external_value(
PARAM_NOTAGS,
'The family name of the user',
VALUE_OPTIONAL
),
'customfields' => new external_multiple_structure(
new external_single_structure(
array(
'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
'value' => new external_value(PARAM_RAW, 'The value of the custom field')
)
), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL
),
)
) )
) )
); );
} }
public function request_login_url($user) { public function request_login_url($user) {
$auth = get_auth_plugin('auth_userkey'); $auth = get_auth_plugin('auth_userkey');
$loginurl = $auth->get_login_url($user); $loginurl = $auth->get_login_url($user);

View file

@ -42,6 +42,8 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
*/ */
public function setUp() { public function setUp() {
global $CFG; global $CFG;
require_once($CFG->libdir . "/externallib.php");
require_once($CFG->dirroot . '/auth/userkey/tests/fake_userkey_manager.php'); require_once($CFG->dirroot . '/auth/userkey/tests/fake_userkey_manager.php');
require_once($CFG->dirroot . '/auth/userkey/auth.php'); require_once($CFG->dirroot . '/auth/userkey/auth.php');
@ -219,4 +221,66 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function test_get_allowed_mapping_fields_list() {
$expected = array(
'username' => 'username',
'email' => 'email',
'idnumber' => 'idnumber',
);
$actual = $this->auth->get_allowed_mapping_fields();
$this->assertEquals($expected, $actual);
}
public function test_get_request_login_url_user_parameters() {
// Check email as it should be set by default.
$expected = array(
'email' => new external_value(
PARAM_EMAIL,
'A valid email address'
),
);
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);
// Check username.
set_config('mappingfield', 'username', 'auth_userkey');
$this->auth = new auth_plugin_userkey();
$expected = array(
'username' => new external_value(
PARAM_USERNAME,
'Username'
),
);
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);
// Check idnumber.
set_config('mappingfield', 'idnumber', 'auth_userkey');
$this->auth = new auth_plugin_userkey();
$expected = array(
'idnumber' => new external_value(
PARAM_RAW,
'An arbitrary ID code number perhaps from the institution'
),
);
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);
// Check some junk field name.
set_config('mappingfield', 'junkfield', 'auth_userkey');
$this->auth = new auth_plugin_userkey();
$expected = array();
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);
}
} }