Skip to content

Commit

Permalink
Merge pull request #590 from systemli/fix/alias_repository
Browse files Browse the repository at this point in the history
fix: bring back old logic of `findOneBySource()` in AliasRepository
  • Loading branch information
doobry-systemli committed Apr 15, 2024
2 parents f8958bc + 8b0688a commit 846399e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
24 changes: 24 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ Feature: User
And I should see text matching "Your new alias address was created."
And the response status code should be 200

@fail-to-create-existing-custom-alias
Scenario: Fail to create existing custom alias
When I am authenticated as "user@example.org"
And I am on "/alias"
And I fill in the following:
| create_custom_alias_alias | alias1 |
And I press "Add"

Then I should be on "/alias"
And I should see "The e-mail address is already taken."
And the response status code should be 200

@fail-to-create-deleted-custom-alias
Scenario: Fail to create deleted custom alias
When I am authenticated as "user@example.org"
And I am on "/alias"
And I fill in the following:
| create_custom_alias_alias | alias2 |
And I press "Add"

Then I should be on "/alias"
And I should see "The e-mail address is already taken."
And the response status code should be 200

@delete-alias
Scenario: Delete custom alias
When I am authenticated as "user@example.org"
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/RandomAliasCreationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function onRandomAliasCreated(RandomAliasCreatedEvent $event): void
/** @var Alias $alias */
$alias = $event->getAlias();

while (null !== $this->manager->getRepository(Alias::class)->findOneBySource($alias->getSource())) {
while (null !== $this->manager->getRepository(Alias::class)->findOneBySource($alias->getSource(), true)) {
$localPart = RandomStringGenerator::generate(24, false);
/** @var Domain $domain */
$domain = $alias->getDomain();
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/AliasHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public function checkAliasLimit(array $aliases, bool $random = false): bool
{
$limit = ($random) ? self::ALIAS_LIMIT_RANDOM : self::ALIAS_LIMIT_CUSTOM;

return (count($aliases) < $limit) ? true : false;
return count($aliases) < $limit;
}

/**
* @throws ValidationException
*/
public function create(User $user, ?string $localPart = null): ?Alias
{
$random = (isset($localPart)) ? false : true;
$random = !isset($localPart);

$aliases = $this->repository->findByUser($user, $random);
if ($this->checkAliasLimit($aliases, $random)) {
Expand Down
18 changes: 11 additions & 7 deletions src/Repository/AliasRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
class AliasRepository extends EntityRepository
{
/**
* @param string $email
* @param string $email
* @param bool|null $includeDeleted
* @return Alias|null
*/
public function findOneBySource(string $email): ?Alias
public function findOneBySource(string $email, ?bool $includeDeleted = false): ?Alias
{
return $this->findOneBy(['source' => $email]);
if ($includeDeleted) {
return $this->findOneBy(['source' => $email]);
}

return $this->findOneBy(['source' => $email, 'deleted' => false]);
}

/**
* @param User $user
* @param bool|null $random
* @param bool $deleted
* @return array|Alias[]
*/
public function findByUser(User $user, ?bool $random = null, ?bool $deleted = false): array
public function findByUser(User $user, ?bool $random = null): array
{
if (isset($random)) {
return $this->findBy(['user' => $user, 'random' => $random, 'deleted' => $deleted]);
return $this->findBy(['user' => $user, 'random' => $random, 'deleted' => false]);
}

return $this->findBy(['user' => $user, 'deleted' => $deleted]);
return $this->findBy(['user' => $user, 'deleted' => false]);
}
}

0 comments on commit 846399e

Please sign in to comment.