Skip to content

Commit

Permalink
EZP-28902: Creating a user with existing username results in Internal…
Browse files Browse the repository at this point in the history
… Server Error. (#2279)
  • Loading branch information
mikadamczyk authored and Łukasz Serwatka committed Mar 19, 2018
1 parent 87a68d8 commit d987eea
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
66 changes: 65 additions & 1 deletion eZ/Publish/Core/FieldType/Tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/
namespace eZ\Publish\Core\FieldType\Tests;

use eZ\Publish\Core\Persistence\Cache\UserHandler;
use eZ\Publish\Core\FieldType\User\Type as UserType;
use eZ\Publish\Core\FieldType\User\Value as UserValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\FieldType\ValidationError;

/**
* @group fieldType
Expand All @@ -31,7 +33,8 @@ class UserTest extends FieldTypeTest
*/
protected function createFieldTypeUnderTest()
{
$fieldType = new UserType();
$userHandler = $this->createMock(UserHandler::class);
$fieldType = new UserType($userHandler);
$fieldType->setTransformationProcessor($this->getTransformationProcessorMock());

return $fieldType;
Expand Down Expand Up @@ -295,6 +298,67 @@ public function provideInputForFromHash()
);
}

/**
* Provides data sets with validator configuration and/or field settings and
* field value which are considered valid by the {@link validate()} method.
*
* @return array
*/
public function provideValidDataForValidate()
{
return [
[
[],
new UserValue([
'hasStoredLogin' => true,
'contentId' => 23,
'login' => 'sindelfingen',
'email' => '[email protected]',
'passwordHash' => '1234567890abcdef',
'passwordHashType' => 'md5',
'enabled' => true,
'maxLogin' => 1000,
]),
],
];
}

/**
* Provides data sets with validator configuration and/or field settings,
* field value and corresponding validation errors returned by
* the {@link validate()} method.
*
* @return array
*/
public function provideInvalidDataForValidate()
{
return [
[
[],
new UserValue([
'hasStoredLogin' => false,
'contentId' => 23,
'login' => 'sindelfingen',
'email' => '[email protected]',
'passwordHash' => '1234567890abcdef',
'passwordHashType' => 'md5',
'enabled' => true,
'maxLogin' => 1000,
]),
[
new ValidationError(
"The user login '%login%' is used by another user. You must enter a unique login.",
null,
[
'%login%' => 'sindelfingen',
],
'username'
),
],
],
];
}

protected function provideFieldTypeIdentifier()
{
return 'ezuser';
Expand Down
55 changes: 55 additions & 0 deletions eZ/Publish/Core/FieldType/User/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
namespace eZ\Publish\Core\FieldType\User;

use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\Core\FieldType\ValidationError;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Persistence\Cache\UserHandler;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;

/**
* The User field type.
Expand All @@ -20,6 +24,17 @@
*/
class Type extends FieldType
{
/** @var \eZ\Publish\Core\Persistence\Cache\UserHandler */
protected $userHandler;

/**
* @param \eZ\Publish\Core\Persistence\Cache\UserHandler $userHandler
*/
public function __construct(UserHandler $userHandler)
{
$this->userHandler = $userHandler;
}

/**
* Returns the field type identifier for this field type.
*
Expand Down Expand Up @@ -190,4 +205,44 @@ public function fromPersistenceValue(FieldValue $fieldValue)
{
return $this->acceptValue($fieldValue->externalData);
}

/**
* Validates a field based on the validators in the field definition.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
* @param \eZ\Publish\Core\FieldType\User\Value $fieldValue The field value for which an action is performed
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
{
$errors = [];

if ($this->isEmptyValue($fieldValue)) {
return $errors;
}

if (!$fieldValue->hasStoredLogin) {
try {
$login = $fieldValue->login;
$this->userHandler->loadByLogin($login);

// If you want to change this ValidationError message, please remember to change it also in Repository Forms in lib/Validator/Constraints/FieldValueValidatorMessages class
$errors[] = new ValidationError(
"The user login '%login%' is used by another user. You must enter a unique login.",
null,
[
'%login%' => $login,
],
'username'
);
} catch (NotFoundException $e) {
// Do nothing
}
}

return $errors;
}
}
1 change: 1 addition & 0 deletions eZ/Publish/Core/settings/fieldtypes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ services:
ezpublish.fieldType.ezuser:
class: "%ezpublish.fieldType.ezuser.class%"
parent: ezpublish.fieldType
arguments: ["@ezpublish.spi.persistence.cache.userHandler"]
tags:
- {name: ezpublish.fieldType, alias: ezuser}

Expand Down
4 changes: 3 additions & 1 deletion eZ/Publish/SPI/Tests/FieldType/UserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
use eZ\Publish\SPI\Persistence\User;
use eZ\Publish\Core\Persistence\Cache\UserHandler;

/**
* Integration test for legacy storage field types.
Expand Down Expand Up @@ -54,7 +55,8 @@ public function getTypeName()
*/
public function getCustomHandler()
{
$fieldType = new FieldType\User\Type();
$userHandler = $this->createMock(UserHandler::class);
$fieldType = new FieldType\User\Type($userHandler);
$fieldType->setTransformationProcessor($this->getTransformationProcessor());

return $this->getHandler(
Expand Down

0 comments on commit d987eea

Please sign in to comment.