Fixed error caused by immutable datetime passed to mutable datetime doctrine type

This commit is contained in:
Jan Böhmer 2024-06-10 20:47:06 +02:00
parent 4d927c5870
commit 777f6ba738
4 changed files with 11 additions and 11 deletions

View file

@ -75,7 +75,7 @@ class APITokenFixtures extends Fixture implements DependentFixtureInterface
$expired_token->setUser($admin_user);
$expired_token->setLevel(ApiTokenLevel::FULL);
$expired_token->setName('expired');
$expired_token->setValidUntil(new \DateTimeImmutable('-1 day'));
$expired_token->setValidUntil(new \DateTime('-1 day'));
$this->setTokenSecret($expired_token, self::TOKEN_EXPIRED);
$manager->persist($expired_token);

View file

@ -41,9 +41,9 @@ class OAuthToken extends AbstractNamedDBElement implements AccessTokenInterface
#[ORM\Column(type: 'text', nullable: true)]
private ?string $token = null;
/** @var \DateTimeInterface The date when the token expires */
/** @var \DateTimeImmutable|null The date when the token expires */
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeInterface $expires_at = null;
private ?\DateTimeImmutable $expires_at = null;
/** @var string|null The refresh token for the OAuth2 auth */
#[ORM\Column(type: 'text', nullable: true)]
@ -54,7 +54,7 @@ class OAuthToken extends AbstractNamedDBElement implements AccessTokenInterface
*/
private const DEFAULT_EXPIRATION_TIME = 3600;
public function __construct(string $name, ?string $refresh_token, ?string $token = null, \DateTimeInterface $expires_at = null)
public function __construct(string $name, ?string $refresh_token, ?string $token = null, \DateTimeImmutable $expires_at = null)
{
//If token is given, you also have to give the expires_at date
if ($token !== null && $expires_at === null) {
@ -82,7 +82,7 @@ class OAuthToken extends AbstractNamedDBElement implements AccessTokenInterface
);
}
private static function unixTimestampToDatetime(int $timestamp): \DateTimeInterface
private static function unixTimestampToDatetime(int $timestamp): \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat('U', (string)$timestamp);
}

View file

@ -78,7 +78,7 @@ class ApiToken implements TimeStampableInterface
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[Groups('token:read')]
#[Year2038BugWorkaround]
private ?\DateTimeInterface $valid_until;
private ?\DateTime $valid_until;
#[ORM\Column(length: 68, unique: true)]
private string $token;
@ -89,7 +89,7 @@ class ApiToken implements TimeStampableInterface
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[Groups('token:read')]
private ?\DateTimeInterface $last_time_used = null;
private ?\DateTime $last_time_used = null;
public function __construct(ApiTokenType $tokenType = ApiTokenType::PERSONAL_ACCESS_TOKEN)
{
@ -130,7 +130,7 @@ class ApiToken implements TimeStampableInterface
return $this->valid_until === null || $this->valid_until > new \DateTime();
}
public function setValidUntil(?\DateTimeInterface $valid_until): ApiToken
public function setValidUntil(?\DateTime $valid_until): ApiToken
{
$this->valid_until = $valid_until;
return $this;
@ -168,10 +168,10 @@ class ApiToken implements TimeStampableInterface
/**
* Sets the last time the token was used to authenticate.
* @param \DateTimeInterface|null $last_time_used
* @param \DateTime|null $last_time_used
* @return ApiToken
*/
public function setLastTimeUsed(?\DateTimeInterface $last_time_used): ApiToken
public function setLastTimeUsed(?\DateTime $last_time_used): ApiToken
{
$this->last_time_used = $last_time_used;
return $this;

View file

@ -75,7 +75,7 @@ class ApiTokenAuthenticator implements AuthenticatorInterface
$old_time = $token->getLastTimeUsed();
//Set the last used date of the token
$token->setLastTimeUsed(new \DateTimeImmutable());
$token->setLastTimeUsed(new \DateTime());
//Only flush the token if the last used date change is more than 10 minutes
//For performance reasons we don't want to flush the token every time it is used, but only if it is used more than 10 minutes after the last time it was used
//If a flush is later in the code we don't want to flush the token again