Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[persistence] remove ResetPasswordRequest objects programmatically #284

Merged
merged 5 commits into from Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Persistence/Fake/FakeResetPasswordInternalRepository.php
Expand Up @@ -60,4 +60,9 @@ public function removeExpiredResetPasswordRequests(): int
{
throw new FakeRepositoryException();
}

public function removeRequests(?object $user = null): void
jrushlow marked this conversation as resolved.
Show resolved Hide resolved
{
throw new FakeRepositoryException();
}
}
28 changes: 28 additions & 0 deletions src/Persistence/Repository/ResetPasswordRequestRepositoryTrait.php
Expand Up @@ -82,4 +82,32 @@ public function removeExpiredResetPasswordRequests(): int

return $query->execute();
}

/**
* Remove ResetPasswordRequest objects from persistence.
*
* Warning - This is a destructive operation. Calling this method
* may have undesired consequences for users who have valid
* ResetPasswordRequests but have not "checked their email" yet.
*
* @TODO _ @legacy This method is useful for something.....
jrushlow marked this conversation as resolved.
Show resolved Hide resolved
*
* If a "user" object is passed, only the requests for that user
* will be removed.
*/
public function removeRequests(?object $user = null): void
{
$query = $this->createQueryBuilder('t')
->delete()
;

if (null !== $user) {
$query
->where('t.user = :user')
->setParameter('user', $user)
;
}

$query->getQuery()->execute();
}
}
2 changes: 2 additions & 0 deletions src/Persistence/ResetPasswordRequestRepositoryInterface.php
Expand Up @@ -14,6 +14,8 @@
/**
* @author Jesse Rushlow <jr@rushlow.dev>
* @author Ryan Weaver <ryan@symfonycasts.com>
*
* @method void removeRequests(?object $user = null) Remove ResetPasswordRequest objects from persistence.
*/
interface ResetPasswordRequestRepositoryInterface
{
Expand Down
Expand Up @@ -208,6 +208,45 @@ public function testRemovedExpiredResetPasswordRequestsOnlyRemovedExpiredRequest
self::assertSame($futureFixture, $result[0]);
}

public function testRemoveRequestsRemovesAllRequestsFromPersistence(): void
{
$this->manager->persist(new ResetPasswordTestFixtureRequest());
$this->manager->persist(new ResetPasswordTestFixtureRequest());

$this->manager->flush();

$this->repository->removeRequests();

self::assertCount(0, $this->repository->findAll());
}

public function testRemoveRequestsRemovesAllRequestsForASingleUser(): void
{
$this->manager->persist($userFixture = new ResetPasswordTestFixtureUser());
$requestFixtures = [new ResetPasswordTestFixtureRequest(), new ResetPasswordTestFixtureRequest()];

foreach ($requestFixtures as $fixture) {
$fixture->user = $userFixture;

$this->manager->persist($fixture);
}

$this->manager->persist($differentUserFixture = new ResetPasswordTestFixtureUser());

$existingRequestFixture = new ResetPasswordTestFixtureRequest();
$existingRequestFixture->user = $differentUserFixture;

$this->manager->persist($existingRequestFixture);
$this->manager->flush();

self::assertCount(3, $this->repository->findAll());

$this->repository->removeRequests($userFixture);

self::assertCount(1, $result = $this->repository->findAll());
self::assertSame($existingRequestFixture, $result[0]);
}

private function configureDatabase(): void
{
$metaData = $this->manager->getMetadataFactory();
Expand Down
Expand Up @@ -28,6 +28,7 @@ public function methodDataProvider(): \Generator
yield ['findResetPasswordRequest', ['']];
yield ['getMostRecentNonExpiredRequestDate', [new \stdClass()]];
yield ['removeResetPasswordRequest', [$this->createMock(ResetPasswordRequestInterface::class)]];
yield ['removeRequests', []];
}

/**
Expand Down