4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-17 22:47:04 +00:00
AzuraCast/src/Entity/Repository/UserLoginTokenRepository.php

50 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
namespace App\Entity\Repository;
use App\Entity;
use App\Security\SplitToken;
/**
* @extends AbstractSplitTokenRepository<Entity\UserLoginToken>
*/
final class UserLoginTokenRepository extends AbstractSplitTokenRepository
{
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 */
$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();
}
}