Skip to content

UUID and ULID support #198

Open
Open
@misaert

Description

@misaert

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions