diff --git a/auth.php b/auth.php index 31df400..8a152f9 100644 --- a/auth.php +++ b/auth.php @@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die(); use auth_userkey\core_userkey_manager; use auth_userkey\userkey_manager_interface; +require_once($CFG->libdir . "/externallib.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 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; + } } diff --git a/externallib.php b/externallib.php index a0863c3..a7f0c6a 100644 --- a/externallib.php +++ b/externallib.php @@ -28,49 +28,16 @@ require_once($CFG->dirroot . "/auth/userkey/auth.php"); class auth_userkey_external extends external_api { 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( array( 'user' => new external_single_structure( - array( - '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 - ), - ) + get_auth_plugin('auth_userkey')->get_request_login_url_user_parameters() ) ) ); } public function request_login_url($user) { - $auth = get_auth_plugin('auth_userkey'); $loginurl = $auth->get_login_url($user); diff --git a/tests/auth_plugin_test.php b/tests/auth_plugin_test.php index 62dd523..ac8fb9f 100644 --- a/tests/auth_plugin_test.php +++ b/tests/auth_plugin_test.php @@ -42,6 +42,8 @@ class auth_plugin_userkey_testcase extends advanced_testcase { */ public function setUp() { 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/auth.php'); @@ -219,4 +221,66 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $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); + } + }