MDL-63174 user: core_user_create_users to throw exception on empty names

To be consistent with the web administration UI, we should not allow to
create invalid user records with empty username, lastname or firstname
via the web services.
This commit is contained in:
David Mudrák 2018-08-17 12:33:25 +02:00
parent 6e445671c1
commit 0e99d58c35
2 changed files with 85 additions and 0 deletions

View File

@ -157,6 +157,13 @@ class core_user_external extends external_api {
$userids = array();
$createpassword = false;
foreach ($params['users'] as $user) {
// Make sure that the username, firstname and lastname are not blank.
foreach (array('username', 'firstname', 'lastname') as $fieldname) {
if (trim($user[$fieldname]) === '') {
throw new invalid_parameter_exception('The field '.$fieldname.' cannot be blank');
}
}
// Make sure that the username doesn't already exist.
if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
throw new invalid_parameter_exception('Username already exists: '.$user['username']);

View File

@ -544,6 +544,84 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
$createdusers = core_user_external::create_users(array($user1));
}
/**
* Test create_users with invalid parameters
*
* @dataProvider data_create_users_invalid_parameter
* @param array $data User data to attempt to register.
* @param string $expectmessage Expected exception message.
*/
public function test_create_users_invalid_parameter(array $data, $expectmessage) {
global $USER, $CFG, $DB;
$this->resetAfterTest(true);
$this->assignUserCapability('moodle/user:create', SYSCONTEXTID);
$this->expectException('invalid_parameter_exception');
$this->expectExceptionMessage($expectmessage);
core_user_external::create_users(array($data));
}
/**
* Data provider for {@link self::test_create_users_invalid_parameter()}.
*
* @return array
*/
public function data_create_users_invalid_parameter() {
return [
'blank_username' => [
'data' => [
'username' => '',
'firstname' => 'Foo',
'lastname' => 'Bar',
'email' => 'foobar@example.com',
'createpassword' => 1,
],
'expectmessage' => 'The field username cannot be blank',
],
'blank_firtname' => [
'data' => [
'username' => 'foobar',
'firstname' => "\t \n",
'lastname' => 'Bar',
'email' => 'foobar@example.com',
'createpassword' => 1,
],
'expectmessage' => 'The field firstname cannot be blank',
],
'blank_lastname' => [
'data' => [
'username' => 'foobar',
'firstname' => '0',
'lastname' => ' ',
'email' => 'foobar@example.com',
'createpassword' => 1,
],
'expectmessage' => 'The field lastname cannot be blank',
],
'invalid_email' => [
'data' => [
'username' => 'foobar',
'firstname' => 'Foo',
'lastname' => 'Bar',
'email' => '@foobar',
'createpassword' => 1,
],
'expectmessage' => 'Email address is invalid',
],
'missing_password' => [
'data' => [
'username' => 'foobar',
'firstname' => 'Foo',
'lastname' => 'Bar',
'email' => 'foobar@example.com',
],
'expectmessage' => 'Invalid password: you must provide a password, or set createpassword',
],
];
}
/**
* Test delete_users
*/