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

UUID and ULID support #198

Open
misaert opened this issue Dec 13, 2021 · 2 comments · May be fixed by #199
Open

UUID and ULID support #198

misaert opened this issue Dec 13, 2021 · 2 comments · May be fixed by #199

Comments

@misaert
Copy link

misaert commented Dec 13, 2021

Hi,

It seems the trait ResetPasswordRequestRepositoryTrait does not support UUID and ULID because with Doctrine, it must add 'ulid' as the third argument to tell Doctrine that this is a ULID or alternatively, you can convert it to a value compatible with the type inferred by Doctrine (see https://symfony.com/doc/current/components/uid.html#storing-ulids-in-databases).

I have to override 2 trait methods to do them work:

class ResetPasswordRequestRepository extends ServiceEntityRepository implements ResetPasswordRequestRepositoryInterface
{
    use ResetPasswordRequestRepositoryTrait;

    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ResetPasswordRequest::class);
    }

    /**
     * @param User $user
     */
    public function createResetPasswordRequest(
        object $user,
        DateTimeInterface $expiresAt,
        string $selector,
        string $hashedToken
    ): ResetPasswordRequestInterface {
        return new ResetPasswordRequest($user, $expiresAt, $selector, $hashedToken);
    }

    /**
     * Override trait method because of using ulid for user ID.
     *
     * @param User $user
     */
    public function getMostRecentNonExpiredRequestDate(object $user): ?DateTimeInterface
    {
        // Normally there is only 1 max request per use, but written to be flexible
        /** @var ResetPasswordRequestInterface $resetPasswordRequest */
        $resetPasswordRequest = $this->createQueryBuilder('t')
            ->where('t.user = :user')
            ->setParameter('user', $user->getId(), 'ulid')
            ->orderBy('t.requestedAt', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneorNullResult()
        ;
        if (null !== $resetPasswordRequest && !$resetPasswordRequest->isExpired()) {
            return $resetPasswordRequest->getRequestedAt();
        }

        return null;
    }

    /**
     * Override trait method because of using ulid for user ID.
     */
    public function removeResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
    {
        /** @var User $user */
        $user = $resetPasswordRequest->getUser();

        $this->createQueryBuilder('t')
            ->delete()
            ->where('t.user = :user')
            ->setParameter('user', $user->getId(), 'ulid')
            ->getQuery()
            ->execute()
        ;
    }
}

Did I have another solution?

Thanks a lot,

Mickaël

@jrushlow jrushlow linked a pull request Dec 13, 2021 that will close this issue
@pacproduct
Copy link

I'm facing this issue too. Adding the error I'm facing here, it might help others finding this thread:

An exception occurred while executing a query: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: "01G56C##############"

In the meantime, thanks @misaert for your workaround 👍

@DrLuke
Copy link

DrLuke commented Apr 1, 2023

Tangentially related: The trait needs to check how many rows were affected, otherwise this just fails silently and the link is reusable until it's cleaned up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants