Files
cmspro.it/app/models/User.php
2026-01-29 21:07:02 +01:00

62 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/Database.php';
class User
{
public static function findByLogin(string $login): ?array
{
$db = Database::getInstance();
$stmt = $db->prepare('SELECT * FROM users WHERE login = :login');
$stmt->execute(['login' => $login]);
$user = $stmt->fetch();
return $user ?: null;
}
public static function verifyPassword(string $login, string $password): ?array
{
$user = self::findByLogin($login);
if ($user && password_verify($password, $user['password'])) {
return $user;
}
return null;
}
public static function saveVerificationCode(int $userId, string $code): bool
{
$db = Database::getInstance();
$expiresAt = date('Y-m-d H:i:s', strtotime('+10 minutes'));
$stmt = $db->prepare('
INSERT OR REPLACE INTO verification_codes (user_id, code, expires_at, created_at)
VALUES (:user_id, :code, :expires_at, datetime("now"))
');
return $stmt->execute([
'user_id' => $userId,
'code' => $code,
'expires_at' => $expiresAt
]);
}
public static function verifyCode(int $userId, string $code): bool
{
$db = Database::getInstance();
$stmt = $db->prepare('
SELECT * FROM verification_codes
WHERE user_id = :user_id
AND code = :code
AND expires_at > datetime("now")
');
$stmt->execute(['user_id' => $userId, 'code' => $code]);
return (bool) $stmt->fetch();
}
public static function deleteVerificationCode(int $userId): bool
{
$db = Database::getInstance();
$stmt = $db->prepare('DELETE FROM verification_codes WHERE user_id = :user_id');
return $stmt->execute(['user_id' => $userId]);
}
}