2021-02-28 02:50:45 +00:00
|
|
|
<?php
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-02-28 02:50:45 +00:00
|
|
|
namespace App\Entity\Repository;
|
|
|
|
|
|
|
|
use App\Entity;
|
|
|
|
use App\Security\SplitToken;
|
|
|
|
|
2021-09-06 09:06:31 +00:00
|
|
|
/**
|
|
|
|
* @extends AbstractSplitTokenRepository<Entity\UserLoginToken>
|
|
|
|
*/
|
2022-05-31 07:50:49 +00:00
|
|
|
final class UserLoginTokenRepository extends AbstractSplitTokenRepository
|
2021-02-28 02:50:45 +00:00
|
|
|
{
|
|
|
|
public function createToken(Entity\User $user): SplitToken
|
|
|
|
{
|
|
|
|
$token = SplitToken::generate();
|
|
|
|
|
|
|
|
$loginToken = new Entity\UserLoginToken($user, $token);
|
|
|
|
$this->em->persist($loginToken);
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function revokeForUser(Entity\User $user): void
|
|
|
|
{
|
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
DELETE FROM App\Entity\UserLoginToken ult
|
|
|
|
WHERE ult.user = :user
|
|
|
|
DQL
|
|
|
|
)->setParameter('user', $user)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanup(): void
|
|
|
|
{
|
2021-06-08 06:40:49 +00:00
|
|
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
2021-02-28 02:50:45 +00:00
|
|
|
$threshold = time() - 86400; // One day
|
|
|
|
|
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
DELETE FROM App\Entity\UserLoginToken ut WHERE ut.created_at <= :threshold
|
|
|
|
DQL
|
|
|
|
)->setParameter('threshold', $threshold)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|