Permalink
Browse files

Removed the old unique validator

  • Loading branch information...
1 parent 4878c6e commit 7dffdd8f095b9ee4a9f8fdd3a8e1e3ee69450cf1 @stof stof committed Jul 8, 2012
View
9 CouchDocument/UserManager.php
@@ -103,15 +103,6 @@ public function updateUser(UserInterface $user, $andFlush = true)
}
}
- /**
- * {@inheritDoc}
- */
- public function validateUnique(UserInterface $value, Constraint $constraint)
- {
- // for now unique checks are not implemented in Doctrine CouchDB yet
- return true;
- }
-
public function reloadUser(UserInterface $user)
{
$this->dm->refresh($user);
View
69 Document/UserManager.php
@@ -102,73 +102,4 @@ public function updateUser(UserInterface $user, $andFlush = true)
$this->dm->flush();
}
}
-
- /**
- * {@inheritDoc}
- */
- public function validateUnique(UserInterface $value, Constraint $constraint)
- {
- // Since we probably want to validate the canonical fields,
- // we'd better make sure we have them.
- $this->updateCanonicalFields($value);
-
- $classMetadata = $this->dm->getClassMetadata($this->class);
- // TODO: ODM seems to be missing handling for multiple properties
- // $fields = array_map('trim', explode(',', $constraint->property));
- $query = $this->getQueryArray($classMetadata, $value, $constraint->property);
-
- $document = $this->findUserBy($query);
- if (null === $document) {
- return true;
- }
-
- // check if document in mongodb is the same document as the checked one
- if ($document->isUser($value)) {
- return true;
- }
- // check if returned document is proxy and initialize the minimum identifier if needed
- if ($document instanceof Proxy) {
- $classMetadata->setIdentifierValue($document, $document->__identifier);
- }
- // check if document has the same identifier as the current one
- if ($classMetadata->getIdentifierValue($document) === $classMetadata->getIdentifierValue($value)) {
- return true;
- }
-
- return false;
- }
-
- protected function getQueryArray($classMetadata, $value, $fieldName)
- {
- $field = $this->getFieldNameFromPropertyPath($fieldName);
- if (!isset($classMetadata->fieldMappings[$field])) {
- throw new \LogicException("Mapping for '$fieldName' doesn't exist for " . $this->class);
- }
-
- $mapping = $classMetadata->fieldMappings[$field];
- if (isset($mapping['reference']) && $mapping['reference']) {
- throw new \LogicException('Cannot determine uniqueness of referenced document values');
- }
-
- $criteria[$field] = $value instanceOf UserInterface ? $classMetadata->getFieldValue($value, $field) : $value;
-
- return $criteria;
- }
-
- /**
- * Returns the actual document field value.
- *
- * E.g. document.someVal -> document
- * user.emails -> user
- * username -> username
- *
- * @param string $field
- * @return string
- */
- protected function getFieldNameFromPropertyPath($field)
- {
- $pieces = explode('.', $field);
-
- return $pieces[0];
- }
}
View
78 Entity/UserManager.php
@@ -101,82 +101,4 @@ public function updateUser(UserInterface $user, $andFlush = true)
$this->em->flush();
}
}
-
- /**
- * {@inheritDoc}
- */
- public function validateUnique(UserInterface $value, Constraint $constraint)
- {
- // Since we probably want to validate the canonical fields,
- // we'd better make sure we have them.
- $this->updateCanonicalFields($value);
-
- $fields = array_map('trim', explode(',', $constraint->property));
- $users = $this->findConflictualUsers($value, $fields);
-
- // there is no conflictual user
- if (empty($users)) {
- return true;
- }
-
- // there is no conflictual user which is not the same as the value
- if ($this->anyIsUser($value, $users)) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Indicates whether the given user and all compared objects correspond to the same record.
- *
- * @param UserInterface $user
- * @param array $comparisons
- * @return Boolean
- */
- protected function anyIsUser($user, array $comparisons)
- {
- foreach ($comparisons as $comparison) {
- if (!$user->isUser($comparison)) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Gets conflictual users for the given user and constraint.
- *
- * @param UserInterface $value
- * @param array $fields
- * @return array
- */
- protected function findConflictualUsers($value, array $fields)
- {
- return $this->repository->findBy($this->getCriteria($value, $fields));
- }
-
- /**
- * Gets the criteria used to find conflictual entities.
- *
- * @param UserInterface $value
- * @param array $fields
- * @return array
- */
- protected function getCriteria($value, array $fields)
- {
- $classMetadata = $this->em->getClassMetadata($this->class);
-
- $criteria = array();
- foreach ($fields as $field) {
- if (!$classMetadata->hasField($field)) {
- throw new \InvalidArgumentException(sprintf('The "%s" class metadata does not have any "%s" field or association mapping.', $this->class, $field));
- }
-
- $criteria[$field] = $classMetadata->getFieldValue($value, $field);
- }
-
- return $criteria;
- }
}
View
10 Model/UserManagerInterface.php
@@ -128,14 +128,4 @@ public function updateCanonicalFields(UserInterface $user);
* @param UserInterface $user
*/
public function updatePassword(UserInterface $user);
-
- /**
- * Checks the uniqueness of the given fields, returns true if its unique.
- *
- * @param UserInterface $value
- * @param Constraint $constraint
- *
- * @return Boolean
- */
- public function validateUnique(UserInterface $value, Constraint $constraint);
}
View
64 Propel/UserManager.php
@@ -137,70 +137,6 @@ public function updateUser(UserInterface $user)
}
/**
- * {@inheritDoc}
- */
- public function validateUnique(UserInterface $value, Constraint $constraint)
- {
- // Since we probably want to validate the canonical fields,
- // we'd better make sure we have them.
- $this->updateCanonicalFields($value);
-
- $fields = array_map('trim', explode(',', $constraint->property));
- $users = $this->findConflictualUsers($value, $fields);
-
- // there is no conflictual user
- if (empty($users)) {
- return true;
- }
-
- // there is no conflictual user which is not the same as the value
- if ($this->anyIsUser($value, $users)) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Indicates whether the given user and all compared objects correspond to the same record.
- *
- * @param UserInterface $user
- * @param array $comparisons
- * @return Boolean
- */
- protected function anyIsUser($user, array $comparisons)
- {
- foreach ($comparisons as $comparison) {
- foreach ($comparison as $field => $value) {
- if ($user->{'get'.$field}() !== $value) {
- return false;
- }
- }
- }
-
- return true;
- }
-
- /**
- * Gets conflictual users for the given user and constraint.
- *
- * @param UserInterface $value
- * @param array $fields
- * @return array
- */
- protected function findConflictualUsers($value, array $fields)
- {
- $query = $this->createQuery();
-
- foreach ($fields as $field) {
- $method = 'get'.ucfirst($field);
- $query->filterBy(ucfirst($field), $value->$method());
- }
-
- return $query->find()->toArray();
- }
-
- /**
* Create the propel query class corresponding to your queryclass
*
* @return \ModelCriteria the queryClass
View
14 Resources/config/validator.xml
@@ -14,20 +14,6 @@
<tag name="validator.initializer" />
<argument type="service" id="fos_user.user_manager" />
</service>
-
- <!-- Unique Validator Service -->
- <service id="fos_user.validator.unique" class="%fos_user.validator.unique.class%">
- <argument type="service" id="fos_user.user_manager" />
- <tag name="validator.constraint_validator" alias="fos_user.validator.unique" />
- </service>
-
- <!-- Password Validator Service -->
- <service id="fos_user.validator.password" class="%fos_user.validator.password.class%">
- <call method="setEncoderFactory">
- <argument type="service" id="security.encoder_factory" />
- </call>
- <tag name="validator.constraint_validator" alias="fos_user.validator.password" />
- </service>
</services>
</container>
View
66 Tests/Validation/UniqueValidatorTest.php
@@ -1,66 +0,0 @@
-<?php
-
-/*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace FOS\UserBundle\Tests\Validation;
-
-use FOS\UserBundle\Validator\UniqueValidator;
-use FOS\UserBundle\Validator\Unique;
-
-class UniqueValidatorTest extends \PHPUnit_Framework_TestCase
-{
- private $validator;
- private $userManagerMock;
- private $constraint;
- private $user;
-
- public function setUp()
- {
- $options = array(
- 'property' => 'username',
- );
- $context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
- ->disableOriginalConstructor()
- ->getMock();
- $this->constraint = new Unique($options);
- $this->userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $this->validator = new UniqueValidator($this->userManagerMock);
- $this->validator->initialize($context);
- $this->user = $this->getMock('FOS\UserBundle\Model\UserInterface');
- }
-
- public function testFalseOnDuplicateUserProperty()
- {
- $this->userManagerMock->expects($this->once())
- ->method('validateUnique')
- ->will($this->returnValue(false))
- ->with($this->equalTo($this->user), $this->equalTo($this->constraint));
-
- $this->assertFalse($this->validator->isValid($this->user, $this->constraint));
- }
-
- public function testTrueOnUniqueUserProperty()
- {
- $this->userManagerMock->expects($this->once())
- ->method('validateUnique')
- ->will($this->returnValue(true))
- ->with($this->equalTo($this->user), $this->equalTo($this->constraint));
-
- $this->assertTrue($this->validator->isValid($this->user, $this->constraint));
- }
-
- /**
- * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
- */
- public function testBadType()
- {
- $this->validator->isValid('bad_type', $this->constraint);
- }
-}
View
46 Validator/Unique.php
@@ -1,46 +0,0 @@
-<?php
-
-/*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace FOS\UserBundle\Validator;
-
-use Symfony\Component\Validator\Constraint;
-
-/**
- * @Annotation
- */
-class Unique extends Constraint
-{
- public $message = 'The value for "%property%" already exists.';
- public $property;
-
- public function getDefaultOption()
- {
- return 'property';
- }
-
- public function getRequiredOptions()
- {
- return array('property');
- }
-
- public function validatedBy()
- {
- return 'fos_user.validator.unique';
- }
-
- /**
- * {@inheritDoc}
- */
- public function getTargets()
- {
- return self::CLASS_CONSTRAINT;
- }
-}
View
66 Validator/UniqueValidator.php
@@ -1,66 +0,0 @@
-<?php
-
-/*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace FOS\UserBundle\Validator;
-
-use Symfony\Component\Validator\Constraint;
-use Symfony\Component\Validator\ConstraintValidator;
-use FOS\UserBundle\Model\UserManagerInterface;
-use Symfony\Component\Validator\Exception\UnexpectedTypeException;
-use FOS\UserBundle\Model\UserInterface;
-
-/**
- * UniqueValidator
- */
-class UniqueValidator extends ConstraintValidator
-{
- /**
- * @var UserManagerInterface
- */
- protected $userManager;
-
- /**
- * Constructor
- *
- * @param UserManagerInterface $userManager
- */
- public function __construct(UserManagerInterface $userManager)
- {
- $this->userManager = $userManager;
- }
-
- /**
- * Checks if the passed value is valid.
- *
- * @param mixed $value The value that should be validated
- * @param Constraint $constraint The constrain for the validation
- *
- * @return Boolean Whether or not the value is valid
- *
- * @throws UnexpectedTypeException if $value is not instance of \FOS\UserBundle\Model\UserInterface
- */
- public function isValid($value, Constraint $constraint)
- {
- if (!$value instanceof UserInterface) {
- throw new UnexpectedTypeException($value, 'FOS\UserBundle\Model\UserInterface');
- }
-
- if (!$this->userManager->validateUnique($value, $constraint)) {
- $this->setMessage($constraint->message, array(
- '%property%' => $constraint->property
- ));
-
- return false;
- }
-
- return true;
- }
-}

0 comments on commit 7dffdd8

Please sign in to comment.