instances added god may help

This commit is contained in:
2026-02-23 12:58:19 +01:00
parent 25370a1a55
commit 99db252f60
290 changed files with 9858 additions and 4914 deletions

View File

@@ -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())'

View File

@@ -6,7 +6,7 @@ use MintyPHP\DB;
class EmailVerificationRepository
{
public static function create(int $userId, string $codeHash, string $expiresAt): ?int
public function create(int $userId, string $codeHash, string $expiresAt): ?int
{
$id = DB::insert(
'insert into email_verifications (user_id, code_hash, expires_at, attempts, used_at, created) values (?,?,?,?,NULL,NOW())',
@@ -18,7 +18,7 @@ class EmailVerificationRepository
return $id ? (int) $id : null;
}
public static function invalidateForUser(int $userId): bool
public function invalidateForUser(int $userId): bool
{
$result = DB::update(
'update email_verifications set used_at = NOW() where user_id = ? and used_at is null',
@@ -27,7 +27,7 @@ class EmailVerificationRepository
return $result !== false;
}
public static function findActiveByUserId(int $userId): ?array
public function findActiveByUserId(int $userId): ?array
{
$row = DB::selectOne(
'select id, user_id, code_hash, expires_at, attempts, used_at from email_verifications where user_id = ? and used_at is null and expires_at > UTC_TIMESTAMP() order by id desc limit 1',
@@ -39,7 +39,7 @@ class EmailVerificationRepository
return $row['email_verifications'];
}
public static function findById(int $id): ?array
public function findById(int $id): ?array
{
$row = DB::selectOne(
'select id, user_id, code_hash, expires_at, attempts, used_at from email_verifications where id = ? limit 1',
@@ -51,7 +51,7 @@ class EmailVerificationRepository
return $row['email_verifications'];
}
public static function incrementAttempts(int $id): bool
public function incrementAttempts(int $id): bool
{
$result = DB::update(
'update email_verifications set attempts = attempts + 1 where id = ?',
@@ -60,7 +60,7 @@ class EmailVerificationRepository
return $result !== false;
}
public static function markUsed(int $id): bool
public function markUsed(int $id): bool
{
$result = DB::update(
'update email_verifications set used_at = NOW() where id = ?',

View File

@@ -6,7 +6,7 @@ use MintyPHP\DB;
class PasswordResetRepository
{
private static function unwrapList($rows): array
private function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
@@ -21,7 +21,7 @@ class PasswordResetRepository
return $list;
}
public static function create(int $userId, string $codeHash, string $expiresAt): ?int
public function create(int $userId, string $codeHash, string $expiresAt): ?int
{
$id = DB::insert(
'insert into password_resets (user_id, code_hash, expires_at, attempts, used_at, created) values (?,?,?,?,NULL,NOW())',
@@ -33,7 +33,7 @@ class PasswordResetRepository
return $id ? (int) $id : null;
}
public static function invalidateForUser(int $userId): bool
public function invalidateForUser(int $userId): bool
{
$result = DB::update(
'update password_resets set used_at = NOW() where user_id = ? and used_at is null',
@@ -42,7 +42,7 @@ class PasswordResetRepository
return $result !== false;
}
public static function findActiveByUserId(int $userId): ?array
public function findActiveByUserId(int $userId): ?array
{
$row = DB::selectOne(
'select id, user_id, code_hash, expires_at, attempts, used_at from password_resets where user_id = ? and used_at is null and expires_at > UTC_TIMESTAMP() order by id desc limit 1',
@@ -54,7 +54,7 @@ class PasswordResetRepository
return $row['password_resets'];
}
public static function findById(int $id): ?array
public function findById(int $id): ?array
{
$row = DB::selectOne(
'select id, user_id, code_hash, expires_at, attempts, used_at from password_resets where id = ? limit 1',
@@ -66,7 +66,7 @@ class PasswordResetRepository
return $row['password_resets'];
}
public static function incrementAttempts(int $id): bool
public function incrementAttempts(int $id): bool
{
$result = DB::update(
'update password_resets set attempts = attempts + 1 where id = ?',
@@ -75,7 +75,7 @@ class PasswordResetRepository
return $result !== false;
}
public static function markUsed(int $id): bool
public function markUsed(int $id): bool
{
$result = DB::update(
'update password_resets set used_at = NOW() where id = ?',
@@ -84,7 +84,7 @@ class PasswordResetRepository
return $result !== false;
}
public static function listByUserId(int $userId, int $limit = 20): array
public function listByUserId(int $userId, int $limit = 20): array
{
if ($userId <= 0) {
return [];
@@ -99,6 +99,6 @@ class PasswordResetRepository
(string) $userId,
(string) $limit
);
return self::unwrapList($rows);
return $this->unwrapList($rows);
}
}

View File

@@ -6,7 +6,7 @@ use MintyPHP\DB;
class RememberTokenRepository
{
private static function unwrapList($rows): array
private function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
@@ -21,7 +21,7 @@ class RememberTokenRepository
return $list;
}
public static function create(int $userId, string $selector, string $tokenHash, string $expiresAt): ?int
public function create(int $userId, string $selector, string $tokenHash, string $expiresAt): ?int
{
$id = DB::insert(
'insert into user_remember_tokens (user_id, selector, token_hash, expires_at, created) values (?,?,?,?,NOW())',
@@ -33,7 +33,7 @@ class RememberTokenRepository
return $id ? (int) $id : null;
}
public static function findBySelector(string $selector): ?array
public function findBySelector(string $selector): ?array
{
$row = DB::selectOne(
'select id, user_id, selector, token_hash, expires_at, expired_by_admin_at, last_used from user_remember_tokens where selector = ? limit 1',
@@ -45,7 +45,7 @@ class RememberTokenRepository
return $row['user_remember_tokens'];
}
public static function updateToken(int $id, string $tokenHash, string $expiresAt): bool
public function updateToken(int $id, string $tokenHash, string $expiresAt): bool
{
$result = DB::update(
'update user_remember_tokens set token_hash = ?, expires_at = ?, expired_by_admin_at = NULL, last_used = NOW() where id = ?',
@@ -56,7 +56,7 @@ class RememberTokenRepository
return $result !== false;
}
public static function expireAllByAdmin(): int
public function expireAllByAdmin(): int
{
$result = DB::update(
'update user_remember_tokens set expires_at = NOW(), expired_by_admin_at = NOW() where expires_at > NOW()'
@@ -64,19 +64,19 @@ class RememberTokenRepository
return $result !== false ? (int) $result : 0;
}
public static function deleteById(int $id): bool
public function deleteById(int $id): bool
{
$result = DB::delete('delete from user_remember_tokens where id = ?', (string) $id);
return $result !== false;
}
public static function deleteByUserId(int $userId): bool
public function deleteByUserId(int $userId): bool
{
$result = DB::delete('delete from user_remember_tokens where user_id = ?', (string) $userId);
return $result !== false;
}
public static function listByUserId(int $userId, int $limit = 20): array
public function listByUserId(int $userId, int $limit = 20): array
{
if ($userId <= 0) {
return [];
@@ -91,10 +91,10 @@ class RememberTokenRepository
(string) $userId,
(string) $limit
);
return self::unwrapList($rows);
return $this->unwrapList($rows);
}
public static function countActive(): int
public function countActive(): int
{
$count = DB::selectValue('select count(*) from user_remember_tokens where expires_at > NOW()');
return $count ? (int) $count : 0;