Skip to content

Commit

Permalink
Added the current email to exclude in the validation of the use profi…
Browse files Browse the repository at this point in the history
…le patch.
  • Loading branch information
ruslanbaidan committed Aug 30, 2024
1 parent 297114f commit 18289a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/Table/UserTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ public function __construct(EntityManager $entityManager, string $entityName = U
parent::__construct($entityManager, $entityName);
}

public function findByEmail(string $email): UserSuperClass
public function findByEmail(string $email, ?string $excludeEmail = null): UserSuperClass
{
$user = $this->getRepository()->createQueryBuilder('u')
$queryBuilder = $this->getRepository()->createQueryBuilder('u')
->where('u.email = :email')
->setParameter(':email', $email)
->getQuery()
->setParameter(':email', $email);
if ($excludeEmail !== null) {
$queryBuilder->andWhere('u.email <> :excludeEmail')
->setParameter('excludeEmail', $excludeEmail);
}

$user = $queryBuilder->getQuery()
->setMaxResults(1)
->getOneOrNullResult();

Expand Down
3 changes: 2 additions & 1 deletion src/Validator/FieldValidator/UniqueEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public function isValid($value)
{
/** @var UserTable $userTable */
$userTable = $this->getOptions()['userTable'];
$excludeEmail = !empty($this->getOptions()['excludeEmail']) ? $this->getOptions()['excludeEmail'] : null;

try {
$userTable->findByEmail($value);
$userTable->findByEmail($value, $excludeEmail);
} catch (EntityNotFoundException $e) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Monarc\Core\Validator\FieldValidator\UniqueEmail;
use Monarc\Core\Validator\InputValidator\AbstractInputValidator;
use Monarc\Core\Table\UserTable;
use Monarc\Core\Validator\InputValidator\FilterFieldsValidationTrait;
use Monarc\Core\Validator\InputValidator\InputValidationTranslator;

class PatchProfileDataInputValidator extends AbstractInputValidator
{
use FilterFieldsValidationTrait;

protected UserTable $userTable;

protected ?UserSuperClass $connectedUser;
Expand Down Expand Up @@ -92,6 +95,7 @@ protected function getRules(): array
'options' => [
'userTable' => $this->userTable,
'currentUserId' => $this->connectedUser !== null ? $this->connectedUser->getId() : 0,
'excludeEmail' => $this->excludeFilter['email'] ?? null,
],
],
],
Expand Down

0 comments on commit 18289a6

Please sign in to comment.