instances added god may help
This commit is contained in:
@@ -9,7 +9,7 @@ class ApiTokenRepository
|
||||
{
|
||||
private const UUID_REGEX = '/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
|
||||
|
||||
private static function unwrapList($rows): array
|
||||
private function unwrapList($rows): array
|
||||
{
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
@@ -24,12 +24,12 @@ class ApiTokenRepository
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function isUuid(string $value): bool
|
||||
public function isUuid(string $value): bool
|
||||
{
|
||||
return (bool) preg_match(self::UUID_REGEX, trim($value));
|
||||
}
|
||||
|
||||
public static function create(
|
||||
public function create(
|
||||
int $userId,
|
||||
string $name,
|
||||
string $selector,
|
||||
@@ -52,7 +52,7 @@ class ApiTokenRepository
|
||||
return $id ? (int) $id : null;
|
||||
}
|
||||
|
||||
public static function findBySelector(string $selector): ?array
|
||||
public function findBySelector(string $selector): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, user_id, name, selector, token_hash, tenant_id, last_used_at, last_ip, expires_at, revoked_at, created_by, created from user_api_tokens where selector = ? limit 1',
|
||||
@@ -64,7 +64,7 @@ class ApiTokenRepository
|
||||
return $row['user_api_tokens'];
|
||||
}
|
||||
|
||||
public static function find(int $id): ?array
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, user_id, name, selector, tenant_id, last_used_at, last_ip, expires_at, revoked_at, created_by, created from user_api_tokens where id = ? limit 1',
|
||||
@@ -76,10 +76,10 @@ class ApiTokenRepository
|
||||
return $row['user_api_tokens'];
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if (!self::isUuid($uuid)) {
|
||||
if (!$this->isUuid($uuid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -93,10 +93,10 @@ class ApiTokenRepository
|
||||
return $row['user_api_tokens'];
|
||||
}
|
||||
|
||||
public static function findByUuidForUser(string $uuid, int $userId): ?array
|
||||
public function findByUuidForUser(string $uuid, int $userId): ?array
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if ($userId <= 0 || !self::isUuid($uuid)) {
|
||||
if ($userId <= 0 || !$this->isUuid($uuid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ class ApiTokenRepository
|
||||
return $row['user_api_tokens'];
|
||||
}
|
||||
|
||||
public static function updateLastUsed(int $id, string $ip): bool
|
||||
public function updateLastUsed(int $id, string $ip): bool
|
||||
{
|
||||
$result = DB::update(
|
||||
'update user_api_tokens set last_used_at = NOW(), last_ip = ? where id = ?',
|
||||
@@ -121,7 +121,7 @@ class ApiTokenRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function revoke(int $id): bool
|
||||
public function revoke(int $id): bool
|
||||
{
|
||||
$result = DB::update(
|
||||
'update user_api_tokens set revoked_at = NOW() where id = ? and revoked_at is null',
|
||||
@@ -130,10 +130,10 @@ class ApiTokenRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function revokeByUuidForUser(string $uuid, int $userId): bool
|
||||
public function revokeByUuidForUser(string $uuid, int $userId): bool
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if ($userId <= 0 || !self::isUuid($uuid)) {
|
||||
if ($userId <= 0 || !$this->isUuid($uuid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ class ApiTokenRepository
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function revokeAllForUser(int $userId, ?int $tenantId = null): int
|
||||
public function revokeAllForUser(int $userId, ?int $tenantId = null): int
|
||||
{
|
||||
if ($tenantId !== null && $tenantId > 0) {
|
||||
$result = DB::update(
|
||||
@@ -162,7 +162,7 @@ class ApiTokenRepository
|
||||
return $result !== false ? (int) $result : 0;
|
||||
}
|
||||
|
||||
public static function revokeAllActiveByAdmin(): int
|
||||
public function revokeAllActiveByAdmin(): int
|
||||
{
|
||||
$result = DB::update(
|
||||
'update user_api_tokens set revoked_at = NOW() where revoked_at is null and (expires_at is null or expires_at > NOW())'
|
||||
@@ -170,7 +170,7 @@ class ApiTokenRepository
|
||||
return $result !== false ? (int) $result : 0;
|
||||
}
|
||||
|
||||
public static function listByUserId(int $userId, int $limit = 25): array
|
||||
public function listByUserId(int $userId, int $limit = 25): array
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return [];
|
||||
@@ -185,10 +185,10 @@ class ApiTokenRepository
|
||||
(string) $userId,
|
||||
(string) $limit
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
return $this->unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function countActiveForUser(int $userId): int
|
||||
public function countActiveForUser(int $userId): int
|
||||
{
|
||||
$count = DB::selectValue(
|
||||
'select count(*) from user_api_tokens where user_id = ? and revoked_at is null and (expires_at is null or expires_at > NOW())',
|
||||
@@ -197,7 +197,7 @@ class ApiTokenRepository
|
||||
return $count ? (int) $count : 0;
|
||||
}
|
||||
|
||||
public static function countActiveForUserExcludingId(int $userId, int $excludeId): int
|
||||
public function countActiveForUserExcludingId(int $userId, int $excludeId): int
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return 0;
|
||||
@@ -211,7 +211,7 @@ class ApiTokenRepository
|
||||
return $count ? (int) $count : 0;
|
||||
}
|
||||
|
||||
public static function countActive(): int
|
||||
public function countActive(): int
|
||||
{
|
||||
$count = DB::selectValue(
|
||||
'select count(*) from user_api_tokens where revoked_at is null and (expires_at is null or expires_at > NOW())'
|
||||
|
||||
Reference in New Issue
Block a user