2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
use openvk\Web\Models\Entities\APIToken;
|
|
|
|
|
|
|
|
class APITokens extends Repository
|
|
|
|
{
|
|
|
|
protected $tableName = "api_tokens";
|
|
|
|
protected $modelName = "APIToken";
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getByCode(string $code, bool $withRevoked = false): ?APIToken
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
$parts = explode("-", $code);
|
|
|
|
$id = $parts[0];
|
|
|
|
$secret = implode("", array_slice($parts, 1, 9));
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
$token = $this->get((int) $id);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
} elseif ($token->getSecret() !== $secret) {
|
|
|
|
return null;
|
|
|
|
} elseif ($token->isRevoked() && !$withRevoked) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
return $token;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getStaleByUser(int $userId, string $platform, bool $withRevoked = false): ?APIToken
|
2024-03-30 13:59:10 +03:00
|
|
|
{
|
|
|
|
return $this->toEntity($this->table->where([
|
|
|
|
'user' => $userId,
|
|
|
|
'platform' => $platform,
|
|
|
|
'deleted' => $withRevoked,
|
|
|
|
])->fetch());
|
|
|
|
}
|
2020-08-12 14:36:18 +03:00
|
|
|
}
|