Make additional user information optional unless creating a new user.

This commit is contained in:
Andrew Hancox 2017-03-31 13:37:46 +01:00
parent d6c957c298
commit 805e22e8e0
2 changed files with 43 additions and 12 deletions

View file

@ -345,6 +345,17 @@ class auth_plugin_userkey extends auth_plugin_base {
$user['auth'] = 'userkey';
$user['mnethostid'] = $CFG->mnet_localhost_id;
$requiredfieds = ['username', 'email', 'firstname', 'lastname'];
$missingfields = [];
foreach($requiredfieds as $requiredfied) {
if (empty($user[$requiredfied])) {
$missingfields[] = $requiredfied;
}
}
if (!empty($missingfields)) {
throw new invalid_parameter_exception('Unable to create user, missing value(s): ' . implode(',', $missingfields));
}
if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
throw new invalid_parameter_exception('Username already exists: '.$user['username']);
}
@ -592,14 +603,14 @@ class auth_plugin_userkey extends auth_plugin_base {
$mappingfield = $this->get_mapping_field();
if ($this->should_create_user() || $this->should_update_user()) {
$parameters['firstname'] = new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user');
$parameters['lastname'] = new external_value(core_user::get_property_type('lastname'), 'The family name of the user');
$parameters['firstname'] = new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL);
$parameters['lastname'] = new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL);
if ($mappingfield != 'email') {
$parameters['email'] = new external_value(core_user::get_property_type('email'), 'A valid and unique email address');
$parameters['email'] = new external_value(core_user::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL);
}
if ($mappingfield != 'username') {
$parameters['username'] = new external_value(core_user::get_property_type('username'), 'A valid and unique username');
$parameters['username'] = new external_value(core_user::get_property_type('username'), 'A valid and unique username', VALUE_OPTIONAL);
}
}

View file

@ -308,6 +308,26 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->assertEquals('userkey', $userrecord->auth);
}
/**
* Test that we can request a key for a new user.
*/
public function test_missing_data_to_create_user() {
global $CFG, $DB;
set_config('createuser', true, 'auth_userkey');
$this->auth = new auth_plugin_userkey();
$userkeymanager = new \auth_userkey\fake_userkey_manager();
$this->auth->set_userkey_manager($userkeymanager);
$user = new stdClass();
$user->email = 'username@test.com';
$user->ip = '192.168.1.1';
$this->setExpectedException('invalid_parameter_exception', 'Unable to create user, missing value(s): username,firstname,lastname');
$this->auth->get_login_url($user);
}
/**
* Test that when we attempt to create a new user duplicate usernames are caught.
*/
@ -578,10 +598,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->auth = new auth_plugin_userkey();
$expected = array(
'ip' => new external_value(PARAM_HOST, 'User IP address'),
'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'),
'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user'),
'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address'),
'username' => new external_value(core_user::get_property_type('username'), 'A valid and unique username'),
'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL),
'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL),
'username' => new external_value(core_user::get_property_type('username'), 'A valid and unique username', VALUE_OPTIONAL),
);
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);
@ -592,10 +612,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->auth = new auth_plugin_userkey();
$expected = array(
'ip' => new external_value(PARAM_HOST, 'User IP address'),
'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'),
'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user'),
'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address'),
'username' => new external_value(core_user::get_property_type('username'), 'A valid and unique username'),
'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL),
'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL),
'username' => new external_value(core_user::get_property_type('username'), 'A valid and unique username', VALUE_OPTIONAL),
);
$actual = $this->auth->get_request_login_url_user_parameters();
$this->assertEquals($expected, $actual);